Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
90c3833e
提交
90c3833e
authored
12月 02, 2011
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #242 from nouiz/max_default
change the default of theano.{max,min,argmax,argmin,max_and_argmax} to t...
上级
c08a6f31
586c0e3c
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
366 行增加
和
552 行删除
+366
-552
sharedvalue.py
theano/compile/sharedvalue.py
+0
-30
basic.py
theano/tensor/basic.py
+79
-170
opt_uncanonicalize.py
theano/tensor/opt_uncanonicalize.py
+1
-1
test_basic.py
theano/tensor/tests/test_basic.py
+210
-258
test_opt_uncanonicalize.py
theano/tensor/tests/test_opt_uncanonicalize.py
+76
-72
test_sharedvar.py
theano/tensor/tests/test_sharedvar.py
+0
-21
没有找到文件。
theano/compile/sharedvalue.py
浏览文件 @
90c3833e
...
@@ -18,13 +18,6 @@ from theano.gof import Container, Variable, generic
...
@@ -18,13 +18,6 @@ from theano.gof import Container, Variable, generic
_logger
=
logging
.
getLogger
(
'theano.compile.sharedvalue'
)
_logger
=
logging
.
getLogger
(
'theano.compile.sharedvalue'
)
AddConfigVar
(
'shared.value_borrows'
,
(
"DEPRECATED. You should not use the 'value' property of shared"
" variables, but use the .get_value() and .set_value() methods."
" False: shared variables 'value' property is guaranteed to not"
" alias theano-managed memory. True: no guarantee, but faster."
),
BoolParam
(
True
),
in_c_key
=
False
)
class
SharedVariable
(
Variable
):
class
SharedVariable
(
Variable
):
"""
"""
...
@@ -125,29 +118,6 @@ class SharedVariable(Variable):
...
@@ -125,29 +118,6 @@ class SharedVariable(Variable):
cp
.
tag
=
copy
.
copy
(
self
.
tag
)
cp
.
tag
=
copy
.
copy
(
self
.
tag
)
return
cp
return
cp
def
_value_get
(
self
):
warnings
.
warn
((
"The .value property of shared variables is deprecated."
" You should use the .get_value() method instead."
),
stacklevel
=
2
)
return
self
.
get_value
(
borrow
=
config
.
shared
.
value_borrows
,
return_internal_type
=
False
)
def
_value_set
(
self
,
new_value
):
warnings
.
warn
((
"The .value property of shared variables is deprecated."
" You should use the .set_value() method instead."
),
stacklevel
=
2
)
return
self
.
set_value
(
new_value
,
borrow
=
config
.
shared
.
value_borrows
)
#TODO: USE A CONFIG VARIABLE TO set these get/set methods to the non-borrowing versions
# Semantically things are clearer when using non-borrow versions. That should be the
# default. The default support transparently (if slowly) when the 'raw' value is in a
# different memory space (e.g. GPU or other machine).
value
=
property
(
_value_get
,
_value_set
,
doc
=
(
"DEPRECATED. Shortcut for self.get_value() and "
"self.set_value(). "
"The `borrow` argument to these methods is read from "
"`theano.config.shared.value_borrows`. "
"You should call get_value() and set_value() directly."
))
def
filter_update
(
self
,
update
):
def
filter_update
(
self
,
update
):
"""
"""
When this shared variable is updated by a pfunc, the update value will be run through this function.
When this shared variable is updated by a pfunc, the update value will be run through this function.
...
...
theano/tensor/basic.py
浏览文件 @
90c3833e
...
@@ -1795,6 +1795,7 @@ shape = Shape()
...
@@ -1795,6 +1795,7 @@ shape = Shape()
_shape
=
shape
#was used in the past, now use shape directly.
_shape
=
shape
#was used in the past, now use shape directly.
pprint
.
assign
(
_shape
,
printing
.
MemberPrinter
(
'shape'
))
pprint
.
assign
(
_shape
,
printing
.
MemberPrinter
(
'shape'
))
class
SpecifyShape
(
Op
):
class
SpecifyShape
(
Op
):
"""
"""
L{Op} put into the graph the user provided shape
L{Op} put into the graph the user provided shape
...
@@ -1808,14 +1809,18 @@ class SpecifyShape(Op):
...
@@ -1808,14 +1809,18 @@ class SpecifyShape(Op):
@note: We currently don't support specifying partial shape information.
@note: We currently don't support specifying partial shape information.
"""
"""
view_map
=
{
0
:
[
0
]}
view_map
=
{
0
:
[
0
]}
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
return
type
(
self
)
==
type
(
other
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
def
make_node
(
self
,
x
,
shape
):
def
make_node
(
self
,
x
,
shape
):
if
not
isinstance
(
x
,
Variable
):
if
not
isinstance
(
x
,
Variable
):
x
=
as_tensor_variable
(
x
)
x
=
as_tensor_variable
(
x
)
shape
=
as_tensor_variable
(
shape
)
shape
=
as_tensor_variable
(
shape
)
return
Apply
(
self
,
[
x
,
shape
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
,
shape
],
[
x
.
type
()])
...
@@ -1823,22 +1828,22 @@ class SpecifyShape(Op):
...
@@ -1823,22 +1828,22 @@ class SpecifyShape(Op):
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
shape
=
inp
x
,
shape
=
inp
out
,
=
out_
out
,
=
out_
assert
numpy
.
all
(
x
.
shape
==
shape
),
(
"got shape"
,
x
.
shape
,
assert
numpy
.
all
(
x
.
shape
==
shape
),
(
"got shape"
,
x
.
shape
,
"expected"
,
shape
)
"expected"
,
shape
)
out
[
0
]
=
x
out
[
0
]
=
x
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
xshape
,
sshape
=
shapes
xshape
,
sshape
=
shapes
new_shape
=
[]
new_shape
=
[]
for
dim
in
xrange
(
node
.
inputs
[
0
]
.
ndim
):
for
dim
in
xrange
(
node
.
inputs
[
0
]
.
ndim
):
try
:
try
:
s
=
get_constant_value
(
node
.
inputs
[
1
][
dim
])
s
=
get_constant_value
(
node
.
inputs
[
1
][
dim
])
s
=
as_tensor_variable
(
s
)
s
=
as_tensor_variable
(
s
)
new_shape
.
append
(
s
)
new_shape
.
append
(
s
)
except
TypeError
,
e
:
except
TypeError
,
e
:
new_shape
.
append
(
node
.
inputs
[
1
][
dim
])
new_shape
.
append
(
node
.
inputs
[
1
][
dim
])
assert
len
(
new_shape
)
==
len
(
xshape
)
assert
len
(
new_shape
)
==
len
(
xshape
)
return
[
new_shape
]
return
[
new_shape
]
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
...
@@ -1847,9 +1852,10 @@ class SpecifyShape(Op):
...
@@ -1847,9 +1852,10 @@ class SpecifyShape(Op):
# Should I set an SpecifyShape on gz? I think so
# Should I set an SpecifyShape on gz? I think so
# But I don't do it now as we need to make an optimization
# But I don't do it now as we need to make an optimization
# to remove that op from the graph to don't block other optimization
# to remove that op from the graph to don't block other optimization
# Should I do an optimizer that will remove the SpecifyShape? I think Yes
# Should I do an optimizer that will remove the SpecifyShape?
# I think Yes
return
[
gz
,
None
]
return
[
gz
,
None
]
return
[
specify_shape
(
gz
,
s
),
None
]
return
[
specify_shape
(
gz
,
s
),
None
]
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
if
eval_points
[
0
]
is
None
:
if
eval_points
[
0
]
is
None
:
...
@@ -1860,97 +1866,83 @@ class SpecifyShape(Op):
...
@@ -1860,97 +1866,83 @@ class SpecifyShape(Op):
specify_shape
=
SpecifyShape
()
specify_shape
=
SpecifyShape
()
class
MaxAndArgmax
(
Op
):
class
MaxAndArgmax
(
Op
):
"""Calculate the max and argmax over a given axis.
"""Calculate the max and argmax over a given axis.
.. note::
If axis is None it means to calculate the max over the last dimension which is
DIFFERENT FROM NUMPY!!
To have the behavior of numpy do a flatten of the input before passing the data to this op.
If the input to flatten is not ccontiguous, this will make a copy to a contiguous version.
"""
"""
nin
=
2
# tensor, axis
nin
=
2
# tensor, axis
nout
=
2
# max val, max idx
nout
=
2
# max val, max idx
E_axis
=
'invalid axis'
E_axis
=
'invalid axis'
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
return
type
(
self
)
==
type
(
other
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
make_node
(
self
,
x
,
axis
=
'DEFAULT'
):
def
make_node
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor_variable
(
x
)
x
=
_as_tensor_variable
(
x
)
if
x
.
type
.
ndim
<=
1
and
axis
in
(
'DEFAULT'
,
None
):
if
isinstance
(
axis
,
int
):
# The old and new behavior are not different.
axis
=
0
if
axis
==
'DEFAULT'
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The default axis of MaxAndArgmax will change! "
"Now we return the max and the armax over the last dimensions. "
"It will change to be the same as numpy: the max and argmax over "
"all dimensions. To hide this warning and be compatible with the "
"future behavior, set axis to -1 to have the current behavior. "
"MaxAndArgmax currently support axis over only 1 dimensions, so "
"you must flatten the tensor to have the futur behavior."
),
stacklevel
=
3
)
elif
axis
is
None
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The behavior of MaxAndArgmax when axis==None will "
"change! Now we return the max and argmax over the last "
"dimensions. It will change to the max and argmax over all "
"dimensions as numpy. To hide this warning and be compatible with "
"the future behavior, set axis to -1 to have the current behavior. "
"MaxAndArgmax currently support axis over only 1 dimensions, so "
"you must flatten the tensor to have the futur behavior."
),
stacklevel
=
3
)
if
isinstance
(
axis
,
int
):
axis
=
[
axis
]
axis
=
[
axis
]
elif
isinstance
(
axis
,(
tuple
,
list
)):
elif
isinstance
(
axis
,
(
tuple
,
list
)):
assert
len
(
axis
)
==
1
,
"MaxAndArgmax don't support multiple axis. the max fct support it."
if
len
(
axis
)
!=
1
:
#we make the axis all positive to make the infer_shape work with negative axis
list
(
axis
)
if
x
.
type
.
ndim
>
0
:
axis
.
sort
()
for
id
,
a
in
enumerate
(
axis
):
assert
axis
==
range
(
x
.
type
.
ndim
),
(
if
not
isinstance
(
a
,
TensorVariable
)
and
a
<
0
:
"MaxAndArgmax don't support multiple"
if
-
a
>
x
.
type
.
ndim
:
" axis. the max fct support it."
)
# we make the axis all positive to make the infer_shape work
# with negative axis
if
x
.
type
.
ndim
>
0
and
axis
is
not
None
:
for
id
,
a
in
enumerate
(
axis
):
if
not
isinstance
(
a
,
TensorVariable
)
and
a
<
0
:
if
-
a
>
x
.
type
.
ndim
:
raise
ValueError
(
'axis out of range'
)
raise
ValueError
(
'axis out of range'
)
axis
[
id
]
=
x
.
type
.
ndim
+
a
axis
[
id
]
=
x
.
type
.
ndim
+
a
axis
=
_as_tensor_variable
(
axis
)
if
axis
is
None
:
axis
=
_as_tensor_variable
(
range
(
x
.
type
.
ndim
))
else
:
axis
=
_as_tensor_variable
(
axis
)
inputs
=
[
x
,
axis
]
inputs
=
[
x
,
axis
]
#TODO: figure things out if axis is a constant
broadcastable
=
[
False
]
*
(
x
.
type
.
ndim
-
len
(
axis
.
data
))
broadcastable
=
[
False
]
*
(
x
.
type
.
ndim
-
1
)
outputs
=
[
tensor
(
x
.
type
.
dtype
,
broadcastable
,
name
=
'max'
),
outputs
=
[
tensor
(
x
.
type
.
dtype
,
broadcastable
,
name
=
'max'
),
tensor
(
'int32'
,
broadcastable
,
name
=
'argmax'
)]
tensor
(
'int32'
,
broadcastable
,
name
=
'argmax'
)]
return
Apply
(
self
,
inputs
,
outputs
)
return
Apply
(
self
,
inputs
,
outputs
)
def
perform
(
self
,
node
,
inp
,
outs
):
def
perform
(
self
,
node
,
inp
,
outs
):
x
,
axis
=
inp
x
,
axis
=
inp
max
,
max_idx
=
outs
max
,
max_idx
=
outs
if
len
(
axis
)
==
0
or
python_all
(
axis
==
range
(
x
.
ndim
)):
axis
=
None
max
[
0
]
=
numpy
.
asarray
(
numpy
.
max
(
x
,
axis
))
max
[
0
]
=
numpy
.
asarray
(
numpy
.
max
(
x
,
axis
))
max_idx
[
0
]
=
theano
.
_asarray
(
numpy
.
argmax
(
x
,
axis
),
dtype
=
'int32'
)
max_idx
[
0
]
=
theano
.
_asarray
(
numpy
.
argmax
(
x
,
axis
),
dtype
=
'int32'
)
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
ishape
,
axis_shape
=
shapes
ishape
,
axis_shape
=
shapes
axis
=
node
.
inputs
[
1
]
axis
=
node
.
inputs
[
1
]
if
axis
is
None
:
if
python_all
(
axis
.
data
==
range
(
node
.
inputs
[
0
]
.
ndim
)):
return
[(),()]
return
[(),
()]
rval
=
tuple
([
ishape
[
i
]
for
(
i
,
b
)
in
enumerate
(
node
.
inputs
[
0
]
.
type
.
broadcastable
)
if
i
!=
axis
.
data
])
rval
=
tuple
([
ishape
[
i
]
for
(
i
,
b
)
in
enumerate
(
return
[
rval
,
rval
]
node
.
inputs
[
0
]
.
type
.
broadcastable
)
if
i
!=
axis
.
data
])
return
[
rval
,
rval
]
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
if
eval_points
[
0
]
is
None
:
if
eval_points
[
0
]
is
None
:
return
[
None
,
None
]
return
[
None
,
None
]
if
not
isinstance
(
inputs
[
1
],
theano
.
Constant
):
if
not
isinstance
(
inputs
[
1
],
theano
.
Constant
):
raise
ValueError
(
(
'R_op supported for arg_max only for '
raise
ValueError
((
'R_op supported for arg_max only for '
'constant axis!'
))
'constant axis!'
))
if
inputs
[
1
]
.
data
>
1
:
if
inputs
[
1
]
.
data
>
1
:
raise
ValueError
(
(
'R_op supported for arg_max only when '
raise
ValueError
((
'R_op supported for arg_max only when '
' axis is 0 or 1'
))
' axis is 0 or 1'
))
if
inputs
[
0
]
.
ndim
!=
2
:
if
inputs
[
0
]
.
ndim
!=
2
:
raise
ValueError
(
(
'R_op supported for arg_max only when '
raise
ValueError
((
'R_op supported for arg_max only when '
' input is a matrix'
))
' input is a matrix'
))
max_vals
,
max_pos
=
self
.
make_node
(
*
inputs
)
.
outputs
max_vals
,
max_pos
=
self
.
make_node
(
*
inputs
)
.
outputs
if
inputs
[
1
]
.
data
==
0
:
if
inputs
[
1
]
.
data
==
0
:
return
[
eval_points
[
0
][
max_pos
,
arange
(
eval_points
[
0
]
.
shape
[
1
])],
None
]
return
[
eval_points
[
0
][
max_pos
,
arange
(
eval_points
[
0
]
.
shape
[
1
])],
None
]
else
:
else
:
return
[
eval_points
[
0
][
arange
(
eval_points
[
0
]
.
shape
[
0
]),
return
[
eval_points
[
0
][
arange
(
eval_points
[
0
]
.
shape
[
0
]),
max_pos
],
None
]
max_pos
],
None
]
...
@@ -1987,112 +1979,48 @@ class MaxAndArgmax(Op):
...
@@ -1987,112 +1979,48 @@ class MaxAndArgmax(Op):
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
_max_and_argmax
=
MaxAndArgmax
()
_max_and_argmax
=
MaxAndArgmax
()
@_redefine_asRoutine
(
_max_and_argmax
)
@_redefine_asRoutine
(
_max_and_argmax
)
def
max_and_argmax
(
a
):
def
max_and_argmax
(
a
):
pass
pass
@constructor
@constructor
def
max
(
x
,
axis
=
'DEFAULT'
):
def
max
(
x
,
axis
=
None
):
"""
"""
Return maximum elements obtained by iterating over given axis
Return maximum elements obtained by iterating over given axis
Default axis is
the last one. This will change
.
Default axis is
None: max over all dimensions
.
:note: we return an error as numpy when we reduce a dim with a shape of 0
:note: we return an error as numpy when we reduce a dim with a shape of 0
:note2: see MaxAndArgmax note for a difference between numpy and theano when axis==None
"""
"""
if
x
.
type
.
ndim
<=
1
and
axis
in
(
'DEFAULT'
,
None
):
if
isinstance
(
axis
,
(
list
,
tuple
))
and
len
(
axis
)
>
1
:
# The old and new behavior are not different.
return
CAReduce
(
scal
.
maximum
,
axis
)(
x
)
axis
=
0
elif
axis
==
'DEFAULT'
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The default axis of max will change! Now we return the "
"max over the last dimensions. It will change to be the same as numpy: "
"the max over all dimensions. To hide this warning and be compatible "
"with the future behavior, set axis to -1 to have the current "
"behavior. To have the futur behavior set axis to range(nb dim), but "
"this don't support the grad. To have the grad, you must flatten the "
"tensor before calling max()."
),
stacklevel
=
2
)
elif
axis
is
None
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The behavior of max when axis==None will change! Now "
"we return the max over the last dimensions. It will change to the max "
"over all dimensions as numpy. To hide this warning and be compatible "
"with the future behavior, set axis to -1 to have the current "
"behavior. To have the futur behavior set axis to range(nb dim), but "
"this don't support the grad. To have the grad, you must flatten the "
"tensor before calling max()."
),
stacklevel
=
2
)
if
isinstance
(
axis
,(
list
,
tuple
))
and
len
(
axis
)
>
1
:
return
CAReduce
(
scal
.
maximum
,
axis
)(
x
)
try
:
try
:
const
=
get_constant_value
(
axis
)
const
=
get_constant_value
(
axis
)
return
CAReduce
(
scal
.
maximum
,
list
(
const
))(
x
)
return
CAReduce
(
scal
.
maximum
,
list
(
const
))(
x
)
except
Exception
:
except
Exception
:
return
max_and_argmax
(
x
,
axis
)[
0
]
return
max_and_argmax
(
x
,
axis
)[
0
]
@constructor
@constructor
def
argmax
(
x
,
axis
=
'DEFAULT'
):
def
argmax
(
x
,
axis
=
None
):
"""
"""
Return indexes of maximum elements obtained by iterating over given axis
Return indexes of maximum elements obtained by iterating over given axis
Default axis is the last one. This will change.
When axis is None (the default value), the argmax is performed
over the flattened tensor.
"""
"""
if
x
.
type
.
ndim
<=
1
and
axis
in
(
'DEFAULT'
,
None
):
# The old and new behavior are not different.
axis
=
0
elif
axis
==
'DEFAULT'
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The default axis of argmax will change! Now we return "
"the argmax over the last dimensions. It will change to be the same as "
"numpy: the argmax over all dimensions. To hide this warning and be "
"compatible with the future behavior, set axis to -1 to have the "
"current behavior. To have the futur behavior, you must flatten the "
"tensor before calling max()."
),
stacklevel
=
2
)
elif
axis
is
None
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The behavior of argmax when axis==None will change! "
"Now we return the argmax over the last dimensions. It will change to "
"the argmax over all dimensions as numpy. To hide this warning and be "
"compatible with the future behavior, set axis to -1 to have the "
"current behavior. To have the futur behavior, you must flatten the "
"tensor before calling argmax()."
),
stacklevel
=
2
)
# In python (using MaxAndArgmax.perform()) this leads to an wasteful
# In python (using MaxAndArgmax.perform()) this leads to an wasteful
# implementation that goes through the data twice instead of once
# implementation that goes through the data twice instead of once
# but when Argmax.c_impl() is in place, it should be fine.
# but when Argmax.c_impl() is in place, it should be fine.
return
max_and_argmax
(
x
,
axis
)[
1
]
return
max_and_argmax
(
x
,
axis
)[
1
]
@constructor
@constructor
def
min
(
x
,
axis
=
'DEFAULT'
):
def
min
(
x
,
axis
=
None
):
if
x
.
type
.
ndim
<=
1
and
axis
in
(
'DEFAULT'
,
None
):
# The old and new behavior are not different.
axis
=
0
elif
axis
==
'DEFAULT'
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The default axis of min will change! Now we return the "
"min over the last dimensions. It will change to be the same as numpy: "
"the min over all dimensions. To hide this warning and be compatible "
"with the future behavior, set axis to -1 to have the current "
"behavior. To have the future behavior, set axis to range(x.ndim), but "
"this does not support the grad. To be able to get the grad, you must "
"flatten the tensor before calling min()."
),
stacklevel
=
2
)
elif
axis
is
None
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The behavior of min when axis is None will change! Now "
"we return the min over the last dimensions. It will change to the min "
"over all dimensions as numpy. To hide this warning and be compatible "
"with the future behavior, set axis to -1 to have the current "
"behavior. To have the future behavior, set axis to range(x.ndim), but "
"this does not support the grad. To be able to get the grad, you must "
"flatten the tensor before calling min()."
),
stacklevel
=
2
)
str_x_type
=
str
(
x
.
dtype
)
str_x_type
=
str
(
x
.
dtype
)
if
str_x_type
.
startswith
(
'float'
)
or
str_x_type
in
int_dtypes
:
if
str_x_type
.
startswith
(
'float'
)
or
str_x_type
in
int_dtypes
:
return
-
max
(
-
x
,
axis
=
axis
)
return
-
max
(
-
x
,
axis
=
axis
)
...
@@ -2100,29 +2028,9 @@ def min(x, axis='DEFAULT'):
...
@@ -2100,29 +2028,9 @@ def min(x, axis='DEFAULT'):
#Be careful about unsigned integers, complex
#Be careful about unsigned integers, complex
raise
NotImplementedError
()
raise
NotImplementedError
()
@constructor
@constructor
def
argmin
(
x
,
axis
=
'DEFAULT'
):
def
argmin
(
x
,
axis
=
None
):
if
x
.
type
.
ndim
<=
1
and
axis
in
(
'DEFAULT'
,
None
):
# The old and new behavior are not different.
axis
=
0
elif
axis
==
'DEFAULT'
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The default axis of argmin will change! Now we return "
"the argmin over the last dimensions. It will change to be the same as "
"numpy: the argmin over all dimensions. To hide this warning and be "
"compatible with the future behavior, set axis to -1 to have the "
"current behavior. To have the futur behavior, you must flatten the "
"axis before calling argmin."
),
stacklevel
=
2
)
elif
axis
is
None
:
axis
=
x
.
type
.
ndim
-
1
warnings
.
warn
((
"The behavior of argmin when axis==None will change! "
"Now we return the argmin over the last dimensions. It will change to "
"the argmin over all dimensions as numpy. To hide this warning and be "
"compatible with the future behavior, set axis to -1 to have the "
"current behavior. To have the futur behavior, you must flatten the "
"axis before calling argmin."
),
stacklevel
=
2
)
str_x_type
=
str
(
x
.
dtype
)
str_x_type
=
str
(
x
.
dtype
)
if
str_x_type
.
startswith
(
'float'
)
or
str_x_type
in
int_dtypes
:
if
str_x_type
.
startswith
(
'float'
)
or
str_x_type
in
int_dtypes
:
return
argmax
(
-
x
,
axis
=
axis
)
return
argmax
(
-
x
,
axis
=
axis
)
...
@@ -2130,6 +2038,7 @@ def argmin(x, axis='DEFAULT'):
...
@@ -2130,6 +2038,7 @@ def argmin(x, axis='DEFAULT'):
#Be careful about unsigned integers, complex
#Be careful about unsigned integers, complex
raise
NotImplementedError
()
raise
NotImplementedError
()
@constructor
@constructor
def
smallest
(
*
args
):
def
smallest
(
*
args
):
"""Return the [elementwise] smallest of a variable number of arguments (like python's min)."""
"""Return the [elementwise] smallest of a variable number of arguments (like python's min)."""
...
...
theano/tensor/opt_uncanonicalize.py
浏览文件 @
90c3833e
...
@@ -57,7 +57,7 @@ class MaxAndArgmaxOptimizer(Optimizer):
...
@@ -57,7 +57,7 @@ class MaxAndArgmaxOptimizer(Optimizer):
if
len
(
node
.
outputs
[
1
]
.
clients
)
==
0
:
if
len
(
node
.
outputs
[
1
]
.
clients
)
==
0
:
try
:
try
:
axis
=
get_constant_value
(
node
.
inputs
[
1
])
axis
=
get_constant_value
(
node
.
inputs
[
1
])
except
ValueError
:
except
(
ValueError
,
TypeError
),
e
:
return
False
return
False
new
=
CAReduce
(
scal
.
maximum
,
axis
)(
node
.
inputs
[
0
])
new
=
CAReduce
(
scal
.
maximum
,
axis
)(
node
.
inputs
[
0
])
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
90c3833e
...
@@ -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,91 @@ class T_max_and_argmax(unittest.TestCase):
...
@@ -1456,108 +1457,91 @@ 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
)
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
-
1
)))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
-
1
)))
v
=
eval_outputs
(
max_and_argmax
(
n
,
-
1
)[
0
]
.
shape
)
assert
v
==
(
2
)
def
test2b
(
self
):
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
))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
None
,
None
),
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
0
)))
([
0
,
1
],
None
),
([
1
,
0
],
None
)]:
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
0
)))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
axis
))
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
np_axis
)))
assert
v
==
(
3
)
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
np_axis
)))
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
v_shape
=
eval_outputs
(
max_and_argmax
(
n
,
axis
)[
0
]
.
shape
)
assert
v
==
(
2
)
assert
tuple
(
v_shape
)
==
numpy
.
max
(
data
,
np_axis
)
.
shape
#currently not supported
#v = eval_outputs(max_and_argmax(n,[0,1])[0].shape)
#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
))
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
)
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
0
))
n
=
as_tensor_variable
(
data
)
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
None
,
None
),
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
([
0
,
1
,
2
],
None
),
([
1
,
2
,
0
],
None
)]:
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
1
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
axis
))
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
numpy
.
all
(
v
==
numpy
.
max
(
data
,
np_axis
)))
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
self
.
assertTrue
(
numpy
.
all
(
i
==
numpy
.
argmax
(
data
,
np_axis
)))
v
,
i
=
eval_outputs
(
max_and_argmax
(
n
,
2
))
v
=
eval_outputs
(
max_and_argmax
(
n
,
axis
)[
0
]
.
shape
)
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
assert
tuple
(
v
)
==
numpy
.
max
(
data
,
np_axis
)
.
shape
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
v
=
eval_outputs
(
max_and_argmax
(
n
,
0
)[
0
]
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
1
)[
0
]
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
max_and_argmax
(
n
,
2
)[
0
]
.
shape
)
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 +1549,39 @@ class T_max_and_argmax(unittest.TestCase):
...
@@ -1565,35 +1549,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 +1596,48 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1608,60 +1596,48 @@ 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
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
i
=
eval_outputs
(
fct
(
n
,
-
1
))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
None
,
None
),
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
-
1
)))
([
0
,
1
],
None
),
([
1
,
0
],
None
)]:
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
v
=
eval_outputs
(
fct
(
n
,
axis
))
assert
v
==
(
2
)
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
np_axis
)))
v_shape
=
eval_outputs
(
fct
(
n
,
axis
)
.
shape
)
def
test2b
(
self
):
assert
tuple
(
v_shape
)
==
nfct
(
data
,
np_axis
)
.
shape
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),(
argmin
,
numpy
.
argmin
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
i
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
data
,
0
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
v
==
(
3
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
v
==
(
2
)
#currently not supported
#v = eval_outputs(fct(n,[0,1]).shape)
#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 +1645,13 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1669,13 +1645,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 +1659,262 @@ class T_argmin_argmax(unittest.TestCase):
...
@@ -1683,286 +1659,262 @@ 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
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
)
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
n
=
as_tensor_variable
(
data
)
i
=
eval_outputs
(
fct
(
n
,
0
))
for
fct
,
nfct
in
[(
argmax
,
numpy
.
argmax
),
(
argmin
,
numpy
.
argmin
)]:
self
.
assertTrue
(
i
.
shape
==
(
3
,
4
))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
2
,
2
),
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
0
)))
(
None
,
None
),
([
0
,
1
,
2
],
None
),
i
=
eval_outputs
(
fct
(
n
,
1
))
([
1
,
0
,
2
],
None
)]:
self
.
assertTrue
(
i
.
shape
==
(
2
,
4
))
v
=
eval_outputs
(
fct
(
n
,
axis
))
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
1
)))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
np_axis
)))
i
=
eval_outputs
(
fct
(
n
,
2
))
v_shape
=
eval_outputs
(
fct
(
n
,
axis
)
.
shape
)
self
.
assertTrue
(
i
.
shape
==
(
2
,
3
))
assert
tuple
(
v_shape
)
==
nfct
(
data
,
np_axis
)
.
shape
self
.
assertTrue
(
numpy
.
all
(
i
==
nfct
(
n
.
value
,
2
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
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
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
n
=
as_tensor_variable
(
data
)
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
v
=
eval_outputs
(
fct
(
n
,
-
1
))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
None
,
None
),
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
-
1
)))
([
0
,
1
],
None
),
([
1
,
0
],
None
)]:
v
=
eval_outputs
(
fct
(
n
,
axis
))
v
=
eval_outputs
(
fct
(
n
,
-
1
)
.
shape
)
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
np_axis
)))
assert
v
==
(
2
)
v_shape
=
eval_outputs
(
fct
(
n
,
axis
)
.
shape
)
assert
tuple
(
v_shape
)
==
nfct
(
data
,
np_axis
)
.
shape
def
test2b
(
self
):
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),(
min
,
numpy
.
min
)]:
data
=
numpy
.
random
.
rand
(
2
,
3
)
n
=
as_tensor_variable
(
data
)
v
=
eval_outputs
(
fct
(
n
,
0
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
0
)))
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
v
==
(
3
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
v
==
(
2
)
v
=
eval_outputs
(
fct
(
n
,[
0
,
1
])
.
shape
)
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
)]:
# Test with 1 axis or all axis out of 3 dims
n
=
as_tensor_variable
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
)
v
=
eval_outputs
(
fct
(
n
,
0
))
n
=
as_tensor_variable
(
data
)
self
.
assertTrue
(
v
.
shape
==
(
3
,
4
))
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
0
)))
for
(
axis
,
np_axis
)
in
[(
-
1
,
-
1
),
(
0
,
0
),
(
1
,
1
),
(
2
,
2
),
v
=
eval_outputs
(
fct
(
n
,
1
))
(
None
,
None
),
([
0
,
1
,
2
],
None
),
self
.
assertTrue
(
v
.
shape
==
(
2
,
4
))
([
1
,
0
,
2
],
None
)]:
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
1
)))
v
=
eval_outputs
(
fct
(
n
,
axis
))
v
=
eval_outputs
(
fct
(
n
,
2
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
data
,
np_axis
)))
self
.
assertTrue
(
v
.
shape
==
(
2
,
3
))
v_shape
=
eval_outputs
(
fct
(
n
,
axis
)
.
shape
)
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
n
.
value
,
2
)))
assert
tuple
(
v_shape
)
==
nfct
(
data
,
np_axis
)
.
shape
v
=
eval_outputs
(
fct
(
n
,[
0
,
1
]))
self
.
assertTrue
(
v
.
shape
==
(
4
,))
def
test3b
(
self
):
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
1
),
0
)))
# Test with 2 axis out of 3 dims
v
=
eval_outputs
(
fct
(
n
,[
0
,
2
]))
data
=
numpy
.
random
.
rand
(
2
,
3
,
4
)
self
.
assertTrue
(
v
.
shape
==
(
3
,))
n
=
as_tensor_variable
(
data
)
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
0
)))
for
fct
,
nfct
in
[(
max
,
numpy
.
max
),
(
min
,
numpy
.
min
)]:
v
=
eval_outputs
(
fct
(
n
,[
1
,
2
]))
for
axis
in
[[
0
,
1
],
[
1
,
2
],
[
0
,
2
]]:
self
.
assertTrue
(
v
.
shape
==
(
2
,))
v
=
eval_outputs
(
fct
(
n
,
axis
))
self
.
assertTrue
(
numpy
.
all
(
v
==
nfct
(
nfct
(
n
.
value
,
2
),
1
)))
np_v
=
nfct
(
nfct
(
data
,
axis
[
1
]),
axis
[
0
])
v
=
eval_outputs
(
fct
(
n
,[
0
,
1
,
2
]))
self
.
assertTrue
(
numpy
.
all
(
v
==
np_v
))
self
.
assertTrue
(
v
.
shape
==
())
v_shape
=
eval_outputs
(
fct
(
n
,
axis
)
.
shape
)
assert
tuple
(
v_shape
)
==
np_v
.
shape
v
=
eval_outputs
(
fct
(
n
,
0
)
.
shape
)
assert
tuple
(
v
)
==
(
3
,
4
)
v
=
eval_outputs
(
fct
(
n
,
1
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
4
)
v
=
eval_outputs
(
fct
(
n
,
2
)
.
shape
)
assert
tuple
(
v
)
==
(
2
,
3
)
v
=
eval_outputs
(
fct
(
n
,[
0
,
1
])
.
shape
)
self
.
assertTrue
(
v
==
(
4
,))
v
=
eval_outputs
(
fct
(
n
,[
0
,
2
])
.
shape
)
self
.
assertTrue
(
v
==
(
3
,))
v
=
eval_outputs
(
fct
(
n
,[
1
,
2
])
.
shape
)
self
.
assertTrue
(
v
==
(
2
,))
v
=
eval_outputs
(
fct
(
n
,[
0
,
1
,
2
])
.
shape
)
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
,
...
...
theano/tensor/tests/test_opt_uncanonicalize.py
浏览文件 @
90c3833e
...
@@ -13,91 +13,95 @@ from theano.tests import unittest_tools as utt
...
@@ -13,91 +13,95 @@ from theano.tests import unittest_tools as utt
class
T_max_and_argmax
(
unittest
.
TestCase
):
class
T_max_and_argmax
(
unittest
.
TestCase
):
def
test_optimization
(
self
):
def
test_optimization
(
self
):
#If we use only the max output, we should replace this op with a faster one.
#If we use only the max output, we should replace this op with
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
.
including
(
'canonicalize'
,
'fast_run'
)
#a faster one.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
.
including
(
'canonicalize'
,
'fast_run'
)
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
for
axis
in
[
0
,
1
,
-
1
]:
n
=
tensor
.
matrix
()
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
n
=
tensor
.
matrix
()
f
=
function
([
n
],
tensor
.
max_and_argmax
(
n
,
0
)[
0
],
mode
=
mode
)
f
=
function
([
n
],
tensor
.
max_and_argmax
(
n
,
axis
)[
0
],
mode
=
mode
)
topo
=
f
.
maker
.
env
.
toposort
()
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
f
=
function
([
n
],
tensor
.
max_and_argmax
(
n
,
0
),
mode
=
mode
)
f
=
function
([
n
],
tensor
.
max_and_argmax
(
n
,
axis
),
mode
=
mode
)
topo
=
f
.
maker
.
env
.
toposort
()
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
tensor
.
MaxAndArgmax
)
assert
isinstance
(
topo
[
0
]
.
op
,
tensor
.
MaxAndArgmax
)
class
T_min_max
(
unittest
.
TestCase
):
class
T_min_max
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
.
including
(
'canonicalize'
,
'fast_run'
)
self
.
mode
=
theano
.
compile
.
mode
.
get_default_mode
()
.
including
(
'canonicalize'
,
'fast_run'
)
def
test_optimization_max
(
self
):
def
test_optimization_max
(
self
):
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
n
=
tensor
.
matrix
()
n
=
tensor
.
matrix
()
f
=
function
([
n
],
tensor
.
max
(
n
,
0
),
mode
=
self
.
mode
)
f
or
axis
in
[
0
,
1
,
-
1
]:
topo
=
f
.
maker
.
env
.
toposort
(
)
f
=
function
([
n
],
tensor
.
max
(
n
,
axis
),
mode
=
self
.
mode
)
assert
len
(
topo
)
==
1
topo
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
assert
len
(
topo
)
==
1
f
(
data
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
f
(
data
)
f
=
function
([
n
],
tensor
.
max
(
-
n
,
0
),
mode
=
self
.
mode
)
f
=
function
([
n
],
tensor
.
max
(
-
n
,
axis
),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
env
.
toposort
()
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
2
assert
len
(
topo
)
==
2
assert
isinstance
(
topo
[
0
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
0
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scalar
.
Neg
)
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scalar
.
Neg
)
assert
isinstance
(
topo
[
1
]
.
op
,
CAReduce
)
assert
isinstance
(
topo
[
1
]
.
op
,
CAReduce
)
f
(
data
)
f
(
data
)
f
=
function
([
n
],
-
tensor
.
max
(
n
,
0
),
mode
=
self
.
mode
)
f
=
function
([
n
],
-
tensor
.
max
(
n
,
axis
),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
env
.
toposort
()
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
2
assert
len
(
topo
)
==
2
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
assert
isinstance
(
topo
[
1
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
1
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
scalar
.
Neg
)
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
scalar
.
Neg
)
f
(
data
)
f
(
data
)
f
=
function
([
n
],
-
tensor
.
max
(
-
n
,
0
),
mode
=
self
.
mode
)
f
=
function
([
n
],
-
tensor
.
max
(
-
n
,
axis
),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
env
.
toposort
()
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
1
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
#
min
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
#
min
f
(
data
)
f
(
data
)
def
test_optimization_min
(
self
):
def
test_optimization_min
(
self
):
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
data
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
2
,
3
),
dtype
=
config
.
floatX
)
n
=
tensor
.
matrix
()
n
=
tensor
.
matrix
()
f
=
function
([
n
],
tensor
.
min
(
n
,
0
),
mode
=
self
.
mode
)
f
or
axis
in
[
0
,
1
,
-
1
]:
topo
=
f
.
maker
.
env
.
toposort
(
)
f
=
function
([
n
],
tensor
.
min
(
n
,
axis
),
mode
=
self
.
mode
)
assert
len
(
topo
)
==
1
topo
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
assert
len
(
topo
)
==
1
f
(
data
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
f
(
data
)
#test variant with neg to make sure we optimize correctly
f
=
function
([
n
],
tensor
.
min
(
-
n
,
0
),
mode
=
self
.
mode
)
#test variant with neg to make sure we optimize correctly
topo
=
f
.
maker
.
env
.
toposort
(
)
f
=
function
([
n
],
tensor
.
min
(
-
n
,
axis
),
mode
=
self
.
mode
)
assert
len
(
topo
)
==
2
topo
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
#max
assert
len
(
topo
)
==
2
assert
isinstance
(
topo
[
1
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
# max
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
scalar
.
Neg
)
assert
isinstance
(
topo
[
1
]
.
op
,
Elemwise
)
f
(
data
)
assert
isinstance
(
topo
[
1
]
.
op
.
scalar_op
,
scalar
.
Neg
)
f
(
data
)
f
=
function
([
n
],
-
tensor
.
min
(
n
,
0
),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
env
.
toposort
(
)
f
=
function
([
n
],
-
tensor
.
min
(
n
,
axis
),
mode
=
self
.
mode
)
assert
len
(
topo
)
==
2
topo
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
topo
[
0
]
.
op
,
Elemwise
)
assert
len
(
topo
)
==
2
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scalar
.
Neg
)
assert
isinstance
(
topo
[
0
]
.
op
,
Elemwise
)
assert
isinstance
(
topo
[
1
]
.
op
,
CAReduce
)
#max
assert
isinstance
(
topo
[
0
]
.
op
.
scalar_op
,
scalar
.
Neg
)
f
(
data
)
assert
isinstance
(
topo
[
1
]
.
op
,
CAReduce
)
# max
f
(
data
)
f
=
function
([
n
],
-
tensor
.
min
(
-
n
,
0
),
mode
=
self
.
mode
)
topo
=
f
.
maker
.
env
.
toposort
(
)
f
=
function
([
n
],
-
tensor
.
min
(
-
n
,
axis
),
mode
=
self
.
mode
)
assert
len
(
topo
)
==
1
topo
=
f
.
maker
.
env
.
toposort
()
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
#max
assert
len
(
topo
)
==
1
f
(
data
)
assert
isinstance
(
topo
[
0
]
.
op
,
CAReduce
)
# max
f
(
data
)
theano/tensor/tests/test_sharedvar.py
浏览文件 @
90c3833e
...
@@ -350,27 +350,6 @@ def makeSharedTester(shared_constructor_,
...
@@ -350,27 +350,6 @@ def makeSharedTester(shared_constructor_,
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
x_shared
.
get_value
(
borrow
=
True
)
x_shared
.
get_value
(
borrow
=
True
)
# Test by .value
# As we know that .value is deprecated, we filter out the warning
warnings
.
filterwarnings
(
action
=
'ignore'
,
message
=
'The .value property of shared variables is deprecated.'
)
try
:
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
value
=
nd
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_value_inplace
finally
:
# Restore the default behavior.
# TODO There is a cleaner way to do this in Python 2.6, once
# Theano drops support of Python 2.4 and 2.5.
warnings
.
filterwarnings
(
action
=
'default'
,
message
=
'The .value property of shared variables is deprecated.'
)
# Test by set_value with borrow=False
# Test by set_value with borrow=False
nd
+=
1
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
old_data
=
x_shared
.
container
.
storage
[
0
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论