Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2ea6bd45
提交
2ea6bd45
authored
4月 17, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
5af58c40
d1cc7c4b
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
307 行增加
和
216 行删除
+307
-216
_test_tensor.py
_test_tensor.py
+294
-208
elemwise.py
elemwise.py
+5
-2
scalar.py
scalar.py
+8
-6
没有找到文件。
_test_tensor.py
浏览文件 @
2ea6bd45
...
...
@@ -22,8 +22,8 @@ def _numpy_checker(x, y):
raise
Exception
(
"Output mismatch."
,
{
'performlinker'
:
x
,
'clinker'
:
y
})
def
make_tester
(
name
,
op_class
,
expected
,
checks
=
{},
good
=
{},
bad_build
=
{},
bad_runtime
=
{},
grad
=
None
):
if
grad
is
Non
e
:
def
make_tester
(
name
,
op_class
,
expected
,
checks
=
{},
good
=
{},
bad_build
=
{},
bad_runtime
=
{},
grad
=
{}
):
if
grad
is
Tru
e
:
grad
=
good
_op_class
,
_expected
,
_checks
,
_good
,
_bad_build
,
_bad_runtime
,
_grad
=
op_class
,
expected
,
checks
,
good
,
bad_build
,
bad_runtime
,
grad
...
...
@@ -134,8 +134,8 @@ def make_tester(name, op_class, expected, checks = {}, good = {}, bad_build = {}
verify_grad
(
self
,
self
.
op_class
,
inputs
)
except
:
type
,
value
,
traceback
=
sys
.
exc_info
()
err_msg
=
"With data
%
s::
%
s: This error occurred while computing the gradient for
%
s"
\
%
(
self
.
op_class
.
__name__
,
testname
,
self
.
op_class
)
err_msg
=
"With data
%
s::
%
s: This error occurred while computing the gradient for
%
s
on the following inputs:
%
s
"
\
%
(
self
.
op_class
.
__name__
,
testname
,
self
.
op_class
,
inputs
)
value
.
args
=
value
.
args
+
(
err_msg
,
)
raise
type
,
value
,
traceback
...
...
@@ -144,111 +144,20 @@ def make_tester(name, op_class, expected, checks = {}, good = {}, bad_build = {}
rand
=
lambda
*
shape
:
2
*
numpy
.
random
.
rand
(
*
shape
)
-
1
randint
=
lambda
*
shape
:
numpy
.
random
.
random_integers
(
-
10
,
10
,
shape
)
def
randint_notzero
(
*
shape
):
r
=
numpy
.
random
.
random_integers
(
-
10
,
9
,
shape
)
return
r
+
(
r
==
0
)
*
10
randint
=
lambda
*
shape
:
numpy
.
random
.
random_integers
(
-
5
,
5
,
shape
)
# randplus = numpy.random.rand
# randintplus =
def
randint_nonzero
(
*
shape
):
r
=
numpy
.
random
.
random_integers
(
-
5
,
4
,
shape
)
return
r
+
(
r
==
0
)
*
5
# def banzero(f):
# def f2(*shape):
# res = f(*shape)
# while numpy.any(res == 0):
# res = f(*shape)
# return res
# return f2
def
rand_ranged
(
min
,
max
,
shape
):
return
numpy
.
random
.
rand
(
*
shape
)
*
(
max
-
min
)
+
min
# def banneg(f):
# def f2(*shape):
# res = f(*shape)
# while numpy.any(res < 0):
# res = f(*shape)
# return res
# return f2
def
randint_ranged
(
min
,
max
,
shape
):
return
numpy
.
random
.
random_integers
(
min
,
max
,
shape
)
def
make_broadcast_tester
(
op_class
,
expected
,
checks
=
{},
**
kwargs
):
_randint
=
randint
_rand
=
rand
if
kwargs
.
has_key
(
'nonzero'
):
if
kwargs
[
'nonzero'
]:
_randint
=
banzero
(
_randint
)
_rand
=
banzero
(
_rand
)
del
kwargs
[
'nonzero'
]
if
kwargs
.
has_key
(
'positive'
):
if
kwargs
[
'positive'
]:
_randint
=
banneg
(
_randint
)
_rand
=
banneg
(
_rand
)
del
kwargs
[
'positive'
]
_good_broadcast
=
dict
(
same_shapes
=
(
_rand
(
2
,
3
),
_rand
(
2
,
3
)),
scalar
=
(
_rand
(
2
,
3
),
_rand
(
1
,
1
)),
row
=
(
_rand
(
2
,
3
),
_rand
(
1
,
3
)),
column
=
(
_rand
(
2
,
3
),
_rand
(
2
,
1
)),
integers
=
(
_randint
(
2
,
3
),
_randint
(
2
,
3
)),
dtype_mixup_1
=
(
_rand
(
2
,
3
),
_randint
(
2
,
3
)),
dtype_mixup_2
=
(
_randint
(
2
,
3
),
_rand
(
2
,
3
)))
_bad_build_broadcast
=
dict
(
not_same_dimensions
=
(
_rand
(
2
),
_rand
(
2
,
2
)))
_bad_runtime_broadcast
=
dict
(
bad_shapes
=
(
_rand
(
2
,
3
),
_rand
(
3
,
2
)),
bad_row
=
(
_rand
(
2
,
3
),
_rand
(
1
,
2
)))
_grad_broadcast
=
dict
(
same_shapes
=
(
_rand
(
2
,
3
),
_rand
(
2
,
3
)),
scalar
=
(
_rand
(
2
,
3
),
_rand
(
1
,
1
)),
row
=
(
_rand
(
2
,
3
),
_rand
(
1
,
3
)),
column
=
(
_rand
(
2
,
3
),
_rand
(
2
,
1
)))
kwargs
.
setdefault
(
'good'
,
_good_broadcast
)
kwargs
.
setdefault
(
'bad_build'
,
_bad_build_broadcast
)
kwargs
.
setdefault
(
'bad_runtime'
,
_bad_runtime_broadcast
)
kwargs
.
setdefault
(
'grad'
,
_grad_broadcast
)
name
=
op_class
.
__name__
+
"Tester"
if
kwargs
.
has_key
(
'inplace'
):
if
kwargs
[
'inplace'
]:
_expected
=
expected
expected
=
lambda
*
inputs
:
numpy
.
array
(
_expected
(
*
inputs
),
dtype
=
inputs
[
0
]
.
dtype
)
checks
=
dict
(
checks
,
inplace_check
=
lambda
inputs
,
outputs
:
inputs
[
0
]
is
outputs
[
0
])
del
kwargs
[
'inplace'
]
return
make_tester
(
name
,
op_class
,
expected
,
checks
,
**
kwargs
)
def
make_broadcast_tester_unary
(
op_class
,
expected
,
checks
=
{},
**
kwargs
):
_randint
=
randint
_rand
=
rand
if
kwargs
.
has_key
(
'nonzero'
):
if
kwargs
[
'nonzero'
]:
_randint
=
banzero
(
_randint
)
_rand
=
banzero
(
_rand
)
del
kwargs
[
'nonzero'
]
if
kwargs
.
has_key
(
'positive'
):
if
kwargs
[
'positive'
]:
_randint
=
banneg
(
_randint
)
_rand
=
banneg
(
_rand
)
del
kwargs
[
'positive'
]
_good_broadcast
=
dict
(
normal
=
(
_rand
(
2
,
3
),
),
int
=
(
_rand
(
2
,
3
),
))
_bad_build_broadcast
=
dict
()
_bad_runtime_broadcast
=
dict
()
_grad_broadcast
=
dict
(
normal
=
(
_rand
(
2
,
3
),
),
int
=
(
_rand
(
2
,
3
),
))
kwargs
.
setdefault
(
'good'
,
_good_broadcast
)
kwargs
.
setdefault
(
'bad_build'
,
_bad_build_broadcast
)
kwargs
.
setdefault
(
'bad_runtime'
,
_bad_runtime_broadcast
)
kwargs
.
setdefault
(
'grad'
,
_grad_broadcast
)
name
=
op_class
.
__name__
+
"Tester"
if
kwargs
.
has_key
(
'inplace'
):
if
kwargs
[
'inplace'
]:
...
...
@@ -261,108 +170,288 @@ def make_broadcast_tester_unary(op_class, expected, checks = {}, **kwargs):
_good_broadcast_binary_normal
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
)),
integers
=
(
randint
(
2
,
3
),
randint
(
2
,
3
)),
dtype_mixup_1
=
(
rand
(
2
,
3
),
randint
(
2
,
3
)),
dtype_mixup_2
=
(
randint
(
2
,
3
),
rand
(
2
,
3
)))
_bad_build_broadcast_binary_normal
=
dict
(
not_same_dimensions
=
(
rand
(
2
),
rand
(
2
,
2
)))
_bad_runtime_broadcast_binary_normal
=
dict
(
bad_shapes
=
(
rand
(
2
,
3
),
rand
(
3
,
2
)),
bad_row
=
(
rand
(
2
,
3
),
rand
(
1
,
2
)))
_grad_broadcast_binary_normal
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
)))
AddTester
=
make_broadcast_tester
(
op_class
=
Add
,
expected
=
lambda
*
inputs
:
reduce
(
lambda
x
,
y
:
x
+
y
,
inputs
),
good
=
dict
(
three_inputs_same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
),
rand
(
2
,
3
)),
four_inputs_broadcast
=
(
rand
(
2
,
3
),
rand
(
1
,
3
),
rand
(
2
,
1
),
rand
(
1
,
1
)),
**
_good_broadcast_binary_normal
),
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
)
AddInplaceTester
=
make_broadcast_tester
(
op_class
=
AddInplace
,
expected
=
lambda
x
,
y
:
x
+
y
,
good
=
_good_broadcast_binary_normal
,
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
,
inplace
=
True
)
SubTester
=
make_broadcast_tester
(
op_class
=
Sub
,
expected
=
lambda
x
,
y
:
x
-
y
,
good
=
_good_broadcast_binary_normal
,
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
,
grad
=
_grad_broadcast_binary_normal
)
SubInplaceTester
=
make_broadcast_tester
(
op_class
=
SubInplace
,
expected
=
lambda
x
,
y
:
x
-
y
,
good
=
_good_broadcast_binary_normal
,
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
,
grad
=
_grad_broadcast_binary_normal
,
inplace
=
True
)
MulTester
=
make_broadcast_tester
(
op_class
=
Mul
,
expected
=
lambda
*
inputs
:
reduce
(
lambda
x
,
y
:
x
*
y
,
inputs
),
good
=
dict
(
three_inputs_same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
),
rand
(
2
,
3
)),
four_inputs_broadcast
=
(
rand
(
2
,
3
),
rand
(
1
,
3
),
rand
(
2
,
1
),
rand
(
1
,
1
)),
**
_good_broadcast_binary_normal
),
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
,
grad
=
dict
(
three_inputs_same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
),
rand
(
2
,
3
)),
four_inputs_broadcast
=
(
rand
(
2
,
3
),
rand
(
1
,
3
),
rand
(
2
,
1
),
rand
(
1
,
1
)),
**
_grad_broadcast_binary_normal
))
MulInplaceTester
=
make_broadcast_tester
(
op_class
=
MulInplace
,
expected
=
lambda
x
,
y
:
x
*
y
,
good
=
_good_broadcast_binary_normal
,
bad_build
=
_bad_build_broadcast_binary_normal
,
bad_runtime
=
_bad_runtime_broadcast_binary_normal
,
grad
=
_grad_broadcast_binary_normal
,
inplace
=
True
)
DivTester
=
make_broadcast_tester
(
op_class
=
Div
,
expected
=
lambda
x
,
y
:
x
/
y
,
good
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
)),
dtype_mixup_1
=
(
rand
(
2
,
3
),
randint_nonzero
(
2
,
3
)),
dtype_mixup_2
=
(
randint_nonzero
(
2
,
3
),
rand
(
2
,
3
)),
# integers_positive = (randint_ranged(4, 10, (2, 3)), randint_ranged(1, 6, (2, 3))),
# integers_known_to_fail = (numpy.array(-1), numpy.array(5))
),
# integers = (randint(2, 3), randint_nonzero(2, 3)),
# dtype_mixup_1 = (rand(2, 3), randint_nonzero(2, 3)),
# dtype_mixup_2 = (randint_nonzero(2, 3), rand(2, 3))),
grad
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
))))
DivInplaceTester
=
make_broadcast_tester
(
op_class
=
DivInplace
,
expected
=
lambda
x
,
y
:
x
/
y
,
good
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
)),
dtype_mixup_1
=
(
rand
(
2
,
3
),
randint_nonzero
(
2
,
3
)),
dtype_mixup_2
=
(
randint_nonzero
(
2
,
3
),
rand
(
2
,
3
))
),
grad
=
dict
(
same_shapes
=
(
rand
(
2
,
3
),
rand
(
2
,
3
)),
scalar
=
(
rand
(
2
,
3
),
rand
(
1
,
1
)),
row
=
(
rand
(
2
,
3
),
rand
(
1
,
3
)),
column
=
(
rand
(
2
,
3
),
rand
(
2
,
1
))),
inplace
=
True
)
PowTester
=
make_broadcast_tester
(
op_class
=
Pow
,
expected
=
lambda
x
,
y
:
x
**
y
,
good
=
dict
(
same_shapes
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
3
))),
scalar
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
1
))),
row
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
3
))),
column
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
1
))),
dtype_mixup
=
(
rand_ranged
(
-
3
,
3
,
(
2
,
3
)),
randint_ranged
(
-
3
,
3
,
(
2
,
3
)))),
grad
=
dict
(
same_shapes
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
3
))),
scalar
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
1
))),
row
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
3
))),
column
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
1
))))
)
PowTester
=
make_broadcast_tester
(
op_class
=
PowInplace
,
expected
=
lambda
x
,
y
:
x
**
y
,
good
=
dict
(
same_shapes
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
3
))),
scalar
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
1
))),
row
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
3
))),
column
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
1
))),
dtype_mixup
=
(
rand_ranged
(
-
3
,
3
,
(
2
,
3
)),
randint_ranged
(
-
3
,
3
,
(
2
,
3
)))),
grad
=
dict
(
same_shapes
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
3
))),
scalar
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
1
))),
row
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
1
,
3
))),
column
=
(
rand_ranged
(
1
,
5
,
(
2
,
3
)),
rand_ranged
(
-
3
,
3
,
(
2
,
1
)))),
inplace
=
True
)
_good_broadcast_unary_normal
=
dict
(
normal
=
(
rand_ranged
(
-
5
,
5
,
(
2
,
3
)),),
integers
=
(
randint_ranged
(
-
5
,
5
,
(
2
,
3
)),))
_grad_broadcast_unary_normal
=
dict
(
normal
=
(
rand_ranged
(
-
5
,
5
,
(
2
,
3
)),))
AbsTester
=
make_broadcast_tester
(
op_class
=
Abs
,
expected
=
lambda
x
:
abs
(
x
),
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
AbsInplaceTester
=
make_broadcast_tester
(
op_class
=
AbsInplace
,
expected
=
lambda
x
:
abs
(
x
),
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
NegTester
=
make_broadcast_tester
(
op_class
=
Neg
,
expected
=
lambda
x
:
-
x
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
NegInplaceTester
=
make_broadcast_tester
(
op_class
=
NegInplace
,
expected
=
lambda
x
:
-
x
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
SgnTester
=
make_broadcast_tester
(
op_class
=
Sgn
,
expected
=
numpy
.
sign
,
good
=
_good_broadcast_unary_normal
)
SgnInplaceTester
=
make_broadcast_tester
(
op_class
=
SgnInplace
,
expected
=
numpy
.
sign
,
good
=
_good_broadcast_unary_normal
,
inplace
=
True
)
SqrTester
=
make_broadcast_tester
(
op_class
=
Sqr
,
expected
=
numpy
.
square
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
SqrInplaceTester
=
make_broadcast_tester
(
op_class
=
SqrInplace
,
expected
=
numpy
.
square
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
ExpTester
=
make_broadcast_tester
(
op_class
=
Exp
,
expected
=
numpy
.
exp
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
ExpInplaceTester
=
make_broadcast_tester
(
op_class
=
ExpInplace
,
expected
=
numpy
.
exp
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
_good_broadcast_unary_positive
=
dict
(
normal
=
(
rand_ranged
(
0.001
,
5
,
(
2
,
3
)),),
integers
=
(
randint_ranged
(
1
,
5
,
(
2
,
3
)),))
_grad_broadcast_unary_positive
=
dict
(
normal
=
(
rand_ranged
(
0.001
,
5
,
(
2
,
3
)),))
LogTester
=
make_broadcast_tester
(
op_class
=
Log
,
expected
=
numpy
.
log
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
)
LogInplaceTester
=
make_broadcast_tester
(
op_class
=
LogInplace
,
expected
=
numpy
.
log
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
,
inplace
=
True
)
Log2Tester
=
make_broadcast_tester
(
op_class
=
Log2
,
expected
=
numpy
.
log2
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
)
Log2InplaceTester
=
make_broadcast_tester
(
op_class
=
Log2Inplace
,
expected
=
numpy
.
log2
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
,
inplace
=
True
)
SqrtTester
=
make_broadcast_tester
(
op_class
=
Sqrt
,
expected
=
numpy
.
sqrt
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
)
SqrtInplaceTester
=
make_broadcast_tester
(
op_class
=
SqrtInplace
,
expected
=
numpy
.
sqrt
,
good
=
_good_broadcast_unary_positive
,
grad
=
_grad_broadcast_unary_positive
,
inplace
=
True
)
_good_broadcast_unary_wide
=
dict
(
normal
=
(
rand_ranged
(
-
1000
,
1000
,
(
2
,
3
)),),
integers
=
(
randint_ranged
(
-
1000
,
1000
,
(
2
,
3
)),))
_grad_broadcast_unary_wide
=
dict
(
normal
=
(
rand_ranged
(
-
1000
,
1000
,
(
2
,
3
)),))
SinTester
=
make_broadcast_tester
(
op_class
=
Sin
,
expected
=
numpy
.
sin
,
good
=
_good_broadcast_unary_wide
,
grad
=
_grad_broadcast_unary_wide
)
SinInplaceTester
=
make_broadcast_tester
(
op_class
=
SinInplace
,
expected
=
numpy
.
sin
,
good
=
_good_broadcast_unary_wide
,
grad
=
_grad_broadcast_unary_wide
,
inplace
=
True
)
CosTester
=
make_broadcast_tester
(
op_class
=
Cos
,
expected
=
numpy
.
cos
,
good
=
_good_broadcast_unary_wide
,
grad
=
_grad_broadcast_unary_wide
)
CosInplaceTester
=
make_broadcast_tester
(
op_class
=
CosInplace
,
expected
=
numpy
.
cos
,
good
=
_good_broadcast_unary_wide
,
grad
=
_grad_broadcast_unary_wide
,
inplace
=
True
)
TanTester
=
make_broadcast_tester
(
op_class
=
Tan
,
expected
=
numpy
.
tan
,
good
=
dict
(
normal
=
(
rand_ranged
(
-
3.14
,
3.14
,
(
2
,
3
)),),
shifted
=
(
rand_ranged
(
3.15
,
6.28
,
(
2
,
3
)),)))
TanInplaceTester
=
make_broadcast_tester
(
op_class
=
CosInplace
,
expected
=
numpy
.
cos
,
good
=
dict
(
normal
=
(
rand_ranged
(
-
3.14
,
3.14
,
(
2
,
3
)),),
shifted
=
(
rand_ranged
(
3.15
,
6.28
,
(
2
,
3
)),)),
inplace
=
True
)
CoshTester
=
make_broadcast_tester
(
op_class
=
Cosh
,
expected
=
numpy
.
cosh
,
good
=
_good_broadcast_unary_normal
)
CoshInplaceTester
=
make_broadcast_tester
(
op_class
=
CoshInplace
,
expected
=
numpy
.
cosh
,
good
=
_good_broadcast_unary_normal
,
inplace
=
True
)
SinhTester
=
make_broadcast_tester
(
op_class
=
Sinh
,
expected
=
numpy
.
sinh
,
good
=
_good_broadcast_unary_normal
)
SinhInplaceTester
=
make_broadcast_tester
(
op_class
=
SinhInplace
,
expected
=
numpy
.
sinh
,
good
=
_good_broadcast_unary_normal
,
inplace
=
True
)
TanhTester
=
make_broadcast_tester
(
op_class
=
Tanh
,
expected
=
numpy
.
tanh
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
)
TanhInplaceTester
=
make_broadcast_tester
(
op_class
=
TanhInplace
,
expected
=
numpy
.
tanh
,
good
=
_good_broadcast_unary_normal
,
grad
=
_grad_broadcast_unary_normal
,
inplace
=
True
)
# AddTester = make_broadcast_tester(op_class = Add,
# expected = lambda x, y: x + y,
# grad = {})
# AddInplaceTester = make_broadcast_tester(op_class = AddInplace,
# expected = lambda x, y: x + y,
# inplace = True,
# grad = {})
# SubTester = make_broadcast_tester(op_class = Sub,
# expected = lambda x, y: x - y)
# SubInplaceTester = make_broadcast_tester(op_class = SubInplace,
# expected = lambda x, y: x - y,
# inplace = True)
# MulTester = make_broadcast_tester(op_class = Mul,
# expected = lambda x, y: x * y)
# MulInplaceTester = make_broadcast_tester(op_class = MulInplace,
# expected = lambda x, y: x * y,
# inplace = True)
# DivTester = make_broadcast_tester(op_class = Div,
# expected = lambda x, y: x / y,
# nonzero = True,
# positive = True)
# DivInplaceTester = make_broadcast_tester(op_class = DivInplace,
# expected = lambda x, y: x / y,
# inplace = True,
# nonzero = True,
# positive = True)
# _pow_good = dict(normal = ()
# _pow_grad =
# PowTester = make_broadcast_tester(op_class = Pow,
# expected = lambda x, y: x ** y,
# good = _pow_good)
# PowInplaceTester = make_broadcast_tester(op_class = PowInplace,
# expected = lambda x, y: x ** y,
# good = _pow_good)
AbsTester
=
make_broadcast_tester_unary
(
op_class
=
Abs
,
expected
=
lambda
x
:
abs
(
x
))
AbsInplaceTester
=
make_broadcast_tester_unary
(
op_class
=
AbsInplace
,
expected
=
lambda
x
:
abs
(
x
),
inplace
=
True
)
# ExpTester = make_broadcast_tester(op_class = Exp,
# expected = lambda x: numpy.exp(x))
# ExpInplaceTester = make_broadcast_tester(op_class = ExpInplace,
# expected = lambda x: numpy.exp(x),
# inplace = True)
# Abs, _abs, AbsInplace, abs_inplace = broadcast(scal.Abs, 'Abs')
# Exp, exp, ExpInplace, exp_inplace = broadcast(scal.Exp, 'Exp')
# Neg, neg, NegInplace, neg_inplace = broadcast(scal.Neg, 'Neg')
# Log, log, LogInplace, log_inplace = broadcast(scal.Log, 'Log')
# Log2, log2, Log2Inplace, log2_inplace = broadcast(scal.Log2, 'Log2')
# Sgn, sgn, SgnInplace, sgn_inplace = broadcast(scal.Sgn, 'Sgn')
# Sqr, sqr, SqrInplace, sqr_inplace = broadcast(scal.Sqr, 'Sqr')
# Sqrt, sqrt, SqrtInplace, sqrt_inplace = broadcast(scal.Sqrt, 'Sqrt')
# AddTester = make_broadcast_tester(op_class = Add,
# expected = lambda x, y: x + y,
# checks = {},
# grad = {})
# AddInplaceTester = make_broadcast_tester(op_class = AddInplace,
# expected = lambda x, y: numpy.array(x + y, dtype = x.dtype),
# checks = dict(inplace_check = lambda (x, y), (z, ): x is z),
# grad = {})
# AddTester = make_tester(name = 'AddTester',
# op_class = Add,
# expected = lambda x, y: x + y,
# checks = {},
# good = dict(same_shapes = (rand(5, 6), rand(5, 6)),
# scalar = (rand(5, 6), rand(1, 1)),
# row = (rand(5, 6), rand(1, 6)),
# column = (rand(5, 6), rand(5, 1)),
# integers = (randint(5, 6), randint(5, 6)),
# dtype_mixup = (rand(5, 6), randint(5, 6))),
# bad_build = dict(not_same_dimensions = (rand(5), rand(5, 5))),
# bad_runtime = dict(bad_shapes = (rand(5, 6), rand(6, 5)),
# bad_row = (rand(5, 6), rand(1, 5))),
# grad = {})
# AddInplaceTester = make_tester(name = 'AddInplaceTester',
# op_class = AddInplace,
# expected = lambda x, y: numpy.array(x + y, dtype = x.dtype),
# checks = dict(inplace_check = lambda (x, y), (z, ): x is z),
# good = dict(same_shapes = (rand(5, 6), rand(5, 6)),
# dtype_mixup = (randint(5, 6), rand(5, 6))),
# bad_build = dict(not_same_dimensions = (rand(5), rand(5, 5))),
# bad_runtime = dict(bad_shapes = (rand(5, 6), rand(6, 5)),
# bad_row = (rand(5, 6), rand(1, 5))),
# grad = {})
DotTester
=
make_tester
(
name
=
'DotTester'
,
op_class
=
Dot
,
...
...
@@ -414,11 +503,8 @@ def verify_grad(testcase, op_cls, pt, n_tests=1, rng=numpy.random, eps=0.0000001
print
'----------'
for
op
in
gof
.
graph
.
io_toposort
(
tensor_pt
,
symbolic_grad
):
print
op
try
:
grad_fn
=
Function
(
tensor_pt
,
symbolic_grad
)
except
:
print
gof
.
graph
.
as_string
(
tensor_pt
,
symbolic_grad
)
raise
grad_fn
=
Function
(
tensor_pt
,
symbolic_grad
)
analytic_grad
=
grad_fn
(
*
pt
)
if
not
isinstance
(
analytic_grad
,
(
list
,
tuple
)):
...
...
elemwise.py
浏览文件 @
2ea6bd45
...
...
@@ -149,13 +149,13 @@ class Broadcast(Op, Destroyer):
if
ib
and
not
ob
:
raise
ValueError
(
"Operation cannot be done inplace on an input with broadcasted dimensions."
)
upcasted
=
upcast
(
*
[
input
.
dtype
for
input
in
inputs
])
out_dtypes
=
[
t
.
dtype
for
t
in
self
.
shadow
.
outputs
]
def
get_dtype
(
i
):
input_idx
=
inplace_pattern
.
get
(
i
,
None
)
if
input_idx
is
not
None
:
return
inputs
[
input_idx
]
.
dtype
else
:
return
upcasted
return
out_dtypes
[
i
]
out_dtypes
=
map
(
get_dtype
,
xrange
(
self
.
nout
))
self
.
inputs
=
inputs
self
.
outputs
=
[
Tensor
(
dtype
=
dtype
,
broadcastable
=
broadcastable
)
for
dtype
,
broadcastable
in
zip
(
out_dtypes
,
out_broadcastables
)]
...
...
@@ -201,6 +201,9 @@ class Broadcast(Op, Destroyer):
return
bcasted
ret
=
[]
for
scalar_igrad
,
input
in
zip
(
scalar_igrads
,
inputs
):
if
scalar_igrad
is
None
:
ret
.
append
(
None
)
continue
r
=
transform
(
scalar_igrad
)
to_sum
=
[
i
for
i
,
bcast
in
enumerate
(
input
.
broadcastable
)
if
bcast
]
if
to_sum
:
...
...
scalar.py
浏览文件 @
2ea6bd45
...
...
@@ -282,6 +282,8 @@ class Div(BinaryScalarOp):
def
impl
(
self
,
x
,
y
):
return
x
/
y
def
c_code
(
self
,
(
x
,
y
),
(
z
,
),
sub
):
if
'int'
in
self
.
inputs
[
0
]
.
dtype
and
'int'
in
self
.
inputs
[
1
]
.
dtype
:
raise
NotImplementedError
(
"For integer arguments the behavior of division in C and in Python differ when the quotient is negative (to implement)."
)
return
"
%(z)
s =
%(x)
s /
%(y)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
return
gz
/
y
,
-
(
gz
*
x
)
/
(
y
*
y
)
...
...
@@ -346,13 +348,13 @@ class Abs(UnaryScalarOp):
class
Sgn
(
UnaryScalarOp
):
def
impl
(
self
,
x
):
#casting to output type is handled by filter
return
1.0
if
x
>=
0
else
-
1.0
return
numpy
.
sign
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
None
,
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
#casting is done by compiler
#TODO: use copysign
return
"
%(z)
s = (
%(x)
s >= 0) ? 1.0 : -1.0;"
%
locals
()
return
"
%(z)
s = (
%(x)
s >= 0) ?
(
%(x)
s == 0) ? 0.0 :
1.0 : -1.0;"
%
locals
()
class
Inv
(
FloatUnaryScalarOp
):
def
impl
(
self
,
x
):
...
...
@@ -406,7 +408,7 @@ class Cos(FloatUnaryScalarOp):
def
impl
(
self
,
x
):
return
math
.
cos
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
gz
*
sin
(
x
),
return
-
gz
*
sin
(
x
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = cos(
%(x)
s);"
%
locals
()
...
...
@@ -414,7 +416,7 @@ class Sin(FloatUnaryScalarOp):
def
impl
(
self
,
x
):
return
math
.
sin
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
-
gz
*
cos
(
x
),
return
gz
*
cos
(
x
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = sin(
%(x)
s);"
%
locals
()
...
...
@@ -440,13 +442,13 @@ class Sinh(FloatUnaryScalarOp):
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
raise
NotImplementedError
()
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = sin(
%(x)
s);"
%
locals
()
return
"
%(z)
s = sin
h
(
%(x)
s);"
%
locals
()
class
Tanh
(
FloatUnaryScalarOp
):
def
impl
(
self
,
x
):
return
math
.
tanh
(
x
)
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
gz
*
(
1
-
tanh
(
x
)
)
**
2
return
gz
*
(
1
-
tanh
(
x
)
**
2
),
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s = tanh(
%(x)
s);"
%
locals
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论