Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
36c317bd
提交
36c317bd
authored
9月 24, 2008
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
converting tensor ops to _scal_elemwise
上级
7ab5fb48
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
85 行增加
和
35 行删除
+85
-35
_test_tensor.py
_test_tensor.py
+19
-8
scalar.py
scalar.py
+1
-1
tensor.py
tensor.py
+65
-26
没有找到文件。
_test_tensor.py
浏览文件 @
36c317bd
...
...
@@ -21,9 +21,16 @@ def _numpy_checker(x, y):
Used in DualLinker to compare C version with Python version.
"""
x
,
y
=
x
[
0
],
y
[
0
]
if
x
.
dtype
!=
y
.
dtype
or
x
.
shape
!=
y
.
shape
or
numpy
.
any
(
abs
(
x
-
y
)
>
1e-10
):
if
x
.
dtype
!=
y
.
dtype
or
x
.
shape
!=
y
.
shape
or
numpy
.
any
(
numpy
.
abs
(
x
-
y
)
>
1e-10
):
raise
Exception
(
"Output mismatch."
,
{
'performlinker'
:
x
,
'clinker'
:
y
})
def
safe_make_node
(
op
,
*
inputs
):
"""Emulate the behaviour of make_node when op is a function instead of an Op instance."""
node
=
op
(
*
inputs
)
if
isinstance
(
node
,
list
):
return
node
[
0
]
.
owner
else
:
return
node
.
owner
def
make_tester
(
name
,
op
,
expected
,
checks
=
{},
good
=
{},
bad_build
=
{},
bad_runtime
=
{},
grad
=
{}):
if
grad
is
True
:
...
...
@@ -46,7 +53,8 @@ def make_tester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_
inputs
=
[
copy
(
input
)
for
input
in
inputs
]
inputrs
=
[
value
(
input
)
for
input
in
inputs
]
try
:
node
=
self
.
op
.
make_node
(
*
inputrs
)
#node = self.op.make_node(*inputrs)
node
=
safe_make_node
(
self
.
op
,
*
inputrs
)
except
:
type
,
exc_value
,
traceback
=
sys
.
exc_info
()
err_msg
=
"Test
%
s::
%
s: Error occurred while making a node with inputs
%
s"
\
...
...
@@ -80,7 +88,8 @@ def make_tester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_
if
not
isinstance
(
expecteds
,
(
list
,
tuple
)):
expecteds
=
(
expecteds
,
)
for
i
,
(
result
,
expected
)
in
enumerate
(
zip
(
results
,
expecteds
)):
if
result
.
dtype
!=
expected
.
dtype
or
result
.
shape
!=
expected
.
shape
or
numpy
.
any
(
abs
(
result
-
expected
)
>
1e-10
):
if
result
.
dtype
!=
expected
.
dtype
or
result
.
shape
!=
expected
.
shape
or
\
numpy
.
any
(
numpy
.
abs
(
result
-
expected
)
>
1e-10
):
self
.
fail
(
"Test
%
s::
%
s: Output
%
s gave the wrong value. With inputs
%
s, expected
%
s, got
%
s."
%
(
self
.
op
,
testname
,
i
,
inputs
,
expected
,
result
))
...
...
@@ -94,7 +103,7 @@ def make_tester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_
inputs
=
[
copy
(
input
)
for
input
in
inputs
]
inputrs
=
[
value
(
input
)
for
input
in
inputs
]
try
:
node
=
s
elf
.
op
.
make_node
(
*
inputrs
)
node
=
s
afe_make_node
(
self
.
op
,
*
inputrs
)
except
:
return
self
.
fail
(
"Test
%
s::
%
s:
%
s was successfully instantiated on the following bad inputs:
%
s"
...
...
@@ -105,7 +114,7 @@ def make_tester(name, op, expected, checks = {}, good = {}, bad_build = {}, bad_
inputs
=
[
copy
(
input
)
for
input
in
inputs
]
inputrs
=
[
value
(
input
)
for
input
in
inputs
]
try
:
node
=
s
elf
.
op
.
make_node
(
*
inputrs
)
node
=
s
afe_make_node
(
self
.
op
,
*
inputrs
)
except
:
type
,
exc_value
,
traceback
=
sys
.
exc_info
()
err_msg
=
"Test
%
s::
%
s: Error occurred while trying to make a node with inputs
%
s"
\
...
...
@@ -340,8 +349,8 @@ AbsTester = make_broadcast_tester(op = tensor._abs,
expected
=
lambda
x
:
abs
(
x
),
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
AbsInplaceTester
=
make_broadcast_tester
(
op
=
tensor
.
_abs_inplace
,
expected
=
lambda
x
:
abs
(
x
),
AbsInplaceTester
=
make_broadcast_tester
(
op
=
tensor
.
_
_
abs_inplace
,
expected
=
lambda
x
:
numpy
.
abs
(
x
),
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
...
...
@@ -519,7 +528,9 @@ def verify_grad(testcase, op, pt, n_tests=1, rng=numpy.random, eps=0.0000001, to
for
test_num
in
xrange
(
n_tests
):
# tensor_pt = [as_tensor(p,name='input %i'%i) for i,p in enumerate(pt)]
tensor_pt
=
[
constant
(
p
)
.
type
(
'input
%
i'
%
i
)
for
i
,
p
in
enumerate
(
pt
)]
o
=
op
.
make_node
(
*
[
tpt
.
copy
()
for
tpt
in
tensor_pt
])
#o = op.make_node(*[tpt.copy() for tpt in tensor_pt])
o
=
safe_make_node
(
op
,
*
[
tpt
.
copy
()
for
tpt
in
tensor_pt
])
if
hasattr
(
o
,
'outputs'
):
o_outputs
=
o
.
outputs
else
:
...
...
scalar.py
浏览文件 @
36c317bd
...
...
@@ -586,7 +586,7 @@ class Abs(UnaryScalarOp):
return
"
%(z)
s = fabs(
%(x)
s);"
%
locals
()
#complex, other?
raise
NotImplementedError
(
'type not supported'
,
type
)
abs
=
Abs
(
same_out
)
_
abs
=
Abs
(
same_out
)
class
Sgn
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
...
...
tensor.py
浏览文件 @
36c317bd
...
...
@@ -508,6 +508,29 @@ def _elemwise(scalar_op, name, doc_prefix=''):
return
straight
,
inplace
def
_epydoc_cheat
(
real_symbol_value
):
"""Replace the value associated with a function symbol"""
def
decorator
(
f
):
return
real_symbol_value
return
decorator
def
_scal_elemwise
(
symbol
):
"""Replace a symbol definition with an elementwise version of the corresponding scalar Op"""
symbolname
=
symbol
.
__name__
inplace
=
symbolname
.
endswith
(
'_inplace'
)
if
inplace
:
scalar_op
=
getattr
(
scal
,
symbolname
[
1
:
-
len
(
'_inplace'
)])
inplace_scalar_op
=
scalar_op
.
__class__
(
scal
.
transfer_type
(
0
))
rval
=
elemwise
.
Elemwise
(
inplace_scalar_op
,
{
0
:
0
},
name
=
symbolname
)
else
:
scalar_op
=
getattr
(
scal
,
symbolname
)
rval
=
elemwise
.
Elemwise
(
scalar_op
,
name
=
symbolname
)
if
getattr
(
symbol
,
'__doc__'
,
''
):
rval
.
__doc__
=
symbol
.
__doc__
+
'
\n
'
+
rval
.
__doc__
return
rval
#########################
...
...
@@ -674,93 +697,109 @@ def _elemwise_macro_inplace(scalar_op, *args):
inplace
=
elemwise
.
Elemwise
(
inplace_scalar_op
,
{
0
:
0
})
return
inplace
(
*
args
)
@_scal_elemwise
def
lt
(
a
,
b
):
"""a < b"""
return
_elemwise_macro
(
scal
.
lt
,
a
,
b
)
@_scal_elemwise
def
_lt_inplace
(
a
,
b
):
"""a < b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
lt
,
a
,
b
)
@_scal_elemwise
def
gt
(
a
,
b
):
"""a > b"""
return
_elemwise_macro
(
scal
.
gt
,
a
,
b
)
@_scal_elemwise
def
_gt_inplace
(
a
,
b
):
"""a > b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
gt
,
a
,
b
)
@_scal_elemwise
def
le
(
a
,
b
):
"""a <= b"""
return
_elemwise_macro
(
scal
.
le
,
a
,
b
)
@_scal_elemwise
def
_le_inplace
(
a
,
b
):
"""a <= b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
le
,
a
,
b
)
@_scal_elemwise
def
ge
(
a
,
b
):
"""a >= b"""
return
_elemwise_macro
(
scal
.
ge
,
a
,
b
)
@_scal_elemwise
def
_ge_inplace
(
a
,
b
):
"""a >= b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
ge
,
a
,
b
)
@_scal_elemwise
def
eq
(
a
,
b
):
"""a == b"""
return
_elemwise_macro
(
scal
.
eq
,
a
,
b
)
@_scal_elemwise
def
_eq_inplace
(
a
,
b
):
"""a == b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
eq
,
a
,
b
)
@_scal_elemwise
def
neq
(
a
,
b
):
"""a != b"""
return
_elemwise_macro
(
scal
.
neq
,
a
,
b
)
@_scal_elemwise
def
_neq_inplace
(
a
,
b
):
"""a != b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
neq
,
a
,
b
)
##########################
# Bit-wise
##########################
@_scal_elemwise
def
and_
(
a
,
b
):
"""bitwise a & b"""
return
_elemwise_macro
(
scal
.
and_
,
a
,
b
)
@_scal_elemwise
def
_and__inplace
(
a
,
b
):
"""bitwise a & b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
and_
,
a
,
b
)
@_scal_elemwise
def
or_
(
a
,
b
):
"""bitwise a | b"""
return
_elemwise_macro
(
scal
.
or_
,
a
,
b
)
@_scal_elemwise
def
_or__inplace
(
a
,
b
):
"""bitwise a | b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
or_
,
a
,
b
)
def
xor_
(
a
,
b
):
@_scal_elemwise
def
xor
(
a
,
b
):
"""bitwise a ^ b"""
return
_elemwise_macro
(
scal
.
xor_
,
a
,
b
)
def
_xor__inplace
(
a
,
b
):
@_scal_elemwise
def
_xor_inplace
(
a
,
b
):
"""bitwise a ^ b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
xor_
,
a
,
b
)
def
invert_
(
a
,
b
):
"""bitwise a ~ b"""
return
_elemwise_macro
(
scal
.
invert
,
a
,
b
)
def
_invert__inplace
(
a
,
b
):
"""bitwise a ~ b (inplace on a)"""
return
_elemwise_macro_inplace
(
scal
.
invert
,
a
,
b
)
@_scal_elemwise
def
invert
(
a
):
"""bitwise ~a"""
@_scal_elemwise
def
_invert_inplace
(
a
):
"""bitwise ~a (inplace on a)"""
##########################
# Math
##########################
_abs
,
_abs_inplace
=
_elemwise
(
scal
.
abs
,
'abs'
,
"""absolute value (elemwise)"""
)
@_scal_elemwise
def
_abs
(
*
a
):
"""|a|
_abs has a leading underscore because abs() is a builtin. TensorResult overloads the
__abs__ operator so that this function is called when you type abs(a).
"""
@_scal_elemwise
def
__abs_inplace
(
a
):
"""|a| (inplace on a)"""
exp
,
_exp_inplace
=
_elemwise
(
scal
.
exp
,
'exp'
,
"""exponential (elemwise)"""
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论