Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5002d0fe
提交
5002d0fe
authored
9月 21, 2015
作者:
Harm de Vries
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support for multiple axes in MaxAndArgMaxOp
上级
e617dc50
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
47 行删除
+60
-47
basic.py
theano/tensor/basic.py
+54
-47
test_basic.py
theano/tensor/tests/test_basic.py
+6
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
5002d0fe
...
@@ -1286,28 +1286,19 @@ class MaxAndArgmax(Op):
...
@@ -1286,28 +1286,19 @@ class MaxAndArgmax(Op):
def
make_node
(
self
,
x
,
axis
=
None
):
def
make_node
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor_variable
(
x
)
x
=
_as_tensor_variable
(
x
)
if
isinstance
(
axis
,
(
tuple
,
list
,
numpy
.
ndarray
)):
if
isinstance
(
axis
,
(
tuple
,
list
)):
# List of axes: make them non-negative, and sort them
axis
=
[
int
(
a
)
for
a
in
axis
]
axis
=
[
int
(
a
)
for
a
in
axis
]
if
len
(
axis
)
!=
1
:
axis
=
list
(
axis
)
#if axis == list(range(-x.type.ndim, 0, 1)):
for
idx
in
xrange
(
len
(
axis
)):
#axis = list(range(x.type.ndim))
if
axis
[
idx
]
<
0
:
#assert axis == list(range(x.type.ndim)), (
axis
[
idx
]
+=
x
.
type
.
ndim
#"MaxAndArgmax does not support multiple"
axis
.
sort
()
#" axes. the max fct supports it. Got %s" % axis)
if
axis
==
list
(
range
(
-
x
.
type
.
ndim
,
0
,
1
)):
if
axis
==
list
(
range
(
x
.
type
.
ndim
)):
axis
=
list
(
range
(
x
.
type
.
ndim
))
assert
axis
==
list
(
range
(
x
.
type
.
ndim
)),
(
"MaxAndArgmax does not support multiple"
" axes. the max fct supports it. Got
%
s"
%
axis
)
axis
=
None
axis
=
None
else
:
elif
isinstance
(
axis
,
(
int
,
numpy
.
integer
)):
axis
=
axis
[
0
]
axis
=
[
int
(
axis
)]
if
isinstance
(
axis
,
(
int
,
numpy
.
integer
)):
axis
=
int
(
axis
)
elif
isinstance
(
axis
,
numpy
.
ndarray
)
and
axis
.
ndim
==
0
:
axis
=
int
(
axis
)
elif
isinstance
(
axis
,
Variable
):
elif
isinstance
(
axis
,
Variable
):
if
NoneConst
.
equals
(
axis
):
if
NoneConst
.
equals
(
axis
):
axis
=
None
axis
=
None
...
@@ -1317,29 +1308,37 @@ class MaxAndArgmax(Op):
...
@@ -1317,29 +1308,37 @@ class MaxAndArgmax(Op):
else
:
else
:
assert
(
axis
.
dtype
.
startswith
(
"int"
)
or
assert
(
axis
.
dtype
.
startswith
(
"int"
)
or
axis
.
dtype
.
startswith
(
"uint"
))
axis
.
dtype
.
startswith
(
"uint"
))
axis
=
int
(
axis
.
data
)
if
isinstance
(
axis
.
data
,
(
int
,
numpy
.
integer
))
or
\
# we make the axis all positive to make the infer_shape work
(
isinstance
(
axis
.
data
,
numpy
.
ndarray
)
and
axis
.
data
.
ndim
==
0
):
# with negative axis
axis
=
[
int
(
axis
.
data
)]
if
x
.
type
.
ndim
>
0
and
axis
is
not
None
:
elif
isinstance
(
axis
.
data
,
(
list
,
numpy
.
ndarray
)):
if
axis
<
0
:
print
axis
.
data
if
-
axis
>
x
.
type
.
ndim
:
axis
=
[
int
(
i
)
for
i
in
axis
.
data
]
raise
ValueError
(
'axis out of range'
)
axis
=
x
.
type
.
ndim
+
axis
# Make axis entries non-negative, and sort them
# Verify that the axis is valid.
if
isinstance
(
axis
,
list
):
for
idx
in
xrange
(
len
(
axis
)):
if
axis
[
idx
]
<
0
:
axis
[
idx
]
+=
x
.
type
.
ndim
axis
.
sort
()
# Verify that axes are valid
all_axes
=
set
()
all_axes
=
set
()
if
axis
is
not
None
:
if
isinstance
(
axis
,
list
):
if
axis
<
0
or
axis
>=
x
.
type
.
ndim
:
for
ax
in
axis
:
raise
ValueError
(
if
ax
<
0
or
ax
>=
x
.
type
.
ndim
:
'Invalid axis:
%
s (the number of dimensions of the '
raise
ValueError
(
'input is:
%
s)'
%
(
axis
,
x
.
type
.
ndim
))
'Invalid axis:
%
s (the number of dimensions of the '
all_axes
.
add
(
axis
)
'input is:
%
s)'
%
(
ax
,
x
.
type
.
ndim
))
all_axes
.
add
(
ax
)
else
:
else
:
all_axes
=
list
(
range
(
x
.
ndim
))
all_axes
=
list
(
range
(
x
.
ndim
))
if
axis
is
None
:
if
axis
is
None
or
axis
==
list
(
range
(
x
.
type
.
ndim
)):
axis
=
NoneConst
.
clone
()
axis
=
NoneConst
.
clone
()
else
:
else
:
axis
=
_as_tensor_variable
(
axis
)
axis
=
_as_tensor_variable
(
axis
)
assert
axis
.
ndim
==
0
#
assert axis.ndim == 0
inputs
=
[
x
,
axis
]
inputs
=
[
x
,
axis
]
# We keep the original broadcastable flags for dimensions on which
# We keep the original broadcastable flags for dimensions on which
# we do not perform the max / argmax.
# we do not perform the max / argmax.
...
@@ -1350,21 +1349,28 @@ class MaxAndArgmax(Op):
...
@@ -1350,21 +1349,28 @@ class MaxAndArgmax(Op):
return
Apply
(
self
,
inputs
,
outputs
)
return
Apply
(
self
,
inputs
,
outputs
)
def
perform
(
self
,
node
,
inp
,
outs
):
def
perform
(
self
,
node
,
inp
,
outs
):
x
,
ax
i
s
=
inp
x
,
ax
e
s
=
inp
max
,
max_idx
=
outs
max
,
max_idx
=
outs
max
[
0
]
=
theano
.
_asarray
(
numpy
.
max
(
x
,
axis
),
max
[
0
]
=
theano
.
_asarray
(
numpy
.
max
(
x
,
tuple
(
axes
)
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
dtype
=
node
.
outputs
[
0
]
.
dtype
)
max_idx
[
0
]
=
theano
.
_asarray
(
numpy
.
argmax
(
x
,
axis
),
dtype
=
'int64'
)
# Numpy does not support multiple axes for argmax
# Work around,
keep_axes
=
numpy
.
array
([
i
for
i
in
range
(
x
.
ndim
)
if
i
not
in
axes
])
# Not reduced axes in front
transposed_x
=
numpy
.
transpose
(
x
,
numpy
.
concatenate
((
keep_axes
,
axes
)))
reshaped_x
=
x
.
reshape
(
transposed_x
.
shape
[:
len
(
keep_axes
)]
+
(
-
1
,))
max_idx
[
0
]
=
theano
.
_asarray
(
numpy
.
argmax
(
reshaped_x
,
-
1
),
dtype
=
'int64'
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
axis
=
inp
x
,
axis
=
inp
max
,
argmax
=
out
max
,
argmax
=
out
fail
=
sub
[
"fail"
]
fail
=
sub
[
"fail"
]
if
NoneConst
.
equals
(
node
.
inputs
[
1
]):
if
NoneConst
.
equals
(
node
.
inputs
[
1
]):
axis_code
=
"axis = NPY_MAXDIMS;"
axis_code
=
"axis = NPY_MAXDIMS;"
else
:
else
:
assert
node
.
inputs
[
1
]
.
ndim
==
0
assert
node
.
inputs
[
1
]
.
ndim
==
1
# Fall back to perform() if there are multiple axes
if
len
(
node
.
inputs
[
1
]
.
data
)
>
1
:
raise
NotImplementedError
()
axis_code
=
"""
axis_code
=
"""
axis = ((dtype_
%(axis)
s*)PyArray_DATA(
%(axis)
s))[0];
axis = ((dtype_
%(axis)
s*)PyArray_DATA(
%(axis)
s))[0];
if(axis > PyArray_NDIM(
%(x)
s)-1 || axis < -PyArray_NDIM(
%(x)
s)){
if(axis > PyArray_NDIM(
%(x)
s)-1 || axis < -PyArray_NDIM(
%(x)
s)){
...
@@ -1420,12 +1426,12 @@ class MaxAndArgmax(Op):
...
@@ -1420,12 +1426,12 @@ class MaxAndArgmax(Op):
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
node
.
inputs
[
1
]
.
data
is
None
:
if
axis
.
data
is
None
:
return
[(),
()]
return
[(),
()]
rval
=
tuple
([
ishape
[
i
]
for
(
i
,
b
)
in
enumerate
(
rval
=
tuple
([
ishape
[
i
]
for
(
i
,
b
)
in
enumerate
(
node
.
inputs
[
0
]
.
type
.
broadcastable
)
if
i
!=
axis
.
data
])
node
.
inputs
[
0
]
.
type
.
broadcastable
)
if
i
not
in
axis
.
data
])
return
[
rval
,
rval
]
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
]
...
@@ -1484,6 +1490,7 @@ class MaxAndArgmax(Op):
...
@@ -1484,6 +1490,7 @@ class MaxAndArgmax(Op):
else
:
else
:
axis_
=
axis
axis_
=
axis
xmax
=
max
(
x
,
axis_
)
xmax
=
max
(
x
,
axis_
)
# Raise the g_max and xmax to the same number of dim as the input.
# Raise the g_max and xmax to the same number of dim as the input.
pattern
=
[]
pattern
=
[]
...
@@ -1492,7 +1499,7 @@ class MaxAndArgmax(Op):
...
@@ -1492,7 +1499,7 @@ class MaxAndArgmax(Op):
# We are taking the max/argmax over all dimensions.
# We are taking the max/argmax over all dimensions.
axis
=
None
axis
=
None
for
i
in
xrange
(
x
.
ndim
):
for
i
in
xrange
(
x
.
ndim
):
if
axis
is
None
or
i
==
axis
.
data
:
if
axis
is
None
or
i
in
axis
.
data
:
pattern
.
append
(
'x'
)
pattern
.
append
(
'x'
)
else
:
else
:
pattern
.
append
(
out_dim
)
pattern
.
append
(
out_dim
)
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
5002d0fe
...
@@ -2963,6 +2963,12 @@ class T_max_and_argmax(unittest.TestCase):
...
@@ -2963,6 +2963,12 @@ class T_max_and_argmax(unittest.TestCase):
x
=
tensor
.
matrix
()
.
dimshuffle
(
'x'
,
0
,
'x'
,
1
,
'x'
)
x
=
tensor
.
matrix
()
.
dimshuffle
(
'x'
,
0
,
'x'
,
1
,
'x'
)
y
=
x
.
max
(
axis
=
1
)
y
=
x
.
max
(
axis
=
1
)
assert
y
.
type
.
broadcastable
==
(
True
,
True
,
False
,
True
)
assert
y
.
type
.
broadcastable
==
(
True
,
True
,
False
,
True
)
def
test_multiple_axes
(
self
):
data
=
as_tensor_variable
(
numpy
.
arange
(
24
)
.
reshape
(
3
,
2
,
4
))
v
,
i
=
eval_outputs
(
max_and_argmax
(
data
,
[
1
,
-
1
]))
assert
numpy
.
all
(
v
==
numpy
.
array
([
7
,
15
,
23
]))
assert
numpy
.
all
(
i
==
numpy
.
array
([
7
,
7
,
7
]))
class
T_argmin_argmax
(
unittest
.
TestCase
):
class
T_argmin_argmax
(
unittest
.
TestCase
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论