Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
68db91e0
提交
68db91e0
authored
3月 26, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added some pickling test cases to test_function
上级
96e48a0e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
75 行增加
和
9 行删除
+75
-9
function_module.py
theano/compile/function_module.py
+12
-9
test_function.py
theano/compile/tests/test_function.py
+63
-0
没有找到文件。
theano/compile/function_module.py
浏览文件 @
68db91e0
...
@@ -13,7 +13,7 @@ from .. import gof
...
@@ -13,7 +13,7 @@ from .. import gof
import
sys
import
sys
import
copy
import
copy
from
mode
import
*
import
mode
as
mode_module
from
io
import
*
from
io
import
*
def
infer_reuse_pattern
(
env
,
outputs_to_disown
):
def
infer_reuse_pattern
(
env
,
outputs_to_disown
):
...
@@ -551,7 +551,7 @@ class FunctionMaker(object):
...
@@ -551,7 +551,7 @@ class FunctionMaker(object):
raise
TypeError
(
"Unknown output type:
%
s (
%
s)"
,
type
(
output
),
output
)
raise
TypeError
(
"Unknown output type:
%
s (
%
s)"
,
type
(
output
),
output
)
def
__init__
(
self
,
inputs
,
outputs
,
def
__init__
(
self
,
inputs
,
outputs
,
mode
=
default_mod
e
,
accept_inplace
=
False
,
function_builder
=
Function
):
mode
=
Non
e
,
accept_inplace
=
False
,
function_builder
=
Function
):
"""
"""
:type inputs: a list of SymbolicInput instances
:type inputs: a list of SymbolicInput instances
...
@@ -560,12 +560,14 @@ class FunctionMaker(object):
...
@@ -560,12 +560,14 @@ class FunctionMaker(object):
case the functions produced by FunctionMaker will return
case the functions produced by FunctionMaker will return
their output value directly
their output value directly
:param mode: a Mode instance telling FunctionMaker how to optimize and link
:param mode: a Mode instance telling FunctionMaker how to optimize and link. None
means to use the `default_mode`.
:param accept_inplace: True iff it is acceptable to have inplace operations
:param accept_inplace: True iff it is acceptable to have inplace operations
in the graph from the inputs to the outputs
in the graph from the inputs to the outputs
"""
"""
mode
=
mode
if
mode
is
not
None
else
mode_module
.
default_mode
# Handle the case where inputs and/or outputs is a single Variable (not in a list)
# Handle the case where inputs and/or outputs is a single Variable (not in a list)
unpack_single
=
False
unpack_single
=
False
...
@@ -586,7 +588,7 @@ class FunctionMaker(object):
...
@@ -586,7 +588,7 @@ class FunctionMaker(object):
self
.
env
=
env
self
.
env
=
env
# Fetch the mode and then the optimizer and linker
# Fetch the mode and then the optimizer and linker
mode
=
predefined_modes
.
get
(
mode
,
mode
)
mode
=
mode_module
.
predefined_modes
.
get
(
mode
,
mode
)
optimizer
,
linker
=
mode
.
optimizer
,
copy
.
copy
(
mode
.
linker
)
optimizer
,
linker
=
mode
.
optimizer
,
copy
.
copy
(
mode
.
linker
)
# optimize the env
# optimize the env
...
@@ -595,7 +597,7 @@ class FunctionMaker(object):
...
@@ -595,7 +597,7 @@ class FunctionMaker(object):
# initialize the linker
# initialize the linker
if
not
hasattr
(
linker
,
'accept'
):
if
not
hasattr
(
linker
,
'accept'
):
raise
ValueError
(
"'linker' parameter of FunctionFactory should be a Linker with an accept method "
\
raise
ValueError
(
"'linker' parameter of FunctionFactory should be a Linker with an accept method "
\
"or one of
%
s"
%
predefined_linkers
.
keys
())
"or one of
%
s"
%
mode_module
.
predefined_linkers
.
keys
())
#the 'no_borrow' outputs are the ones for which that we can't return the internal storage pointer.
#the 'no_borrow' outputs are the ones for which that we can't return the internal storage pointer.
no_borrow
=
[
output
for
output
,
spec
in
zip
(
env
.
outputs
,
outputs
+
additional_outputs
)
if
not
spec
.
borrow
]
no_borrow
=
[
output
for
output
,
spec
in
zip
(
env
.
outputs
,
outputs
+
additional_outputs
)
if
not
spec
.
borrow
]
...
@@ -742,7 +744,7 @@ def register_checker(checker):
...
@@ -742,7 +744,7 @@ def register_checker(checker):
def
function
(
inputs
,
outputs
,
mode
=
default_mod
e
,
accept_inplace
=
False
):
def
function
(
inputs
,
outputs
,
mode
=
Non
e
,
accept_inplace
=
False
):
"""
"""
Return a function calculating the outputs from the inputs.
Return a function calculating the outputs from the inputs.
...
@@ -752,8 +754,8 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
...
@@ -752,8 +754,8 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
value of the returned function will match the format of this argument (either the value
value of the returned function will match the format of this argument (either the value
itself or a list of one or more return values)
itself or a list of one or more return values)
:param mode: a descriptive string or a Mode instance. (
See below for descriptive string
:param mode: a descriptive string or a Mode instance. (
Default of None means to use
list).
`mode.default_mode` (See below for descriptive string
list).
Currently, the library provides the following mode strings:
Currently, the library provides the following mode strings:
...
@@ -789,6 +791,7 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
...
@@ -789,6 +791,7 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
f[<kitname>] = seed #re-seed the elements of a RandomKit
f[<kitname>] = seed #re-seed the elements of a RandomKit
"""
"""
mode
=
mode
if
mode
is
not
None
else
mode_module
.
default_mode
inputs
=
map
(
convert_function_input
,
inputs
)
inputs
=
map
(
convert_function_input
,
inputs
)
if
outputs
is
None
:
if
outputs
is
None
:
...
@@ -798,7 +801,7 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
...
@@ -798,7 +801,7 @@ def function(inputs, outputs, mode=default_mode, accept_inplace = False):
defaults
=
[
getattr
(
input
,
'value'
,
None
)
for
input
in
inputs
]
defaults
=
[
getattr
(
input
,
'value'
,
None
)
for
input
in
inputs
]
mode
=
predefined_modes
.
get
(
mode
,
mode
)
mode
=
mode_module
.
predefined_modes
.
get
(
mode
,
mode
)
if
isinstance
(
mode
,
(
list
,
tuple
)):
# "mode comparison" semantics
if
isinstance
(
mode
,
(
list
,
tuple
)):
# "mode comparison" semantics
if
not
mode
:
if
not
mode
:
raise
ValueError
(
"Please provide at least one mode."
)
raise
ValueError
(
"Please provide at least one mode."
)
...
...
theano/compile/tests/test_function.py
浏览文件 @
68db91e0
...
@@ -408,6 +408,69 @@ class T_picklefunction(unittest.TestCase):
...
@@ -408,6 +408,69 @@ class T_picklefunction(unittest.TestCase):
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 not be equal anymore.
self
.
failIf
(
f
(
1
,
2
)
==
g
(
1
,
2
))
#they should not be equal anymore.
def
test_pickle
(
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
)
try
:
g
=
cPickle
.
loads
(
cPickle
.
dumps
(
f
))
except
NotImplementedError
,
e
:
if
e
[
0
]
.
startswith
(
'DebugMode is not picklable'
):
return
else
:
raise
#if they both return, assume that they return equivalent things.
#print [(k,id(k)) for k in f.finder.keys()]
#print [(k,id(k)) for k in g.finder.keys()]
self
.
failIf
(
g
.
container
[
0
]
.
storage
is
f
.
container
[
0
]
.
storage
)
self
.
failIf
(
g
.
container
[
1
]
.
storage
is
f
.
container
[
1
]
.
storage
)
self
.
failIf
(
g
.
container
[
2
]
.
storage
is
f
.
container
[
2
]
.
storage
)
self
.
failIf
(
x
in
g
.
container
)
self
.
failIf
(
x
in
g
.
value
)
self
.
failIf
(
g
.
value
[
1
]
is
f
.
value
[
1
])
# should not have been copied
self
.
failIf
(
g
.
value
[
2
]
is
f
.
value
[
2
])
# should have been copied because it is mutable.
self
.
failIf
((
g
.
value
[
2
]
!=
f
.
value
[
2
])
.
any
())
# its contents should be identical
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
2
))
#they should be in sync, default value should be copied.
self
.
failUnless
(
f
(
2
,
1
)
==
g
(
2
))
#they should be in sync, default value should be copied.
f
(
1
,
2
)
# put them out of sync
self
.
failIf
(
f
(
1
,
2
)
==
g
(
1
,
2
))
#they should not be equal anymore.
def
test_optimizations_preserved
(
self
):
a
=
T
.
dvector
()
# the a is for 'anonymous' (un-named).
x
=
T
.
dvector
(
'x'
)
s
=
T
.
dvector
(
's'
)
xm
=
T
.
dmatrix
(
'x'
)
sm
=
T
.
dmatrix
(
's'
)
f
=
function
([
a
,
x
,
s
,
xm
,
sm
],
((
a
.
T
.
T
)
*
(
tensor
.
dot
(
xm
,
(
sm
.
T
.
T
.
T
))
+
x
)
.
T
*
(
x
/
x
)
+
s
))
old_default_mode
=
compile
.
mode
.
default_mode
try
:
str_f
=
cPickle
.
dumps
(
f
)
compile
.
mode
.
default_mode
=
mode_module
.
Mode
(
linker
=
'py'
,
optimizer
=
None
)
g
=
cPickle
.
loads
(
str_f
)
#print g.maker.mode
#print compile.mode.default_mode
finally
:
compile
.
mode
.
default_mode
=
old_default_mode
assert
f
.
maker
is
not
g
.
maker
assert
f
.
maker
.
env
is
not
g
.
maker
.
env
tf
=
f
.
maker
.
env
.
toposort
()
tg
=
f
.
maker
.
env
.
toposort
()
assert
len
(
tf
)
==
len
(
tg
)
for
nf
,
ng
in
zip
(
tf
,
tg
):
assert
nf
.
op
==
ng
.
op
assert
len
(
nf
.
inputs
)
==
len
(
ng
.
inputs
)
assert
len
(
nf
.
outputs
)
==
len
(
ng
.
outputs
)
assert
[
i
.
type
for
i
in
nf
.
inputs
]
==
[
i
.
type
for
i
in
ng
.
inputs
]
assert
[
i
.
type
for
i
in
nf
.
outputs
]
==
[
i
.
type
for
i
in
ng
.
outputs
]
# class T_function_examples(unittest.TestCase):
# class T_function_examples(unittest.TestCase):
# def test_accumulator(self):
# def test_accumulator(self):
# """Test low-level interface with state."""
# """Test low-level interface with state."""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论