Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e5b8c40c
提交
e5b8c40c
authored
4月 11, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
4fbcd83a
cf07b78f
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
201 行增加
和
50 行删除
+201
-50
_test_tensor.py
_test_tensor.py
+177
-49
result.py
gof/result.py
+3
-0
gradient.py
gradient.py
+16
-0
scalar.py
scalar.py
+1
-1
tensor.py
tensor.py
+4
-0
没有找到文件。
_test_tensor.py
浏览文件 @
e5b8c40c
...
...
@@ -150,54 +150,178 @@ def randint_notzero(*shape):
r
=
numpy
.
random
.
random_integers
(
-
10
,
9
,
shape
)
return
r
+
(
r
==
0
)
*
10
randplus
=
numpy
.
random
.
rand
# randplus = numpy.random.rand
# randintplus =
# def banzero(f):
# def f2(*shape):
# res = f(*shape)
# while numpy.any(res == 0):
# res = f(*shape)
# return res
# return f2
# def banneg(f):
# def f2(*shape):
# res = f(*shape)
# while numpy.any(res < 0):
# res = f(*shape)
# return res
# return f2
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
)))
_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
=
(
rand
(
2
,
3
),
randint
(
2
,
3
)))
_bad_build_broadcast
=
dict
(
not_same_dimensions
=
(
rand
(
2
),
rand
(
2
,
2
)))
_bad_runtime_broadcast
=
dict
(
not_same_dimensions
=
(
rand
(
2
),
rand
(
2
,
2
)))
_grad_broadcast
=
_good_broadcast
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
=
{})
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
)
# 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(op_class = Abs,
# expected = lambda x: abs(x))
# AbsInplaceTester = make_broadcast_tester(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
,
...
...
@@ -221,7 +345,7 @@ def verify_grad(testcase, op_cls, pt, n_tests=1, rng=numpy.random, eps=0.0000001
for
test_num
in
xrange
(
n_tests
):
tensor_pt
=
[
astensor
(
p
,
name
=
'input
%
i'
%
i
)
for
i
,
p
in
enumerate
(
pt
)]
o
=
op_cls
(
*
tensor_pt
)
o
=
op_cls
(
*
[
tpt
.
copy
()
for
tpt
in
tensor_pt
]
)
if
hasattr
(
o
,
'outputs'
):
o_outputs
=
o
.
outputs
else
:
...
...
@@ -249,7 +373,11 @@ 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
grad_fn
=
Function
(
tensor_pt
,
symbolic_grad
)
try
:
grad_fn
=
Function
(
tensor_pt
,
symbolic_grad
)
except
:
print
gof
.
graph
.
as_string
(
tensor_pt
,
symbolic_grad
)
raise
analytic_grad
=
grad_fn
(
*
pt
)
if
not
isinstance
(
analytic_grad
,
(
list
,
tuple
)):
...
...
gof/result.py
浏览文件 @
e5b8c40c
...
...
@@ -182,6 +182,9 @@ class Result(object):
negligible.
"""
return
False
def
c_literal
(
self
):
raise
AbstractFunctionError
()
def
c_declare
(
self
,
name
,
sub
):
"""
...
...
gradient.py
浏览文件 @
e5b8c40c
...
...
@@ -85,6 +85,22 @@ def grad_sources_inputs(sources, graph_inputs):
output_arg
=
g_outputs
input_arg
=
op
.
inputs
try
:
dinputs
=
[
x
[
0
]
for
x
in
op
.
destroy_map
()
.
values
()]
except
AttributeError
:
dinputs
=
[]
# input_arg = [input in dinputs and input.copy() or input for input in input_arg]
new_input_arg
=
[]
for
input
in
input_arg
:
if
input
in
dinputs
:
new_input_arg
.
append
(
input
.
copy
())
else
:
new_input_arg
.
append
(
input
)
input_arg
=
new_input_arg
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
if
not
isinstance
(
op_grad
,
(
list
,
tuple
)):
raise
ValueError
(
_msg_retType
,
op
.
__class__
)
...
...
scalar.py
浏览文件 @
e5b8c40c
...
...
@@ -291,7 +291,7 @@ class Identity(UnaryScalarOp):
return
x
def
c_code
(
self
,
(
x
,
),
(
z
,
),
sub
):
return
"
%(z)
s =
%(x)
s;"
%
locals
()
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
def
grad
(
self
,
(
x
,
),
(
gz
,
)):
return
gz
,
class
Neg
(
UnaryScalarOp
):
...
...
tensor.py
浏览文件 @
e5b8c40c
...
...
@@ -67,6 +67,10 @@ class Tensor(BaseTensor):
#SLICING
def
__getitem__
(
self
,
item
):
return
subtensor
(
self
,
item
)
def
__getslice__
(
self
,
*
args
):
return
subtensor
(
self
,
slice
(
*
args
))
#COPYING
def
copy
(
self
):
return
tensor_copy
(
self
)
s2t
.
Tensor
=
Tensor
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论