Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
eea1ba8e
提交
eea1ba8e
authored
3月 19, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added Argmax Op
上级
4dedf419
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
111 行增加
和
81 行删除
+111
-81
_test_tensor.py
_test_tensor.py
+57
-0
tensor.py
tensor.py
+54
-0
tensor_ops.py
tensor_ops.py
+0
-81
没有找到文件。
_test_tensor.py
浏览文件 @
eea1ba8e
...
@@ -67,6 +67,63 @@ def check_eq2_c(self, inputs, output, args_in, arg_out):
...
@@ -67,6 +67,63 @@ def check_eq2_c(self, inputs, output, args_in, arg_out):
val
=
fn
(
*
args_in
)
val
=
fn
(
*
args_in
)
self
.
failUnless
(
numpy
.
all
(
val
==
arg_out
),
(
val
,
arg_out
))
self
.
failUnless
(
numpy
.
all
(
val
==
arg_out
),
(
val
,
arg_out
))
class
T_argmax
(
unittest
.
TestCase
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
123784
)
Argmax
.
debug
=
0
def
test0
(
self
):
n
=
tinit
(
5.0
)
v
,
i
=
eval_outputs
(
argmax
(
n
))
self
.
failUnless
(
v
==
5.0
)
self
.
failUnless
(
i
==
0
)
def
test1
(
self
):
n
=
tinit
([
1
,
2
,
3
,
2
,
-
6
])
v
,
i
=
eval_outputs
(
argmax
(
n
))
self
.
failUnless
(
v
==
3
)
self
.
failUnless
(
i
==
2
)
def
test2
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
))
self
.
failUnless
(
numpy
.
all
(
i
==
[
0
,
1
]))
def
test2b
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=
0
))
self
.
failUnless
(
numpy
.
all
(
i
==
[
0
,
1
,
1
]))
def
test2_invalid
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
))
try
:
eval_outputs
(
argmax
(
n
,
axis
=
3
))
self
.
fail
()
except
ValueError
,
e
:
return
def
test2_invalid_neg
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
))
try
:
eval_outputs
(
argmax
(
n
,
axis
=-
3
))
self
.
fail
()
except
ValueError
,
e
:
return
def
test2_valid_neg
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=-
1
))
self
.
failUnless
(
v
.
shape
==
(
2
,))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=-
2
))
self
.
failUnless
(
v
.
shape
==
(
3
,))
def
test3
(
self
):
n
=
tinit
(
numpy
.
random
.
rand
(
2
,
3
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=
0
))
self
.
failUnless
(
v
.
shape
==
(
3
,
4
))
self
.
failUnless
(
i
.
shape
==
(
3
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=
1
))
self
.
failUnless
(
v
.
shape
==
(
2
,
4
))
self
.
failUnless
(
i
.
shape
==
(
2
,
4
))
v
,
i
=
eval_outputs
(
argmax
(
n
,
axis
=
2
))
self
.
failUnless
(
v
.
shape
==
(
2
,
3
))
self
.
failUnless
(
i
.
shape
==
(
2
,
3
))
class
T_transpose
(
unittest
.
TestCase
):
class
T_transpose
(
unittest
.
TestCase
):
def
test0
(
self
):
def
test0
(
self
):
...
...
tensor.py
浏览文件 @
eea1ba8e
...
@@ -281,12 +281,44 @@ class Abs(_Elemwise):
...
@@ -281,12 +281,44 @@ class Abs(_Elemwise):
return
"
%(z)
s_i = abs(
%(x)
s_i);"
return
"
%(z)
s_i = abs(
%(x)
s_i);"
#Constructor not necessary because builtin abs() does this
#Constructor not necessary because builtin abs() does this
class
Argmax
(
Op
):
nin
=
2
# tensor, axis
nout
=
2
# max val, max idx
E_axis
=
'invalid axis'
debug
=
0
def
__init__
(
self
,
x
,
axis
=
None
):
x
=
_as_tensor
(
x
)
if
axis
is
None
:
axis
=
len
(
x
.
broadcastable
)
-
1
axis
=
_as_tensor
(
axis
)
self
.
inputs
=
[
x
,
axis
]
broadcastable
=
[
0
]
*
(
len
(
x
.
broadcastable
)
-
1
)
self
.
outputs
=
[
Tensor
(
x
.
dtype
,
broadcastable
),
Tensor
(
axis
.
dtype
,
broadcastable
)]
def
perform
(
self
):
axis
=
self
.
inputs
[
1
]
.
data
x
=
self
.
inputs
[
0
]
.
data
self
.
outputs
[
0
]
.
data
=
numpy
.
max
(
x
,
axis
)
self
.
outputs
[
1
]
.
data
=
numpy
.
argmax
(
x
,
axis
)
argmax
=
_constructor
(
Argmax
)
def
max
(
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
# 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
]
class
Exp
(
_Elemwise
):
class
Exp
(
_Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
exp
(
x
)
def
impl
(
self
,
x
):
return
numpy
.
exp
(
x
)
def
grad
(
self
,
x
,
gz
):
return
gz
*
exp
(
x
)
def
grad
(
self
,
x
,
gz
):
return
gz
*
exp
(
x
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = exp(x_i);"
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = exp(x_i);"
exp
=
_constructor
(
Exp
)
exp
=
_constructor
(
Exp
)
class
Neg
(
_Elemwise
):
class
Neg
(
_Elemwise
):
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
-
x
return
-
x
...
@@ -302,6 +334,12 @@ class Log(_Elemwise):
...
@@ -302,6 +334,12 @@ class Log(_Elemwise):
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = log(x_i);"
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = log(x_i);"
log
=
_constructor
(
Log
)
log
=
_constructor
(
Log
)
class
Log2
(
_Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
log2
(
x
)
def
grad
(
self
,
x
,
gz
):
return
gz
/
(
x
*
numpy
.
log
(
2.0
))
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"
%(z)
s_i = log2(
%(x)
s_i);"
log2
=
_constructor
(
Log2
)
class
Sgn
(
_Elemwise
):
class
Sgn
(
_Elemwise
):
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
numpy
.
abs
(
x
)
/
x
return
numpy
.
abs
(
x
)
/
x
...
@@ -311,6 +349,18 @@ class Sgn(_Elemwise):
...
@@ -311,6 +349,18 @@ class Sgn(_Elemwise):
return
"
%(z)
s_i =
%(x)
s_i/abs(
%(x)
s_i);"
# TODO: C use copysign
return
"
%(z)
s_i =
%(x)
s_i/abs(
%(x)
s_i);"
# TODO: C use copysign
sgn
=
_constructor
(
Sgn
)
sgn
=
_constructor
(
Sgn
)
class
Sqr
(
_Elemwise
):
def
impl
(
self
,
x
):
return
x
*
x
def
grad
(
self
,
x
,
gz
):
return
2.0
*
x
*
gz
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"
%(z)
s_i =
%(x)
s_i *
%(x)
s_i;"
sqr
=
_constructor
(
Sqr
)
class
Sqrt
(
_Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
sqrt
(
x
)
def
grad
(
self
,
x
,
gz
):
return
0.5
*
gz
/
sqrt
(
x
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"
%(z)
s_i = sqrt(
%(x)
s_i);"
sqrt
=
_constructor
(
Sqrt
)
class
Sum
(
_Elemwise
):
class
Sum
(
_Elemwise
):
def
impl
(
self
,
x
):
def
impl
(
self
,
x
):
return
numpy
.
sum
(
x
)
return
numpy
.
sum
(
x
)
...
@@ -334,6 +384,10 @@ class Fill(_Elemwise):
...
@@ -334,6 +384,10 @@ class Fill(_Elemwise):
def
c_foreach
(
self
,
(
model_i
,
value
),
(
z_i
,
)):
def
c_foreach
(
self
,
(
model_i
,
value
),
(
z_i
,
)):
return
"
%(z)
s_i =
%(value)
s0;"
return
"
%(z)
s_i =
%(value)
s0;"
fill
=
_constructor
(
Fill
)
fill
=
_constructor
(
Fill
)
def
ones_like
(
model
):
return
fill
(
model
,
1.0
)
def
zeros_like
(
model
):
return
fill
(
model
,
0.0
)
class
TensorCopy
(
_Elemwise
):
class
TensorCopy
(
_Elemwise
):
...
...
tensor_ops.py
浏览文件 @
eea1ba8e
...
@@ -64,87 +64,6 @@ class Dot(TensorOp):
...
@@ -64,87 +64,6 @@ class Dot(TensorOp):
class
NegInplace
(
Neg
.
inplace_version
()):
def
impl
(
self
,
x
):
x
*=
-
1
return
x
class
InvElemwise
(
Elemwise
):
def
impl
(
self
,
x
):
return
1
/
x
def
grad
(
self
,
x
,
gz
):
return
-
gz
/
(
x
*
x
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = 1 / x_i;"
class
InvElemwiseInplace
(
InvElemwise
.
inplace_version
()):
def
impl
(
self
,
x
):
x
[:]
=
1
/
x
return
x
class
Log2
(
Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
log2
(
x
)
def
grad
(
self
,
x
,
gz
):
return
gz
/
(
x
*
numpy
.
log
(
2
))
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = log2(x_i);"
class
Twice
(
Elemwise
):
def
impl
(
self
,
x
):
return
2.0
*
x
def
grad
(
self
,
x
,
gz
):
return
scale
(
gz
,
2.0
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
"z_i = x_i + x_i;"
class
TwiceInplace
(
Twice
.
inplace_version
()):
def
impl
(
self
,
x
):
x
*=
2.0
return
x
class
Sqr
(
Elemwise
):
def
impl
(
self
,
x
):
return
x
*
x
def
grad
(
self
,
x
,
gz
):
return
scale
(
mul_elemwise
(
x
,
gz
),
2.0
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = x_i * x_i;"
class
SqrInplace
(
Sqr
.
inplace_version
()):
def
impl
(
x
):
x
*=
x
return
x
class
Sqrt
(
Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
sqrt
(
x
)
def
grad
(
self
,
x
,
gz
):
return
scale
(
div
(
gz
,
sqrt
(
x
)),
0.5
)
def
c_foreach
(
self
,
(
x_i
,
),
(
z_i
,
)):
return
"z_i = pow(x_i, 0.5);"
class
SqrtInplace
(
Sqrt
.
inplace_version
()):
def
impl
(
self
,
x
):
x
**=
0.5
return
x
class
OnesLike
(
Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
ones_like
(
x
)
def
grad
(
self
,
x
,
gz
):
return
None
class
ZerosLike
(
Elemwise
):
def
impl
(
self
,
x
):
return
numpy
.
zeros_like
(
x
)
def
grad
(
self
,
x
,
gz
):
return
None
class
Min
:
class
Min
:
pass
pass
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论