Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8c94ba0c
提交
8c94ba0c
authored
6月 20, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added skip_identities code to PatternSub
上级
a7e855b6
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
22 行增加
和
6 行删除
+22
-6
opt.py
theano/gof/opt.py
+22
-6
没有找到文件。
theano/gof/opt.py
浏览文件 @
8c94ba0c
...
...
@@ -525,7 +525,8 @@ class PatternSub(LocalOptimizer):
(scrabble, 'x'))
"""
def
__init__
(
self
,
in_pattern
,
out_pattern
,
allow_multiple_clients
=
False
):
def
__init__
(
self
,
in_pattern
,
out_pattern
,
allow_multiple_clients
=
False
,
skip_identities_fn
=
None
):
"""
Creates a PatternSub that replaces occurrences of
in_pattern by occurrences of out_pattern.
...
...
@@ -543,7 +544,12 @@ class PatternSub(LocalOptimizer):
raise
TypeError
(
"The pattern to search for must start with a specific Op instance."
)
self
.
__doc__
=
self
.
__class__
.
__doc__
+
"
\n\n
This instance does: "
+
str
(
self
)
+
"
\n
"
self
.
allow_multiple_clients
=
allow_multiple_clients
self
.
skip_identities_fn
=
skip_identities_fn
def
skip_identities
(
self
,
expr
):
if
self
.
skip_identities_fn
:
return
self
.
skip_identities_fn
(
expr
)
def
op_key
(
self
):
return
self
.
op
...
...
@@ -568,13 +574,22 @@ class PatternSub(LocalOptimizer):
if
node
.
op
!=
self
.
op
:
return
False
def
match
(
pattern
,
expr
,
u
,
allow_multiple_clients
=
False
):
def
retry_with_equiv
():
expr_equiv
=
self
.
skip_identities
(
expr
)
if
expr_equiv
is
None
:
return
False
#TODO: Not sure how to handle multiple_clients flag
###print 'retrying match', pattern, expr_equiv
return
match
(
pattern
,
expr_equiv
,
u
,
allow_multiple_clients
=
allow_multiple_clients
)
if
isinstance
(
pattern
,
(
list
,
tuple
)):
if
expr
.
owner
is
None
:
return
False
if
not
(
expr
.
owner
.
op
==
pattern
[
0
])
or
(
not
allow_multiple_clients
and
len
(
expr
.
clients
)
>
1
):
return
False
return
retry_with_equiv
()
if
len
(
pattern
)
-
1
!=
len
(
expr
.
owner
.
inputs
):
return
False
return
retry_with_equiv
()
for
p
,
v
in
zip
(
pattern
[
1
:],
expr
.
owner
.
inputs
):
u
=
match
(
p
,
v
,
u
,
self
.
allow_multiple_clients
)
if
not
u
:
...
...
@@ -588,17 +603,17 @@ class PatternSub(LocalOptimizer):
if
constraint
(
expr
):
return
match
(
real_pattern
,
expr
,
u
,
pattern
.
get
(
'allow_multiple_clients'
,
False
))
else
:
return
False
return
retry_with_equiv
()
elif
isinstance
(
pattern
,
str
):
v
=
unify
.
Var
(
pattern
)
if
u
[
v
]
is
not
v
and
u
[
v
]
is
not
expr
:
return
False
return
retry_with_equiv
()
else
:
u
=
u
.
merge
(
expr
,
v
)
elif
isinstance
(
pattern
,
graph
.
Constant
)
and
isinstance
(
expr
,
graph
.
Constant
)
and
pattern
.
equals
(
expr
):
return
u
else
:
return
False
return
retry_with_equiv
()
return
u
def
build
(
pattern
,
u
):
...
...
@@ -614,6 +629,7 @@ class PatternSub(LocalOptimizer):
if
u
:
p
=
self
.
out_pattern
new
=
build
(
p
,
u
)
####print "PatternSub matched:", new
return
[
new
]
else
:
return
False
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论