Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c3106ad7
提交
c3106ad7
authored
2月 16, 2009
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
imported patch module2
上级
6d8b732e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
109 行增加
和
39 行删除
+109
-39
module.py
theano/compile/module.py
+105
-35
test_module.py
theano/compile/tests/test_module.py
+1
-1
raw_random.py
theano/tensor/raw_random.py
+1
-1
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+2
-2
没有找到文件。
theano/compile/module.py
浏览文件 @
c3106ad7
from
..
import
gof
from
.
.printing
import
pprint
from
theano
import
gof
from
theano
.printing
import
pprint
from
collections
import
defaultdict
from
itertools
import
chain
from
functools
import
partial
...
...
@@ -791,31 +791,29 @@ def wrap(x):
return
wrapper
(
x
)
return
x
def
dict_wrap
(
d
):
for
k
,
v
in
d
.
iteritems
():
d
[
k
]
=
wrap
(
v
)
return
d
# Result -> Member
register_wrapper
(
lambda
x
:
isinstance
(
x
,
gof
.
Result
)
and
not
x
.
owner
,
lambda
x
:
Member
(
x
))
# Result -> External
register_wrapper
(
lambda
x
:
isinstance
(
x
,
gof
.
Result
),
register_wrapper
(
lambda
x
:
isinstance
(
x
,
gof
.
Result
)
and
x
.
owner
,
lambda
x
:
External
(
x
))
# [Component1, Component2, ...] -> ComponentList(Component1, Component2, ...)
register_wrapper
(
lambda
x
:
isinstance
(
x
,
(
list
,
tuple
))
and
all
(
isinstance
(
r
,
Component
)
for
r
in
x
),
lambda
x
:
ComponentList
(
*
x
))
# [Result1, Result2, ...] -> ComponentList(Member(Result1), Member(Result2), ...)
# [[Result1], {Result2}, Result3...] -> ComponentList(Member(Result1), Member(Result2), ...)
register_wrapper
(
lambda
x
:
isinstance
(
x
,
(
list
,
tuple
))
\
and
all
(
isinstance
(
r
,
gof
.
Result
)
and
not
r
.
owner
for
r
in
x
),
lambda
x
:
ComponentList
(
*
map
(
Member
,
x
)))
#{ "name1":Result1,...} -> ComponentDict(Member(Result1),...)
def
dict_member
(
d
):
nd
=
{}
for
k
,
v
in
d
.
iteritems
():
nd
[
k
]
=
Member
(
v
)
return
nd
register_wrapper
(
lambda
x
:
isinstance
(
x
,
dict
)
\
and
all
(
isinstance
(
r
,
gof
.
Result
)
\
and
not
r
.
owner
for
r
in
x
.
itervalues
()),
lambda
x
:
ComponentDict
(
dict_member
(
x
)))
and
all
(
isinstance
(
r
,
(
gof
.
Result
,
Component
,
list
,
tuple
,
dict
))
for
r
in
x
),
lambda
x
:
ComponentList
(
*
map
(
wrap
,
x
)))
#{ "name1":{Component,Result,list,tuple,dict},...} -> ComponentDict({Component,Result,list,tuple,dict},...)
register_wrapper
(
lambda
x
:
isinstance
(
x
,
dict
)
\
and
all
(
isinstance
(
r
,
Component
)
for
r
in
x
.
itervalues
()),
lambda
x
:
ComponentDict
(
x
))
and
all
(
isinstance
(
r
,
(
Component
,
gof
.
Result
,
list
,
tuple
,
dict
)
)
for
r
in
x
.
itervalues
()),
lambda
x
:
ComponentDict
(
dict_wrap
(
x
)
))
class
Curry
:
def
__init__
(
self
,
obj
,
name
,
arg
):
...
...
@@ -869,13 +867,9 @@ class Module(ComponentDict):
if
attr
==
'_components'
and
'_components'
not
in
self
.
__dict__
:
self
.
__dict__
[
'_components'
]
=
{}
try
:
rval
=
self
[
attr
]
rval
=
self
.
__dict__
[
"local_attr"
]
[
attr
]
except
KeyError
:
raise
AttributeError
(
'
%
s has no
%
s attribute.'
%
(
self
.
__class__
,
attr
))
if
isinstance
(
rval
,
(
External
,
Member
)):
# Special treatment for External and Member, so that
# the user may use them to build graphs more easily.
return
rval
.
r
return
rval
def
__setattr__
(
self
,
attr
,
value
):
...
...
@@ -885,17 +879,35 @@ class Module(ComponentDict):
elif
attr
==
'name'
:
self
.
__set_name__
(
value
)
return
value
=
self
.
__wrapper__
(
value
)
try
:
self
[
attr
]
=
value
except
:
if
isinstance
(
value
,
Component
):
raise
def
remove_member
(
v
):
if
isinstance
(
v
,
(
Member
,
External
)):
return
v
.
r
elif
isinstance
(
v
,
(
gof
.
Result
,
Method
,
Module
)):
return
v
elif
isinstance
(
v
,(
int
,
bool
)):
return
v
elif
isinstance
(
v
,
(
list
,
tuple
)):
return
map
(
remove_member
,
v
)
elif
isinstance
(
v
,
dict
):
for
k
,
vv
in
v
.
iteritems
():
v
[
k
]
=
remove_member
(
vv
)
return
v
else
:
self
.
__dict__
[
attr
]
=
value
# raise NotImplementedError
# print "WARNING: unknow:",v
return
v
value
=
remove_member
(
value
)
if
not
hasattr
(
self
,
"local_attr"
):
self
.
__dict__
[
"local_attr"
]
=
{}
self
.
__dict__
[
"local_attr"
][
attr
]
=
value
def
build
(
self
,
mode
,
memo
):
for
k
,
v
in
self
.
local_attr
.
iteritems
():
self
.
__setattr__
(
k
,
v
)
inst
=
super
(
Module
,
self
)
.
build
(
mode
,
memo
)
for
method
in
dir
(
self
):
# Any method with a name like '_instance_XXX' is added to
...
...
@@ -911,6 +923,64 @@ class Module(ComponentDict):
for
name
,
value
in
chain
(
init
.
iteritems
(),
kwinit
.
iteritems
()):
inst
[
name
]
=
value
def
make_mi
(
self
,
*
args
,
**
kwargs
):
meth
=
[]
#we put the method after the member to be sure of the ordering.
for
k
,
v
in
self
.
local_attr
.
iteritems
():
if
isinstance
(
v
,
Module
):
v
=
v
.
make_mi
(
args
,
kwargs
)
if
isinstance
(
v
,
Method
):
meth
.
append
((
k
,
v
))
else
:
v
=
self
.
__wrapper__
(
v
)
try
:
self
[
k
]
=
v
except
:
if
isinstance
(
v
,
Component
):
raise
else
:
self
.
__dict__
[
k
]
=
v
# self.__setitem__(k,v)
for
k
,
v
in
meth
:
self
.
__setitem__
(
k
,
v
)
return
self
def
make
(
self
,
*
args
,
**
kwargs
):
"""
Allocates the necessary containers using allocate() and uses
build() to make an instance which will be returned. The
initialize() method of the instance will be called with the
arguments and the keyword arguments. If 'mode' is in the
keyword arguments it will be passed to build().
"""
self
.
make_mi
(
args
,
kwargs
)
mode
=
kwargs
.
pop
(
'mode'
,
'FAST_COMPILE'
)
rval
=
self
.
make_no_init
(
mode
)
if
hasattr
(
rval
,
'initialize'
):
rval
.
initialize
(
*
args
,
**
kwargs
)
return
rval
def
__str__
(
self
):
return
self
.
__class__
.
__name__
+
"(
%
s)"
%
', '
.
join
(
x
for
x
in
sorted
(
map
(
str
,
self
.
local_attr
))
if
x
[
0
]
!=
'_'
)
def
__get_name__
(
self
):
"""
Getter for self.name
"""
return
self
.
_name
def
__set_name__
(
self
,
name
):
"""
Setter for self.name
"""
self
.
_name
=
name
name
=
property
(
lambda
self
:
self
.
__get_name__
(),
lambda
self
,
value
:
self
.
__set_name__
(
value
),
"Contains the name of this Component"
)
FancyModule
=
Module
FancyModuleInstance
=
ModuleInstance
...
...
theano/compile/tests/test_module.py
浏览文件 @
c3106ad7
...
...
@@ -10,7 +10,7 @@ class T_test_module(unittest.TestCase):
def
test_whats_up_with_submembers
(
self
):
class
Blah
(
FancyModule
):
def
__init__
(
self
,
stepsize
):
super
(
Blah
,
self
)
.
__init__
(
self
)
super
(
Blah
,
self
)
.
__init__
()
self
.
stepsize
=
Member
(
T
.
value
(
stepsize
))
x
=
T
.
dscalar
()
...
...
theano/tensor/raw_random.py
浏览文件 @
c3106ad7
...
...
@@ -245,7 +245,7 @@ class RModule(compile.Module):
def
__init__
(
self
,
components
=
{},
**
kwcomponents
):
super
(
RModule
,
self
)
.
__init__
(
components
,
**
kwcomponents
)
self
.
random
=
RandomKit
(
'rkit'
)
self
.
_
components
[
'_rkit'
]
=
compile
.
KitComponent
(
self
.
random
)
self
.
_
rkit
=
compile
.
KitComponent
(
self
.
random
)
def
__wrapper__
(
self
,
x
):
x
=
compile
.
module
.
wrap
(
x
)
...
...
theano/tensor/tests/test_raw_random.py
浏览文件 @
c3106ad7
...
...
@@ -32,7 +32,7 @@ class T_test_module(unittest.TestCase):
"""
class
B
(
RModule
):
def
__init__
(
self
):
super
(
B
,
self
)
.
__init__
(
self
)
super
(
B
,
self
)
.
__init__
()
self
.
x
=
compile
.
Member
(
tensor
.
dvector
())
self
.
r
=
self
.
random
.
uniform
(
tensor
.
shape
(
self
.
x
))
...
...
@@ -40,7 +40,7 @@ class T_test_module(unittest.TestCase):
self
.
f
=
compile
.
Method
([
self
.
x
],
self
.
r
)
class
E
(
RModule
):
def
__init__
(
self
):
super
(
E
,
self
)
.
__init__
(
self
)
super
(
E
,
self
)
.
__init__
()
self
.
b
=
B
()
self
.
f
=
compile
.
Method
([
self
.
b
.
x
],
self
.
b
.
r
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论