Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
0d8dc459
提交
0d8dc459
authored
9月 19, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed tests
上级
f6e05092
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
253 行增加
和
295 行删除
+253
-295
__init__.py
__init__.py
+1
-1
_test_compile.py
_test_compile.py
+231
-291
graph.py
gof/graph.py
+4
-2
tensor_random.py
tensor_random.py
+17
-1
没有找到文件。
__init__.py
浏览文件 @
0d8dc459
...
@@ -9,7 +9,7 @@ from gof import \
...
@@ -9,7 +9,7 @@ from gof import \
Type
,
Generic
,
generic
,
\
Type
,
Generic
,
generic
,
\
object2
,
utils
object2
,
utils
from
compile
import
function
,
eval_outputs
,
fast_compute
,
OpFromGraph
from
compile
import
FunctionMaker
,
function
,
OpFromGraph
#, eval_outputs, fast_compute
import
tensor
import
tensor
import
tensor_random
import
tensor_random
...
...
_test_compile.py
浏览文件 @
0d8dc459
...
@@ -23,174 +23,174 @@ def checkfor(testcase, fn, E):
...
@@ -23,174 +23,174 @@ def checkfor(testcase, fn, E):
testcase
.
fail
()
testcase
.
fail
()
def
graph1
():
# (x+y) * (x/z)
#
def graph1(): # (x+y) * (x/z)
x
,
y
,
z
=
floats
(
'xyz'
)
#
x, y, z = floats('xyz')
o
=
mul
(
add
(
x
,
y
),
div
(
x
,
z
))
#
o = mul(add(x, y), div(x, z))
return
[
x
,
y
,
z
],
[
o
]
#
return [x,y,z], [o]
class
T_Function
(
unittest
.
TestCase
):
#
class T_Function(unittest.TestCase):
def
test_noopt
(
self
):
#
def test_noopt(self):
gi
,
go
=
graph1
()
#
gi, go = graph1()
p
=
function
(
gi
,
go
,
optimizer
=
None
,
linker
=
'py'
)
#
p = function(gi, go, optimizer = None, linker = 'py')
self
.
failUnless
(
p
(
1.0
,
3.0
,
4.0
)
==
1.0
)
#
self.failUnless(p(1.0,3.0,4.0) == 1.0)
def
test_opt
(
self
):
#
def test_opt(self):
opt
=
PatternOptimizer
((
div
,
'1'
,
'2'
),
(
div
,
'2'
,
'1'
))
#
opt = PatternOptimizer((div, '1', '2'), (div, '2', '1'))
gi
,
go
=
graph1
()
#
gi, go = graph1()
p
=
function
(
gi
,
go
,
optimizer
=
opt
.
optimize
,
linker
=
'py'
)
#
p = function(gi,go, optimizer=opt.optimize, linker = 'py')
self
.
failUnless
(
p
(
1.
,
3.
,
4.
)
==
16.0
)
#
self.failUnless(p(1.,3.,4.) == 16.0)
def
test_multiout
(
self
):
#
def test_multiout(self):
def
graph2
():
#
def graph2():
x
,
y
,
z
=
floats
(
'xyz'
)
#
x, y, z = floats('xyz')
o
=
mul
(
add
(
x
,
y
),
div
(
x
,
z
))
#
o = mul(add(x, y), div(x, z))
return
[
x
,
y
,
z
],
[
o
,
o
.
owner
.
inputs
[
1
]]
#
return [x,y,z], [o, o.owner.inputs[1]]
opt
=
PatternOptimizer
((
div
,
'1'
,
'2'
),
(
div
,
'2'
,
'1'
))
#
opt = PatternOptimizer((div, '1', '2'), (div, '2', '1'))
gi
,
go
=
graph2
()
#
gi, go = graph2()
p
=
function
(
gi
,
go
,
optimizer
=
opt
.
optimize
)
#
p = function(gi,go, optimizer=opt.optimize)
a
,
b
=
p
(
1.
,
3.
,
4.
)
#
a,b = p(1.,3.,4.)
self
.
failUnless
(
a
==
16.0
)
#
self.failUnless(a == 16.0)
self
.
failUnless
(
b
==
4.0
)
#
self.failUnless(b == 4.0)
def
test_make_many_functions
(
self
):
#
def test_make_many_functions(self):
x
,
y
,
z
=
tensor
.
scalars
(
'xyz'
)
#
x, y, z = tensor.scalars('xyz')
e0
,
e1
,
e2
=
x
+
y
+
z
,
x
*
y
-
z
,
z
*
z
+
x
*
x
+
y
*
y
#
e0, e1, e2 = x+y+z, x*y-z, z*z+x*x+y*y
f1
=
function
([
x
,
y
,
z
],
[
e0
])
#
f1 = function([x, y, z], [e0])
f2
=
function
([
x
,
y
,
z
],
[
e0
])
#
f2 = function([x, y, z], [e0])
f3
=
function
([
x
,
y
,
z
],
[
e1
])
#
f3 = function([x, y, z], [e1])
f4
=
function
([
x
,
y
,
z
],
[
e2
])
#
f4 = function([x, y, z], [e2])
f5
=
function
([
e0
],
[
e0
*
e0
])
#
f5 = function([e0], [e0 * e0])
ff
=
FunctionFactory
([
x
,
y
,
z
],
[
e0
])
#
ff = FunctionFactory([x, y, z], [e0])
f6
=
ff
.
create
()
#
f6 = ff.create()
f7
=
ff
.
create
()
#
f7 = ff.create()
f8
=
ff
.
create
()
#
f8 = ff.create()
f9
=
ff
.
partial
(
1.0
,
2.0
)
#
f9 = ff.partial(1.0, 2.0)
assert
f1
(
1.0
,
2.0
,
3.0
)
==
6.0
#
assert f1(1.0, 2.0, 3.0) == 6.0
assert
f2
(
1.0
,
2.0
,
3.0
)
==
6.0
#
assert f2(1.0, 2.0, 3.0) == 6.0
assert
f3
(
1.0
,
2.0
,
3.0
)
==
-
1.0
#
assert f3(1.0, 2.0, 3.0) == -1.0
assert
f4
(
1.0
,
2.0
,
3.0
)
==
14.0
#
assert f4(1.0, 2.0, 3.0) == 14.0
assert
f5
(
7.0
)
==
49.0
#
assert f5(7.0) == 49.0
assert
f6
(
1.0
,
2.0
,
3.0
)
==
6.0
#
assert f6(1.0, 2.0, 3.0) == 6.0
assert
f7
(
1.0
,
2.0
,
3.0
)
==
6.0
#
assert f7(1.0, 2.0, 3.0) == 6.0
assert
f8
(
1.0
,
2.0
,
3.0
)
==
6.0
#
assert f8(1.0, 2.0, 3.0) == 6.0
assert
f9
(
3.0
)
==
6.0
#
assert f9(3.0) == 6.0
def
test_no_inputs
(
self
):
#
def test_no_inputs(self):
x
,
y
,
z
=
tensor
.
value
(
1.0
),
tensor
.
value
(
2.0
),
tensor
.
value
(
3.0
)
#
x, y, z = tensor.value(1.0), tensor.value(2.0), tensor.value(3.0)
e
=
x
*
x
+
y
*
y
+
z
*
z
#
e = x*x + y*y + z*z
assert
function
([],
[
e
],
linker
=
'py'
)()
==
14.0
#
assert function([], [e], linker = 'py')() == 14.0
assert
function
([],
[
e
],
linker
=
'c'
)()
==
14.0
#
assert function([], [e], linker = 'c')() == 14.0
assert
function
([],
[
e
],
linker
=
'c|py'
)()
==
14.0
#
assert function([], [e], linker = 'c|py')() == 14.0
assert
function
([],
[
e
],
linker
=
'c&py'
)()
==
14.0
#
assert function([], [e], linker = 'c&py')() == 14.0
assert
eval_outputs
([
e
])
==
14.0
#
assert eval_outputs([e]) == 14.0
assert
fast_compute
(
e
)
==
14.0
#
assert fast_compute(e) == 14.0
def
test_closure
(
self
):
#
def test_closure(self):
x
,
y
,
z
=
tensor
.
scalars
(
'xyz'
)
#
x, y, z = tensor.scalars('xyz')
v
=
tensor
.
value
(
numpy
.
zeros
(()))
#
v = tensor.value(numpy.zeros(()))
e
=
x
+
tensor
.
add_inplace
(
v
,
1
)
#
e = x + tensor.add_inplace(v, 1)
f
=
function
([
x
],
[
e
])
#
f = function([x], [e])
assert
f
(
1.
)
==
2.
#
assert f(1.) == 2.
assert
f
(
1.
)
==
3.
#
assert f(1.) == 3.
assert
f
(
1.
)
==
4.
#
assert f(1.) == 4.
def
test_borrow_true
(
self
):
#
def test_borrow_true(self):
x
,
y
,
z
=
tensor
.
scalars
(
'xyz'
)
#
x, y, z = tensor.scalars('xyz')
e
=
x
+
y
+
z
#
e = x + y + z
f
=
function
([
x
,
y
,
z
],
[
e
],
borrow_outputs
=
True
)
#
f = function([x, y, z], [e], borrow_outputs = True)
res1
=
f
(
1.0
,
2.0
,
3.0
)
#
res1 = f(1.0, 2.0, 3.0)
assert
res1
==
6.0
#
assert res1 == 6.0
res2
=
f
(
1.0
,
3.0
,
5.0
)
#
res2 = f(1.0, 3.0, 5.0)
assert
res1
is
res2
#
assert res1 is res2
assert
res1
==
9.0
#
assert res1 == 9.0
assert
res2
==
9.0
#
assert res2 == 9.0
def
test_borrow_false
(
self
):
#
def test_borrow_false(self):
x
,
y
,
z
=
tensor
.
scalars
(
'xyz'
)
#
x, y, z = tensor.scalars('xyz')
e
=
x
+
y
+
z
#
e = x + y + z
for
linker
in
'py c c|py c&py'
.
split
():
#
for linker in 'py c c|py c&py'.split():
f
=
function
([
x
,
y
,
z
],
[
e
],
borrow_outputs
=
False
,
linker
=
linker
)
#
f = function([x, y, z], [e], borrow_outputs = False, linker = linker)
res1
=
f
(
1.0
,
2.0
,
3.0
)
#
res1 = f(1.0, 2.0, 3.0)
self
.
failUnless
(
res1
==
6.0
,
(
res1
,
linker
))
#
self.failUnless(res1 == 6.0, (res1, linker))
res2
=
f
(
1.0
,
3.0
,
5.0
)
#
res2 = f(1.0, 3.0, 5.0)
self
.
failUnless
(
res1
is
not
res2
,
(
res1
,
res2
,
linker
))
#
self.failUnless(res1 is not res2, (res1, res2, linker))
self
.
failUnless
(
res1
==
6.0
,
(
res1
,
linker
))
#
self.failUnless(res1 == 6.0, (res1, linker))
self
.
failUnless
(
res2
==
9.0
,
(
res2
,
linker
))
#
self.failUnless(res2 == 9.0, (res2, linker))
def
test_borrow_false_through_inplace
(
self
):
#
def test_borrow_false_through_inplace(self):
x
,
y
,
z
=
tensor
.
scalars
(
'xyz'
)
#
x, y, z = tensor.scalars('xyz')
# if borrow_outputs is False, we must not reuse the temporary created for x+y
#
# if borrow_outputs is False, we must not reuse the temporary created for x+y
e
=
tensor
.
add_inplace
(
x
+
y
,
z
)
#
e = tensor.add_inplace(x + y, z)
for
linker
in
'py c c|py c&py'
.
split
():
#
for linker in 'py c c|py c&py'.split():
f
=
function
([
x
,
y
,
z
],
[
e
],
borrow_outputs
=
False
,
linker
=
linker
)
#
f = function([x, y, z], [e], borrow_outputs = False, linker = linker)
res1
=
f
(
1.0
,
2.0
,
3.0
)
#
res1 = f(1.0, 2.0, 3.0)
self
.
failUnless
(
res1
==
6.0
,
(
res1
,
linker
))
#
self.failUnless(res1 == 6.0, (res1, linker))
res2
=
f
(
1.0
,
3.0
,
5.0
)
#
res2 = f(1.0, 3.0, 5.0)
self
.
failUnless
(
res1
is
not
res2
,
(
res1
,
res2
,
linker
))
#
self.failUnless(res1 is not res2, (res1, res2, linker))
self
.
failUnless
(
res1
==
6.0
,
(
res1
,
linker
))
#
self.failUnless(res1 == 6.0, (res1, linker))
self
.
failUnless
(
res2
==
9.0
,
(
res2
,
linker
))
#
self.failUnless(res2 == 9.0, (res2, linker))
class
T_fast_compute
(
unittest
.
TestCase
):
#
class T_fast_compute(unittest.TestCase):
def
test_straightforward
(
self
):
#
def test_straightforward(self):
x
,
y
,
z
=
tensor
.
value
(
1.0
),
tensor
.
value
(
2.0
),
tensor
.
value
(
3.0
)
#
x, y, z = tensor.value(1.0), tensor.value(2.0), tensor.value(3.0)
e
=
x
*
x
+
y
*
y
+
z
*
z
#
e = x*x + y*y + z*z
assert
fast_compute
(
e
)
==
14.0
#
assert fast_compute(e) == 14.0
assert
compile
.
_fcache
[(
e
,
)]()
==
14.0
#
assert compile._fcache[(e, )]() == 14.0
import
tensor
as
T
import
tensor
as
T
import
random
import
random
import
numpy
as
N
import
numpy
as
N
class
T_OpFromGraph
(
unittest
.
TestCase
):
# class T_OpFromGraph(unittest.TestCase):
def
test_straightforward
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
# def test_straightforward(self):
e
=
x
+
y
*
z
# x, y, z = T.matrices('xyz')
op
=
OpFromGraph
([
x
,
y
,
z
],
[
e
],
linker
=
'c|py'
)
# e = x + y * z
f
=
op
(
x
,
y
,
z
)
-
op
(
y
,
z
,
x
)
# op = OpFromGraph([x, y, z], [e], linker='c|py')
fn
=
function
([
x
,
y
,
z
],
[
f
])
# f = op(x, y, z) - op(y, z, x)
xv
,
yv
,
zv
=
N
.
ones
((
2
,
2
)),
N
.
ones
((
2
,
2
))
*
3
,
N
.
ones
((
2
,
2
))
*
5
# fn = function([x, y, z], [f])
assert
numpy
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
# xv, yv, zv = N.ones((2, 2)), N.ones((2, 2))*3, N.ones((2, 2))*5
assert
numpy
.
all
(
8.0
==
fn
(
xv
,
yv
,
zv
))
# assert numpy.all(8.0 == fn(xv, yv, zv))
# assert numpy.all(8.0 == fn(xv, yv, zv))
def
test_size_changes
(
self
):
#
def test_size_changes(self):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
#
x, y, z = T.matrices('xyz')
e
=
T
.
dot
(
x
,
y
)
#
e = T.dot(x, y)
op
=
OpFromGraph
([
x
,
y
],
[
e
],
linker
=
'c|py'
)
#
op = OpFromGraph([x, y], [e], linker='c|py')
f
=
op
(
x
,
op
(
y
,
z
))
#
f = op(x, op(y, z))
fn
=
function
([
x
,
y
,
z
],
[
f
])
#
fn = function([x, y, z], [f])
xv
,
yv
,
zv
=
N
.
ones
((
2
,
3
)),
N
.
ones
((
3
,
4
))
*
3
,
N
.
ones
((
4
,
5
))
*
5
#
xv, yv, zv = N.ones((2, 3)), N.ones((3, 4))*3, N.ones((4, 5))*5
res
=
fn
(
xv
,
yv
,
zv
)
#
res = fn(xv, yv, zv)
assert
res
.
shape
==
(
2
,
5
)
#
assert res.shape == (2, 5)
assert
numpy
.
all
(
180.0
==
res
)
#
assert numpy.all(180.0 == res)
res
=
fn
(
xv
,
yv
,
zv
)
#
res = fn(xv, yv, zv)
assert
res
.
shape
==
(
2
,
5
)
#
assert res.shape == (2, 5)
assert
numpy
.
all
(
180.0
==
res
)
#
assert numpy.all(180.0 == res)
def
test_grad
(
self
):
#
def test_grad(self):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
#
x, y, z = T.matrices('xyz')
e
=
x
+
y
*
z
#
e = x + y * z
op
=
OpFromGraph
([
x
,
y
,
z
],
[
e
],
linker
=
'c|py'
,
grad_depth
=
2
)
#
op = OpFromGraph([x, y, z], [e], linker='c|py', grad_depth = 2)
f
=
op
(
x
,
y
,
z
)
#
f = op(x, y, z)
f
=
f
-
T
.
grad
(
f
,
y
)
#
f = f - T.grad(f, y)
fn
=
function
([
x
,
y
,
z
],
[
f
])
#
fn = function([x, y, z], [f])
xv
,
yv
,
zv
=
N
.
ones
((
2
,
2
)),
N
.
ones
((
2
,
2
))
*
3
,
N
.
ones
((
2
,
2
))
*
5
#
xv, yv, zv = N.ones((2, 2)), N.ones((2, 2))*3, N.ones((2, 2))*5
assert
numpy
.
all
(
11.0
==
fn
(
xv
,
yv
,
zv
))
#
assert numpy.all(11.0 == fn(xv, yv, zv))
class
T_function
(
unittest
.
TestCase
):
class
T_function
(
unittest
.
TestCase
):
def
test_empty
(
self
):
def
test_empty
(
self
):
fn
=
function
([],
[])
#ok
fn
=
function
([],
[])
#ok
self
.
fail
u
nless
(
fn
()
==
[])
self
.
fail
U
nless
(
fn
()
==
[])
def
test_missing_inputs
(
self
):
def
test_missing_inputs
(
self
):
raise
NotImplementedError
()
MissingInputException
=
TypeError
MissingInputException
=
None
def
fn
():
def
fn
():
x
,
s
=
T
.
scalars
(
'xs'
)
x
,
s
=
T
.
scalars
(
'xs'
)
...
@@ -234,18 +234,6 @@ class T_function(unittest.TestCase):
...
@@ -234,18 +234,6 @@ class T_function(unittest.TestCase):
fn
=
function
([
s
,
x
],
x
+
s
)
fn
=
function
([
s
,
x
],
x
+
s
)
self
.
failUnless
(
fn
(
2
,
3
)
==
5
)
self
.
failUnless
(
fn
(
2
,
3
)
==
5
)
def
test_eq
(
self
):
x
,
s
=
T
.
scalars
(
'xs'
)
xx
,
ss
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
s
],
x
+
s
)
ff
=
function
([
xx
,
ss
],
xx
+
ss
)
self
.
failUnless
(
f
==
ff
)
self
.
failUnless
(
f
!=
function
([
x
,
s
],
x
-
s
))
self
.
failUnless
(
ff
!=
function
([
x
,
s
],
x
-
s
))
def
test_naming_rule0
(
self
):
def
test_naming_rule0
(
self
):
x
,
s
=
T
.
scalars
(
'xs'
)
x
,
s
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
s
],
x
/
s
)
f
=
function
([
x
,
s
],
x
/
s
)
...
@@ -309,17 +297,17 @@ class T_function(unittest.TestCase):
...
@@ -309,17 +297,17 @@ class T_function(unittest.TestCase):
checkfor
(
self
,
lambda
:
f
(),
TypeError
)
#takes exactly 3 non-keyword arguments (0 given)
checkfor
(
self
,
lambda
:
f
(),
TypeError
)
#takes exactly 3 non-keyword arguments (0 given)
checkfor
(
self
,
lambda
:
f
(
5.0
,
x
=
9
),
TypeError
)
#got multiple values for keyword argument 'x'
checkfor
(
self
,
lambda
:
f
(
5.0
,
x
=
9
),
TypeError
)
#got multiple values for keyword argument 'x'
def
test_state_acces
(
self
):
def
test_state_acces
s
(
self
):
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
x
,
s
=
T
.
scalars
(
'xs'
)
x
,
s
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
)],
s
+
a
*
x
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
)],
s
+
a
*
x
)
self
.
failUnless
(
f
.
a
==
1.0
)
self
.
failUnless
(
f
.
a
==
1.0
)
self
.
failUnless
(
f
.
stat
e
[
a
]
is
f
.
a
)
self
.
failUnless
(
f
.
valu
e
[
a
]
is
f
.
a
)
self
.
failUnless
(
f
.
s
==
0.0
)
self
.
failUnless
(
f
.
s
==
0.0
)
self
.
failUnless
(
f
.
stat
e
[
s
]
is
f
.
s
)
self
.
failUnless
(
f
.
valu
e
[
s
]
is
f
.
s
)
self
.
failUnless
(
f
(
3.0
)
==
3.0
)
self
.
failUnless
(
f
(
3.0
)
==
3.0
)
self
.
failUnless
(
f
(
3.0
,
a
=
2.0
)
==
9.0
)
#3.0 + 2*3.0
self
.
failUnless
(
f
(
3.0
,
a
=
2.0
)
==
9.0
)
#3.0 + 2*3.0
...
@@ -329,25 +317,28 @@ class T_function(unittest.TestCase):
...
@@ -329,25 +317,28 @@ class T_function(unittest.TestCase):
f
.
a
=
5.0
f
.
a
=
5.0
self
.
failUnless
(
f
.
a
==
5.0
)
self
.
failUnless
(
f
.
a
==
5.0
)
self
.
failUnless
(
f
.
stat
e
[
a
]
is
f
.
a
)
self
.
failUnless
(
f
.
valu
e
[
a
]
is
f
.
a
)
self
.
failUnless
(
f
(
3.0
)
==
24.0
)
#9 + 3*5
self
.
failUnless
(
f
(
3.0
)
==
24.0
)
#9 + 3*5
self
.
failUnless
(
f
.
s
==
24.0
)
self
.
failUnless
(
f
.
s
==
24.0
)
self
.
failUnless
(
f
.
stat
e
[
s
]
is
f
.
s
)
self
.
failUnless
(
f
.
valu
e
[
s
]
is
f
.
s
)
def
test_same_names
(
self
):
def
test_same_names
(
self
):
a
,
x
,
s
=
T
.
scalars
(
'xxx'
)
a
,
x
,
s
=
T
.
scalars
(
'xxx'
)
#implicit names would cause error. What do we do?
#implicit names would cause error. What do we do?
f
=
function
([
a
,
x
,
s
],
a
+
x
+
s
)
f
=
function
([
a
,
x
,
s
],
a
+
x
+
s
)
raise
NotImplementedError
()
self
.
failUnless
(
f
(
1
,
2
,
3
)
==
6
)
checkfor
(
self
,
lambda
:
f
(
1
,
2
,
x
=
3
),
TypeError
)
def
test_weird_names
(
self
):
def
test_weird_names
(
self
):
a
,
x
,
s
=
T
.
scalars
(
'xxx'
)
a
,
x
,
s
=
T
.
scalars
(
'xxx'
)
checkfor
(
self
,
lambda
:
function
([
In
(
a
,
name
=
[])],[]),
UnhashableName
)
checkfor
(
self
,
lambda
:
function
([
In
(
a
,
name
=
[])],[]),
TypeError
)
f
=
function
([
In
(
a
,
name
=
set
([
'adsf'
,()]),
value
=
1.0
),
def
t
():
In
(
x
,
name
=
(),
value
=
2.0
),
f
=
function
([
In
(
a
,
name
=
set
([
'adsf'
,()]),
value
=
1.0
),
In
(
s
,
name
=
T
.
scalar
(),
value
=
3.0
)],
a
+
x
+
s
)
In
(
x
,
name
=
(),
value
=
2.0
),
In
(
s
,
name
=
T
.
scalar
(),
value
=
3.0
)],
a
+
x
+
s
)
checkfor
(
self
,
t
,
TypeError
)
def
test_copy
(
self
):
def
test_copy
(
self
):
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
...
@@ -355,25 +346,21 @@ class T_function(unittest.TestCase):
...
@@ -355,25 +346,21 @@ class T_function(unittest.TestCase):
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
g
=
copy
.
copy
(
f
)
g
=
copy
(
f
)
g
=
f
.
copy
()
#if they both return, assume that they return equivalent things.
#if they both return, assume that they return equivalent things.
self
.
failUnless
(
len
(
g
.
container
)
==
3
)
self
.
failIf
(
g
.
container
[
x
]
.
storage
is
f
.
container
[
x
]
.
storage
)
self
.
failUnless
(
len
(
g
.
state
)
==
3
)
self
.
failIf
(
g
.
container
[
a
]
.
storage
is
f
.
container
[
a
]
.
storage
)
self
.
failIf
(
g
.
container
[
s
]
.
storage
is
f
.
container
[
s
]
.
storage
)
self
.
failIf
(
g
.
container
[
x
]
is
f
.
container
[
x
])
self
.
failIf
(
g
.
value
[
a
]
is
not
f
.
value
[
a
])
# should not have been copied
self
.
failIf
(
g
.
container
[
a
]
is
f
.
container
[
a
])
self
.
failIf
(
g
.
value
[
s
]
is
f
.
value
[
s
])
# should have been copied because it is mutable.
self
.
failIf
(
g
.
container
[
s
]
is
f
.
container
[
s
])
self
.
failIf
(
(
g
.
value
[
s
]
!=
f
.
value
[
s
])
.
any
())
# its contents should be identical
self
.
failIf
(
g
.
state
[
x
]
is
not
f
.
state
[
x
])
# should not have been copied
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
2
))
#they should be in sync, default value should be copied.
self
.
failIf
(
g
.
state
[
a
]
is
not
f
.
state
[
a
])
# should not have been copied
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
2
))
#they should be in sync, default value should be copied.
self
.
failIf
(
g
.
state
[
s
]
is
f
.
state
[
s
])
# should have been copied because it is mutable.
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
1
))
#they should be in sync, default value should be copied.
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
1
))
#they should be in sync, default value should be copied.
f
(
1
,
2
)
# put them out of sync
f
(
1
,
2
)
# put them out of sync
self
.
failIf
(
f
(
1
,
2
)
==
g
(
1
,
2
))
#they should be equal anymore.
self
.
failIf
(
f
(
1
,
2
)
==
g
(
1
,
2
))
#they should
not
be equal anymore.
def
test_shared_state0
(
self
):
def
test_shared_state0
(
self
):
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
...
@@ -389,53 +376,6 @@ class T_function(unittest.TestCase):
...
@@ -389,53 +376,6 @@ class T_function(unittest.TestCase):
self
.
failUnless
(
f
.
s
==
0
)
self
.
failUnless
(
f
.
s
==
0
)
self
.
failUnless
(
g
.
s
==
0
)
self
.
failUnless
(
g
.
s
==
0
)
def
test_shared_state1
(
self
):
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
x
,
s
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
g
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
99.0
,
update
=
s
-
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
g
.
container
[
s
]
=
f
.
container
[
s
]
f
(
1
,
2
)
self
.
failUnless
(
f
.
s
==
2
)
self
.
failUnless
(
g
.
s
==
2
)
g
(
1
,
2
)
self
.
failUnless
(
f
.
s
==
0
)
self
.
failUnless
(
g
.
s
==
0
)
def
test_autoname
(
self
):
raise
NotImplementedError
()
def
test_modes
(
self
):
raise
NotImplementedError
()
def
test_modes_duallinker
(
self
):
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
x
,
s
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
f
.
append_mode
(
'c&py'
)
f
.
append_mode
(
custom_mode
)
f
(
1
,
2
)
raise
NotImplementedError
()
def
test_modes_dual_with_error
(
self
):
custom_broken_mode
=
None
a
=
T
.
scalar
()
# the a is for 'anonymous' (un-named).
x
,
s
=
T
.
scalars
(
'xs'
)
f
=
function
([
x
,
In
(
a
,
value
=
1.0
,
name
=
'a'
),
In
(
s
,
value
=
0.0
,
update
=
s
+
a
*
x
,
mutable
=
True
)],
s
+
a
*
x
)
f
.
append_mode
(
custom_broken_mode
)
checkfor
(
self
,
lambda
:
f
(
1
,
2
),
SomeException
)
raise
NotImplementedError
()
class
T_function_examples
(
unittest
.
TestCase
):
class
T_function_examples
(
unittest
.
TestCase
):
def
test_accumulator
(
self
):
def
test_accumulator
(
self
):
...
@@ -538,64 +478,64 @@ class T_function_examples(unittest.TestCase):
...
@@ -538,64 +478,64 @@ class T_function_examples(unittest.TestCase):
print
'errs ='
,
errs
print
'errs ='
,
errs
class
T_dict_interface
(
unittest
.
TestCase
):
#
class T_dict_interface(unittest.TestCase):
def
test_keyword
(
self
):
#
def test_keyword(self):
x
=
T
.
scalar
(
'x'
)
#
x = T.scalar('x')
y
=
T
.
scalar
(
'y'
)
#
y = T.scalar('y')
s
=
T
.
scalar
(
's'
)
#
s = T.scalar('s')
fn
=
function
(
input_kw
=
{
'a'
:
x
,
'b'
:
y
},
outputs
=
[],
state
=
{
's'
:(
s
,
0
,
s
+
x
/
y
)})
#
fn = function(input_kw = {'a':x, 'b':y}, outputs = [], state = {'s':(s, 0, s+x/y)})
try
:
#
try:
fn
(
1
,
1
)
#
fn(1, 1)
self
.
fail
(
"non-keyword call accepted!"
)
#
self.fail("non-keyword call accepted!")
except
SpecificException
:
#
except SpecificException:
raise
NotImplementedError
()
#
raise NotImplementedError()
except
Exception
:
#
except Exception:
self
.
fail
(
"non-keyword call accepted!"
)
#
self.fail("non-keyword call accepted!")
try
:
#
try:
fn
(
a
=
1
)
#
fn(a=1)
self
.
fail
(
"incomplete call accepted!"
)
#
self.fail("incomplete call accepted!")
except
SpecificException
:
#
except SpecificException:
raise
NotImplementedError
()
#
raise NotImplementedError()
except
Exception
:
#
except Exception:
self
.
fail
(
"incomplete call accepted!"
)
#
self.fail("incomplete call accepted!")
try
:
#
try:
fn
(
a
=
1
,
b
=
1
,
c
=
1
)
#
fn(a=1, b=1, c=1)
self
.
fail
(
"overcomplete call accepted!"
)
#
self.fail("overcomplete call accepted!")
except
SpecificException
:
#
except SpecificException:
raise
NotImplementedError
()
#
raise NotImplementedError()
except
Exception
:
#
except Exception:
self
.
fail
(
"overcomplete call accepted!"
)
#
self.fail("overcomplete call accepted!")
def
test_aliased_state
(
self
):
#
def test_aliased_state(self):
"""Test keyword input and copy."""
#
"""Test keyword input and copy."""
x
=
T
.
scalar
(
'x'
)
#
x = T.scalar('x')
y
=
T
.
scalar
(
'y'
)
#
y = T.scalar('y')
s
=
T
.
scalar
(
's'
)
#
s = T.scalar('s')
fn
=
function
(
input_kw
=
{
'a'
:
x
,
'b'
:
y
},
outputs
=
[],
state
=
{
's'
:(
s
,
0
,
s
+
x
/
y
)})
#
fn = function(input_kw = {'a':x, 'b':y}, outputs = [], state = {'s':(s, 0, s+x/y)})
fn2
=
fn
.
copy
()
#
fn2 = fn.copy()
fn3
=
fn
.
copy
()
#
fn3 = fn.copy()
fn
(
a
=
2
,
b
=
5
)
#
fn(a=2, b=5)
fn2
(
a
=
5
,
b
=
2
)
#
fn2(a=5, b=2)
fn3
(
b
=
2
,
a
=
5
)
#
fn3(b=2, a=5)
assert
fn
.
state
[
's'
]
==
2.0
/
5
#
assert fn.state['s'] == 2.0/5
assert
fn2
.
state
[
's'
]
==
5.0
/
2
#
assert fn2.state['s'] == 5.0/2
assert
fn3
.
state
[
's'
]
==
5.0
/
2
#
assert fn3.state['s'] == 5.0/2
#fn and fn3 use the same sort of state, so this is OK.
#
#fn and fn3 use the same sort of state, so this is OK.
fn3
.
state
=
fn
.
state
#
fn3.state = fn.state
fn
.
state
[
's'
]
=
0
#
fn.state['s'] = 0
fn
(
a
=
1
,
b
=
1
)
#increment the shared state
#
fn(a=1, b=1) #increment the shared state
assert
fn3
.
state
[
's'
]
==
1
#
assert fn3.state['s'] == 1
fn3
(
a
=-
1
,
b
=
1
)
#decrement the shared state
#
fn3(a=-1, b=1) #decrement the shared state
assert
fn
.
state
[
's'
]
==
0
#
assert fn.state['s'] == 0
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
gof/graph.py
浏览文件 @
0d8dc459
...
@@ -358,8 +358,6 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True):
...
@@ -358,8 +358,6 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True):
else
:
else
:
d
[
input
]
=
input
d
[
input
]
=
input
for
apply
in
io_toposort
(
i
,
o
):
for
apply
in
io_toposort
(
i
,
o
):
for
input
in
apply
.
inputs
:
for
input
in
apply
.
inputs
:
if
input
not
in
d
:
if
input
not
in
d
:
...
@@ -374,6 +372,10 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True):
...
@@ -374,6 +372,10 @@ def clone_get_equiv(i, o, copy_inputs_and_orphans = True):
for
output
,
new_output
in
zip
(
apply
.
outputs
,
new_apply
.
outputs
):
for
output
,
new_output
in
zip
(
apply
.
outputs
,
new_apply
.
outputs
):
d
[
output
]
=
new_output
d
[
output
]
=
new_output
for
output
in
o
:
if
output
not
in
d
:
d
[
output
]
=
output
.
clone
()
return
d
return
d
def
general_toposort
(
r_out
,
deps
,
debug_print
=
False
):
def
general_toposort
(
r_out
,
deps
,
debug_print
=
False
):
...
...
tensor_random.py
浏览文件 @
0d8dc459
...
@@ -113,11 +113,16 @@ from collections import deque
...
@@ -113,11 +113,16 @@ from collections import deque
class
RandomKit
(
SymbolicInputKit
):
class
RandomKit
(
SymbolicInputKit
):
def
__init__
(
self
,
name
,
value
=
None
):
super
(
RandomKit
,
self
)
.
__init__
(
name
)
self
.
value
=
value
def
gen
(
self
,
op
,
*
args
,
**
kwargs
):
def
gen
(
self
,
op
,
*
args
,
**
kwargs
):
r
=
gof
.
generic
()
r
=
gof
.
generic
()
new_r
,
out
=
op
(
r
,
*
args
,
**
kwargs
)
new_r
,
out
=
op
(
r
,
*
args
,
**
kwargs
)
self
.
add_input
(
SymbolicInput
(
r
,
update
=
new_r
))
self
.
add_input
(
SymbolicInput
(
r
,
update
=
new_r
))
out
.
rng
=
r
out
.
rng
=
r
out
.
auto
=
self
return
out
return
out
def
distribute
(
self
,
value
,
indices
,
containers
):
def
distribute
(
self
,
value
,
indices
,
containers
):
...
@@ -135,7 +140,18 @@ class RandomKit(SymbolicInputKit):
...
@@ -135,7 +140,18 @@ class RandomKit(SymbolicInputKit):
def
binomial
(
self
,
*
args
,
**
kwargs
):
def
binomial
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
binomial
,
*
args
,
**
kwargs
)
return
self
.
gen
(
binomial
,
*
args
,
**
kwargs
)
rk
=
RandomKit
(
'rk'
)
def
uniform
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
uniform
,
*
args
,
**
kwargs
)
def
normal
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
normal
,
*
args
,
**
kwargs
)
def
random_integers
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
random_integers
,
*
args
,
**
kwargs
)
rk
=
RandomKit
(
'rk'
,
0xBAD5EED
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论