Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
dada6d72
提交
dada6d72
authored
5月 12, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
changed Argmax for MaxAndArgmax, argmax now only returns a single result
上级
75247c10
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
30 行增加
和
17 行删除
+30
-17
_test_tensor.py
_test_tensor.py
+13
-13
tensor.py
tensor.py
+17
-4
没有找到文件。
_test_tensor.py
浏览文件 @
dada6d72
...
...
@@ -625,60 +625,60 @@ class T_Cast(unittest.TestCase):
b
=
f
(
a
)
self
.
failUnless
(
numpy
.
all
(
b
==
numpy
.
arange
(
10
,
dtype
=
type2
)))
class
T_argmax
(
unittest
.
TestCase
):
class
T_
max_and_
argmax
(
unittest
.
TestCase
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
123784
)
Argmax
.
debug
=
0
MaxAnd
Argmax
.
debug
=
0
def
test0
(
self
):
n
=
as_tensor
(
5.0
)
v
,
i
=
eval_outputs
(
argmax
(
n
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
))
self
.
failUnless
(
v
==
5.0
)
self
.
failUnless
(
i
==
0
)
def
test1
(
self
):
n
=
as_tensor
([
1
,
2
,
3
,
2
,
-
6
])
v
,
i
=
eval_outputs
(
argmax
(
n
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
))
self
.
failUnless
(
v
==
3
)
self
.
failUnless
(
i
==
2
)
def
test2
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
))
self
.
failUnless
(
numpy
.
all
(
i
==
[
0
,
1
]))
def
test2b
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
0
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
0
))
self
.
failUnless
(
numpy
.
all
(
i
==
[
0
,
1
,
1
]))
def
test2_invalid
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
))
try
:
eval_outputs
(
argmax
(
n
,
3
))
eval_outputs
(
max_and_
argmax
(
n
,
3
))
except
ValueError
,
e
:
return
self
.
fail
()
def
test2_invalid_neg
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
))
try
:
eval_outputs
(
argmax
(
n
,
-
3
))
eval_outputs
(
max_and_
argmax
(
n
,
-
3
))
except
ValueError
,
e
:
return
self
.
fail
()
def
test2_valid_neg
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
-
1
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
-
1
))
self
.
failUnless
(
v
.
shape
==
(
2
,))
v
,
i
=
eval_outputs
(
argmax
(
n
,
-
2
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
-
2
))
self
.
failUnless
(
v
.
shape
==
(
3
,))
def
test3
(
self
):
n
=
as_tensor
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
0
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
0
))
self
.
failUnless
(
v
.
shape
==
(
3
,
4
))
self
.
failUnless
(
i
.
shape
==
(
3
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
1
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
1
))
self
.
failUnless
(
v
.
shape
==
(
2
,
4
))
self
.
failUnless
(
i
.
shape
==
(
2
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
2
))
v
,
i
=
eval_outputs
(
max_and_
argmax
(
n
,
2
))
self
.
failUnless
(
v
.
shape
==
(
2
,
3
))
self
.
failUnless
(
i
.
shape
==
(
2
,
3
))
...
...
tensor.py
浏览文件 @
dada6d72
...
...
@@ -484,11 +484,12 @@ class Shape(Op):
return
[
None
]
shape
=
Shape
()
class
Argmax
(
Op
):
class
MaxAnd
Argmax
(
Op
):
"""Calculate the max and argmax over a given axis"""
nin
=
2
# tensor, axis
nout
=
2
# max val, max idx
E_axis
=
'invalid axis'
def
make_node
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor
(
x
)
if
axis
is
None
:
...
...
@@ -502,17 +503,29 @@ class Argmax(Op):
def
perform
(
self
,
node
,
(
x
,
axis
),
(
max
,
max_idx
)):
max
[
0
]
=
numpy
.
max
(
x
,
axis
)
max_idx
[
0
]
=
numpy
.
argmax
(
x
,
axis
)
argmax
=
Argmax
()
max_and_argmax
=
MaxAndArgmax
()
def
max
(
x
,
axis
=
None
):
"""Return indexes of maximum elements obtained by iterating over given axis
Default axis is the last one.
"""
# In python (using MaxAndArgmax.perform()) this leads to an wasteful
# implementation that goes through the data twice instead of once
# but when Argmax.c_impl() is in place, it should be fine.
return
max_and_argmax
(
x
,
axis
)[
0
]
def
argmax
(
x
,
axis
=
None
):
"""Return maximum elements obtained by iterating over given axis
Default axis is the last one.
"""
# In python (using Argmax.perform()) this leads to an wasteful
# In python (using
MaxAnd
Argmax.perform()) this leads to an wasteful
# implementation that goes through the data twice instead of once
# but when Argmax.c_impl() is in place, it should be fine.
return
argmax
(
x
,
axis
)[
0
]
return
max_and_argmax
(
x
,
axis
)[
1
]
##########################
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论