Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1f833c24
提交
1f833c24
authored
12月 01, 2011
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
bbf3f5dd
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
248 行增加
和
230 行删除
+248
-230
test_basic.py
theano/tensor/tests/test_basic.py
+248
-230
没有找到文件。
theano/tensor/tests/test_basic.py
浏览文件 @
1f833c24
...
@@ -1449,6 +1449,7 @@ class T_Shape(unittest.TestCase):
...
@@ -1449,6 +1449,7 @@ class T_Shape(unittest.TestCase):
s
=
shape
(
numpy
.
ones
((
5
,
3
,
10
)))
s
=
shape
(
numpy
.
ones
((
5
,
3
,
10
)))
self
.
assertTrue
((
eval_outputs
([
s
])
==
[
5
,
3
,
10
])
.
all
())
self
.
assertTrue
((
eval_outputs
([
s
])
==
[
5
,
3
,
10
])
.
all
())
class
T_max_and_argmax
(
unittest
.
TestCase
):
class
T_max_and_argmax
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
...
@@ -1456,108 +1457,110 @@ class T_max_and_argmax(unittest.TestCase):
...
@@ -1456,108 +1457,110 @@ class T_max_and_argmax(unittest.TestCase):
def
test0
(
self
):
def
test0
(
self
):
n
=
as_tensor_variable
(
5.0
)
n
=
as_tensor_variable
(
5.0
)
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
))
self
.
assertTrue
(
v
==
5.0
)
self
.
assertTrue
(
v
==
5.0
)
self
.
assertTrue
(
i
==
0
)
self
.
assertTrue
(
i
==
0
)
v
=
eval_outputs
(
max_and_argmax
(
n
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
)[
0
]
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
v
=
eval_outputs
(
max_and_argmax
(
n
)[
1
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
)[
1
]
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test1
(
self
):
def
test1
(
self
):
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
))
self
.
assertTrue
(
v
==
3
)
self
.
assertTrue
(
v
==
3
)
self
.
assertTrue
(
i
==
2
)
self
.
assertTrue
(
i
==
2
)
v
=
eval_outputs
(
max_and_argmax
(
n
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
)[
0
]
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test2
(
self
):
def
test2
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
-
1
)))
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
)[
0
]
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
def
test2b
(
self
):
def
test2b
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
0
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
0
))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
0
)))
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
#currently not supported
#currently not supported
#v = eval_outputs(max_and_argmax(n,[0,1])[0].shape)
#v = eval_outputs(max_and_argmax(n,[0,1])[0].shape)
#assert v.size==0
#assert v.size==0
def
test2_invalid
(
self
):
def
test2_invalid
(
self
):
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
# Silence expected error messages
# Silence expected error messages
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
oldlevel
=
_logger
.
level
oldlevel
=
_logger
.
level
_logger
.
setLevel
(
logging
.
CRITICAL
)
_logger
.
setLevel
(
logging
.
CRITICAL
)
try
:
try
:
try
:
try
:
eval_outputs
(
max_and_argmax
(
n
,
3
))
eval_outputs
(
max_and_argmax
(
n
,
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
finally
:
finally
:
_logger
.
setLevel
(
oldlevel
)
_logger
.
setLevel
(
oldlevel
)
def
test2_invalid_neg
(
self
):
def
test2_invalid_neg
(
self
):
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
old_stderr
=
sys
.
stderr
old_stderr
=
sys
.
stderr
sys
.
stderr
=
StringIO
.
StringIO
()
sys
.
stderr
=
StringIO
.
StringIO
()
try
:
try
:
try
:
try
:
eval_outputs
(
max_and_argmax
(
n
,
-
3
))
eval_outputs
(
max_and_argmax
(
n
,
-
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
finally
:
finally
:
sys
.
stderr
=
old_stderr
sys
.
stderr
=
old_stderr
def
test2_valid_neg
(
self
):
def
test2_valid_neg
(
self
):
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
i
.
shape
==
(
2
,))
self
.
assertTrue
(
i
.
shape
==
(
2
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
n
.
value
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
n
.
value
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
n
.
value
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
n
.
value
,
-
1
)))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
2
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
2
))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
i
.
shape
==
(
3
,))
self
.
assertTrue
(
i
.
shape
==
(
3
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
n
.
value
,
-
2
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
n
.
value
,
-
2
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
n
.
value
,
-
2
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
n
.
value
,
-
2
)))
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
)[
0
]
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
2
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
2
)[
0
]
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
def
test3
(
self
):
def
test3
(
self
):
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
0
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
0
))
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
1
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
1
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
2
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
2
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
2
)[
0
]
.
shape
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
2
)[
0
]
.
shape
)
assert
tuple
(
v
)
==
(
2
,
3
)
assert
tuple
(
v
)
==
(
2
,
3
)
def
test_grad
(
self
):
def
test_grad
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
def
check_grad_max
(
data
,
max_grad_data
,
axis
=
None
):
def
check_grad_max
(
data
,
max_grad_data
,
axis
=
None
):
...
@@ -1565,35 +1568,39 @@ class T_max_and_argmax(unittest.TestCase):
...
@@ -1565,35 +1568,39 @@ class T_max_and_argmax(unittest.TestCase):
Why this is needed? verify_grad is not enought?
Why this is needed? verify_grad is not enought?
"""
"""
#This work only for axis in [0,None]
#This work only for axis in [0,None]
assert
axis
in
[
0
,
None
]
assert
axis
in
[
0
,
None
]
z
=
numpy
.
zeros_like
(
data
)
z
=
numpy
.
zeros_like
(
data
)
z
=
z
.
flatten
()
z
=
z
.
flatten
()
argmax
=
numpy
.
argmax
(
data
,
axis
=
axis
)
argmax
=
numpy
.
argmax
(
data
,
axis
=
axis
)
if
argmax
.
ndim
==
0
:
if
argmax
.
ndim
==
0
:
z
[
argmax
]
+=
1
z
[
argmax
]
+=
1
else
:
else
:
for
id
,
v
in
enumerate
(
argmax
):
for
id
,
v
in
enumerate
(
argmax
):
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
=
z
.
reshape
(
data
.
shape
)
z
=
z
.
reshape
(
data
.
shape
)
assert
numpy
.
all
(
max_grad_data
==
z
)
assert
numpy
.
all
(
max_grad_data
==
z
)
#test grad of max
#test grad of max
#axis is the last one
#axis is the last one
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=-
1
)[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=-
1
)[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=-
1
)[
1
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=-
1
)[
1
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
0
])[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
0
])[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
0
])[
1
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
0
])[
1
],
[
data
])
check_grad_max
(
data
,
eval_outputs
(
grad
(
max_and_argmax
(
n
,
axis
=
0
)[
0
]
.
sum
(),
n
)),
axis
=
0
)
check_grad_max
(
data
,
eval_outputs
(
grad
(
max_and_argmax
(
n
,
axis
=
0
)[
0
]
.
sum
(),
n
)),
axis
=
0
)
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
1
])[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
1
])[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
1
])[
1
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
,
axis
=
[
1
])[
1
],
[
data
])
#check_grad_max(data,eval_outputs(grad(max_and_argmax(n,axis=1)[0],n)),axis=1)
#check_grad_max(data,eval_outputs(grad(
# max_and_argmax(n,axis=1)[0],n)),axis=1)
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
.
flatten
())[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
.
flatten
())[
0
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
.
flatten
())[
1
],
[
data
])
utt
.
verify_grad
(
lambda
v
:
max_and_argmax
(
v
.
flatten
())[
1
],
[
data
])
check_grad_max
(
data
,
eval_outputs
(
grad
(
max_and_argmax
(
n
.
flatten
())[
0
],
n
)))
check_grad_max
(
data
,
eval_outputs
(
grad
(
max_and_argmax
(
n
.
flatten
())[
0
],
n
)))
# Test 4d inner dimensions
# Test 4d inner dimensions
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
,
5
)
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
,
5
)
...
@@ -1608,60 +1615,60 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1608,60 +1615,60 @@ class T_argmin_argmax(unittest.TestCase):
utt
.
seed_rng
()
utt
.
seed_rng
()
MaxAndArgmax
.
debug
=
0
MaxAndArgmax
.
debug
=
0
def
test
0
(
self
):
def
test
_scalar
(
self
):
for
fct
in
[
argmin
,
argmax
]:
for
fct
in
[
argmin
,
argmax
]:
n
=
as_tensor_variable
(
5.0
)
n
=
as_tensor_variable
(
5.0
)
i
=
eval_outputs
(
fct
(
n
))
i
=
eval_outputs
(
fct
(
n
))
self
.
assertTrue
(
i
==
0
)
self
.
assertTrue
(
i
==
0
)
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test
1
(
self
):
def
test
_list
(
self
):
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
i
=
eval_outputs
(
argmin
(
n
))
i
=
eval_outputs
(
argmin
(
n
))
self
.
assertTrue
(
i
==
4
)
self
.
assertTrue
(
i
==
4
)
v
=
eval_outputs
(
argmin
(
n
)
.
shape
)
v
=
eval_outputs
(
argmin
(
n
)
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
i
=
eval_outputs
(
argmax
(
n
))
i
=
eval_outputs
(
argmax
(
n
))
self
.
assertTrue
(
i
==
2
)
self
.
assertTrue
(
i
==
2
)
v
=
eval_outputs
(
argmax
(
n
)
.
shape
)
v
=
eval_outputs
(
argmax
(
n
)
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test2
(
self
):
def
test2
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
i
=
eval_outputs
(
fct
(
n
,
-
1
))
i
=
eval_outputs
(
fct
(
n
,
-
1
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
-
1
)))
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
def
test2b
(
self
):
def
test2b
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
i
=
eval_outputs
(
fct
(
n
,
0
))
i
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
0
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
#currently not supported
#currently not supported
#v = eval_outputs(fct(n,[0,1]).shape)
#v = eval_outputs(fct(n,[0,1]).shape)
#assert v.size==0
#assert v.size==0
def
test2_invalid
(
self
):
def
test2_invalid
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
# Silence expected error messages
# Silence expected error messages
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
oldlevel
=
_logger
.
level
oldlevel
=
_logger
.
level
_logger
.
setLevel
(
logging
.
CRITICAL
)
_logger
.
setLevel
(
logging
.
CRITICAL
)
try
:
try
:
try
:
try
:
eval_outputs
(
fct
(
n
,
3
))
eval_outputs
(
fct
(
n
,
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
...
@@ -1669,13 +1676,13 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1669,13 +1676,13 @@ class T_argmin_argmax(unittest.TestCase):
_logger
.
setLevel
(
oldlevel
)
_logger
.
setLevel
(
oldlevel
)
def
test2_invalid_neg
(
self
):
def
test2_invalid_neg
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
old_stderr
=
sys
.
stderr
old_stderr
=
sys
.
stderr
sys
.
stderr
=
StringIO
.
StringIO
()
sys
.
stderr
=
StringIO
.
StringIO
()
try
:
try
:
try
:
try
:
eval_outputs
(
fct
(
n
,
-
3
))
eval_outputs
(
fct
(
n
,
-
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
...
@@ -1683,286 +1690,297 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1683,286 +1690,297 @@ class T_argmin_argmax(unittest.TestCase):
sys
.
stderr
=
old_stderr
sys
.
stderr
=
old_stderr
def
test2_valid_neg
(
self
):
def
test2_valid_neg
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
i
=
eval_outputs
(
fct
(
n
,
-
1
))
i
=
eval_outputs
(
fct
(
n
,
-
1
))
self
.
assertTrue
(
i
.
shape
==
(
2
,))
self
.
assertTrue
(
i
.
shape
==
(
2
,))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
-
1
)))
i
=
eval_outputs
(
fct
(
n
,
-
2
))
i
=
eval_outputs
(
fct
(
n
,
-
2
))
self
.
assertTrue
(
i
.
shape
==
(
3
,))
self
.
assertTrue
(
i
.
shape
==
(
3
,))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
-
2
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
-
2
)))
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
v
=
eval_outputs
(
fct
(
n
,
-
2
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
2
)
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
def
test3
(
self
):
def
test3
(
self
):
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
i
=
eval_outputs
(
fct
(
n
,
0
))
i
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
0
)))
i
=
eval_outputs
(
fct
(
n
,
1
))
i
=
eval_outputs
(
fct
(
n
,
1
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
1
)))
i
=
eval_outputs
(
fct
(
n
,
2
))
i
=
eval_outputs
(
fct
(
n
,
2
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
2
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
2
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
3
)
assert
tuple
(
v
)
==
(
2
,
3
)
def
test_grad_argmin
(
self
):
def
test_grad_argmin
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
#test grad of argmin
#test grad of argmin
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
.
flatten
()),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmin
(
v
.
flatten
()),
[
data
])
try
:
try
:
grad
(
argmin
(
n
,
axis
=-
1
),
n
)
grad
(
argmin
(
n
,
axis
=-
1
),
n
)
raise
Exception
(
'Expected an error'
)
raise
Exception
(
'Expected an error'
)
except
TypeError
:
except
TypeError
:
pass
pass
def
test_grad_argmax
(
self
):
def
test_grad_argmax
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
#test grad of argmax
#test grad of argmax
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
.
flatten
()),
[
data
])
utt
.
verify_grad
(
lambda
v
:
argmax
(
v
.
flatten
()),
[
data
])
try
:
try
:
grad
(
argmax
(
n
,
axis
=-
1
),
n
)
grad
(
argmax
(
n
,
axis
=-
1
),
n
)
raise
Exception
(
'Expected an error'
)
raise
Exception
(
'Expected an error'
)
except
TypeError
:
except
TypeError
:
pass
pass
class
T_min_max
(
unittest
.
TestCase
):
class
T_min_max
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
MaxAndArgmax
.
debug
=
0
MaxAndArgmax
.
debug
=
0
def
test
0
(
self
):
def
test
_scalar
(
self
):
for
fct
in
[
max
,
min
]:
for
fct
in
[
max
,
min
]:
n
=
as_tensor_variable
(
5.0
)
n
=
as_tensor_variable
(
5.0
)
v
=
eval_outputs
(
fct
(
n
))
v
=
eval_outputs
(
fct
(
n
))
self
.
assertTrue
(
v
==
5.0
)
self
.
assertTrue
(
v
==
5.0
)
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test
1
(
self
):
def
test
_list
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
n
=
as_tensor_variable
([
1
,
2
,
3
,
2
,
-
6
])
v
=
eval_outputs
([
fct
(
n
)])
v
=
eval_outputs
([
fct
(
n
)])
self
.
assertTrue
(
v
==
nfct
(
n
.
value
))
self
.
assertTrue
(
v
==
nfct
(
n
.
value
))
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
)
.
shape
)
assert
len
(
v
)
==
0
assert
len
(
v
)
==
0
def
test2
(
self
):
def
test2
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
v
=
eval_outputs
(
fct
(
n
,
-
1
))
v
=
eval_outputs
(
fct
(
n
,
-
1
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
-
1
)))
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
def
test2b
(
self
):
def
test2b
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
v
=
eval_outputs
(
fct
(
n
,
0
))
v
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
0
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
])
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
])
.
shape
)
assert
v
.
size
==
0
assert
v
.
size
==
0
def
test2_invalid
(
self
):
def
test2_invalid
(
self
):
for
fct
in
[
max
,
min
]:
for
fct
in
[
max
,
min
]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
# Silence expected error messages
# Silence expected error messages
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
_logger
=
logging
.
getLogger
(
'theano.gof.opt'
)
oldlevel
=
_logger
.
level
oldlevel
=
_logger
.
level
_logger
.
setLevel
(
logging
.
CRITICAL
)
_logger
.
setLevel
(
logging
.
CRITICAL
)
try
:
try
:
try
:
try
:
eval_outputs
(
fct
(
n
,
3
))
eval_outputs
(
fct
(
n
,
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
finally
:
finally
:
_logger
.
setLevel
(
oldlevel
)
_logger
.
setLevel
(
oldlevel
)
def
test2_invalid_neg
(
self
):
def
test2_invalid_neg
(
self
):
for
fct
in
[
max
,
min
]:
for
fct
in
[
max
,
min
]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
old_stderr
=
sys
.
stderr
old_stderr
=
sys
.
stderr
sys
.
stderr
=
StringIO
.
StringIO
()
sys
.
stderr
=
StringIO
.
StringIO
()
try
:
try
:
try
:
try
:
eval_outputs
(
fct
(
n
,
-
3
))
eval_outputs
(
fct
(
n
,
-
3
))
assert
False
assert
False
except
ValueError
,
e
:
except
ValueError
,
e
:
pass
pass
finally
:
finally
:
sys
.
stderr
=
old_stderr
sys
.
stderr
=
old_stderr
def
test2_valid_neg
(
self
):
def
test2_valid_neg
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
))
v
=
eval_outputs
(
fct
(
n
,
-
1
))
v
=
eval_outputs
(
fct
(
n
,
-
1
))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
-
1
)))
v
=
eval_outputs
(
fct
(
n
,
-
2
))
v
=
eval_outputs
(
fct
(
n
,
-
2
))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
-
2
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
-
2
)))
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
assert
v
==
(
2
)
assert
v
==
(
2
)
v
=
eval_outputs
(
fct
(
n
,
-
2
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
-
2
)
.
shape
)
assert
v
==
(
3
)
assert
v
==
(
3
)
def
test3
(
self
):
def
test3
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
v
=
eval_outputs
(
fct
(
n
,
0
))
v
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
0
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
0
)))
v
=
eval_outputs
(
fct
(
n
,
1
))
v
=
eval_outputs
(
fct
(
n
,
1
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
1
)))
v
=
eval_outputs
(
fct
(
n
,
2
))
v
=
eval_outputs
(
fct
(
n
,
2
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
2
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
2
)))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
]))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
]))
self
.
assertTrue
(
v
.
shape
==
(
4
,))
self
.
assertTrue
(
v
.
shape
==
(
4
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
1
),
0
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
1
),
0
)))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
2
]))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
2
]))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
v
.
shape
==
(
3
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
0
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
0
)))
v
=
eval_outputs
(
fct
(
n
,
[
1
,
2
]))
v
=
eval_outputs
(
fct
(
n
,
[
1
,
2
]))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
v
.
shape
==
(
2
,))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
1
)))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
,
2
]))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
,
2
]))
self
.
assertTrue
(
v
.
shape
==
())
self
.
assertTrue
(
v
.
shape
==
())
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
3
)
assert
tuple
(
v
)
==
(
2
,
3
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
])
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
])
.
shape
)
self
.
assertTrue
(
v
==
(
4
,))
self
.
assertTrue
(
v
==
(
4
,))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
2
])
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
2
])
.
shape
)
self
.
assertTrue
(
v
==
(
3
,))
self
.
assertTrue
(
v
==
(
3
,))
v
=
eval_outputs
(
fct
(
n
,
[
1
,
2
])
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
[
1
,
2
])
.
shape
)
self
.
assertTrue
(
v
==
(
2
,))
self
.
assertTrue
(
v
==
(
2
,))
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
,
2
])
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
[
0
,
1
,
2
])
.
shape
)
self
.
assertTrue
(
v
.
size
==
0
)
self
.
assertTrue
(
v
.
size
==
0
)
def
test_grad_max
(
self
):
def
test_grad_max
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
def
check_grad_max
(
data
,
max_grad_data
,
axis
=
None
):
def
check_grad_max
(
data
,
max_grad_data
,
axis
=
None
):
#This work only for axis in [0,None]
#This work only for axis in [0,None]
assert
axis
in
[
0
,
None
]
assert
axis
in
[
0
,
None
]
z
=
numpy
.
zeros_like
(
data
)
z
=
numpy
.
zeros_like
(
data
)
z
=
z
.
flatten
()
z
=
z
.
flatten
()
argmax
=
numpy
.
argmax
(
data
,
axis
=
axis
)
argmax
=
numpy
.
argmax
(
data
,
axis
=
axis
)
if
argmax
.
ndim
==
0
:
if
argmax
.
ndim
==
0
:
z
[
numpy
.
argmax
(
data
,
axis
=
axis
)]
+=
1
z
[
numpy
.
argmax
(
data
,
axis
=
axis
)]
+=
1
else
:
else
:
for
id
,
v
in
enumerate
(
argmax
):
for
id
,
v
in
enumerate
(
argmax
):
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
=
z
.
reshape
(
data
.
shape
)
z
=
z
.
reshape
(
data
.
shape
)
assert
numpy
.
all
(
max_grad_data
==
z
)
assert
numpy
.
all
(
max_grad_data
==
z
)
#test grad of max
#test grad of max
#axis is the last one
#axis is the last one
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=
[
0
]),
[
data
])
check_grad_max
(
data
,
eval_outputs
(
grad
(
max
(
n
,
axis
=
0
)
.
sum
(),
n
)),
axis
=
0
)
check_grad_max
(
data
,
eval_outputs
(
grad
(
max
(
n
,
axis
=
0
)
.
sum
(),
n
)),
axis
=
0
)
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
max
(
v
,
axis
=
[
1
]),
[
data
])
#check_grad_max(data,eval_outputs(grad(max(n,axis=1),n)),axis=1)
#check_grad_max(data,eval_outputs(grad(max(n,axis=1),n)),axis=1)
utt
.
verify_grad
(
lambda
v
:
max
(
v
.
flatten
()),
[
data
])
utt
.
verify_grad
(
lambda
v
:
max
(
v
.
flatten
()),
[
data
])
check_grad_max
(
data
,
eval_outputs
(
grad
(
max
(
n
.
flatten
()),
n
)))
check_grad_max
(
data
,
eval_outputs
(
grad
(
max
(
n
.
flatten
()),
n
)))
def
test_grad_min
(
self
):
def
test_grad_min
(
self
):
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
def
check_grad_min
(
data
,
min_grad_data
,
axis
=
None
):
def
check_grad_min
(
data
,
min_grad_data
,
axis
=
None
):
#This work only for axis in [0,None]
#This work only for axis in [0,
None]
assert
axis
in
[
0
,
None
]
assert
axis
in
[
0
,
None
]
z
=
numpy
.
zeros_like
(
data
)
z
=
numpy
.
zeros_like
(
data
)
z
=
z
.
flatten
()
z
=
z
.
flatten
()
argmin
=
numpy
.
argmin
(
data
,
axis
=
axis
)
argmin
=
numpy
.
argmin
(
data
,
axis
=
axis
)
if
argmin
.
ndim
==
0
:
if
argmin
.
ndim
==
0
:
z
[
numpy
.
argmin
(
data
,
axis
=
axis
)]
+=
1
z
[
numpy
.
argmin
(
data
,
axis
=
axis
)]
+=
1
else
:
else
:
for
id
,
v
in
enumerate
(
argmin
):
for
id
,
v
in
enumerate
(
argmin
):
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
[
v
*
numpy
.
prod
(
data
.
shape
[
data
.
ndim
-
1
:
axis
:
-
1
])
+
id
]
+=
1
z
=
z
.
reshape
(
data
.
shape
)
z
=
z
.
reshape
(
data
.
shape
)
assert
numpy
.
all
(
min_grad_data
==
z
)
assert
numpy
.
all
(
min_grad_data
==
z
)
#test grad of min
#test grad of min
#axis is the last one
#axis is the last one
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=-
1
),
[
data
])
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=
[
0
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=
[
0
]),
[
data
])
check_grad_min
(
data
,
eval_outputs
(
grad
(
min
(
n
,
axis
=
0
)
.
sum
(),
n
)),
axis
=
0
)
check_grad_min
(
data
,
eval_outputs
(
grad
(
min
(
n
,
axis
=
0
)
.
sum
(),
n
)),
axis
=
0
)
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=
[
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
min
(
v
,
axis
=
[
1
]),
[
data
])
#check_grad_min(data,eval_outputs(grad(min(n,axis=1),n)),axis=1)
#check_grad_min(data,eval_outputs(grad(min(n,axis=1),n)),axis=1)
utt
.
verify_grad
(
lambda
v
:
min
(
v
.
flatten
()),
[
data
])
utt
.
verify_grad
(
lambda
v
:
min
(
v
.
flatten
()),
[
data
])
check_grad_min
(
data
,
eval_outputs
(
grad
(
min
(
n
.
flatten
()),
n
)))
check_grad_min
(
data
,
eval_outputs
(
grad
(
min
(
n
.
flatten
()),
n
)))
def
_grad_list
(
self
):
def
_grad_list
(
self
):
"""
"""
Test the gradient when we have multiple axis at the same time.
Test the gradient when we have multiple axis at the same time.
This not implemented, so we disable the test. See ticket: http://trac-hg.assembla.com/theano/ticket/511
This not implemented, so we disable the test. See ticket:
http://trac-hg.assembla.com/theano/ticket/511
"""
"""
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
for
fct
in
[
max_and_argmax
,
max
,
min
]:
for
fct
in
[
max_and_argmax
,
max
,
min
]:
utt
.
verify_grad
(
lambda
v
:
fct
(
v
,
axis
=
[
0
,
1
]),
[
data
])
utt
.
verify_grad
(
lambda
v
:
fct
(
v
,
axis
=
[
0
,
1
]),
[
data
])
#check_grad_max(data,eval_outputs(grad(max_and_argmax(n,axis=1)[0],n)),axis=1)
#check_grad_max(data, eval_outputs(grad(max_and_argmax(n,
#axis=1)[0], n)),axis=1)
class
T_subtensor
(
unittest
.
TestCase
):
class
T_subtensor
(
unittest
.
TestCase
):
"""
"""
This is build in a way that allow to reuse it to test the equivalent gpu op.
This is build in a way that allow to reuse it to test the
equivalent gpu op.
"""
"""
def
__init__
(
self
,
name
,
shared
=
_shared
,
def
__init__
(
self
,
name
,
shared
=
_shared
,
sub
=
tensor
.
Subtensor
,
sub
=
tensor
.
Subtensor
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论