Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
62651c4a
提交
62651c4a
authored
5月 22, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
5月 22, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add aesara.graph.opt_utils.optimize_graph function
上级
ea070b61
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
2 行删除
+68
-2
opt_utils.py
aesara/graph/opt_utils.py
+46
-1
test_opt_utils.py
tests/graph/test_opt_utils.py
+22
-1
没有找到文件。
aesara/graph/opt_utils.py
浏览文件 @
62651c4a
import
copy
import
copy
from
typing
import
Sequence
,
Union
import
aesara
import
aesara
from
aesara.graph.basic
import
equal_computations
,
graph_inputs
,
vars_between
from
aesara.graph.basic
import
Variable
,
equal_computations
,
graph_inputs
,
vars_between
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.optdb
import
Query
def
optimize_graph
(
fgraph
:
Union
[
Variable
,
FunctionGraph
],
include
:
Sequence
[
str
]
=
[
"canonicalize"
],
custom_opt
=
None
,
clone
:
bool
=
False
,
**
kwargs
)
->
Union
[
Variable
,
FunctionGraph
]:
"""Easily optimize a graph.
Parameters
==========
fgraph:
A ``FunctionGraph`` or ``Variable`` to be optimized.
include:
String names of the optimizations to be applied. The default
optimization is ``"canonicalization"``.
custom_opt:
A custom ``Optimization`` to also be applied.
clone:
Whether or not to clone the input graph before optimizing.
**kwargs:
Keyword arguments passed to the ``aesara.graph.optdb.Query`` object.
"""
from
aesara.compile
import
optdb
return_only_out
=
False
if
not
isinstance
(
fgraph
,
FunctionGraph
):
fgraph
=
FunctionGraph
(
outputs
=
[
fgraph
],
clone
=
clone
)
return_only_out
=
True
canonicalize_opt
=
optdb
.
query
(
Query
(
include
=
include
,
**
kwargs
))
_
=
canonicalize_opt
.
optimize
(
fgraph
)
if
custom_opt
:
custom_opt
.
optimize
(
fgraph
)
if
return_only_out
:
return
fgraph
.
outputs
[
0
]
else
:
return
fgraph
def
is_same_graph_with_merge
(
var1
,
var2
,
givens
=
None
):
def
is_same_graph_with_merge
(
var1
,
var2
,
givens
=
None
):
...
...
tests/graph/test_opt_utils.py
浏览文件 @
62651c4a
from
aesara.graph.opt_utils
import
is_same_graph
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.opt
import
optimizer
from
aesara.graph.opt_utils
import
is_same_graph
,
optimize_graph
from
aesara.tensor.math
import
neg
from
aesara.tensor.math
import
neg
from
aesara.tensor.type
import
vectors
from
aesara.tensor.type
import
vectors
...
@@ -135,3 +137,22 @@ class TestIsSameGraph:
...
@@ -135,3 +137,22 @@ class TestIsSameGraph:
),
),
],
],
)
)
def
test_optimize_graph
():
x
,
y
=
vectors
(
"xy"
)
@optimizer
def
custom_opt
(
fgraph
):
fgraph
.
replace
(
x
,
y
,
import_missing
=
True
)
x_opt
=
optimize_graph
(
x
,
custom_opt
=
custom_opt
)
assert
x_opt
is
y
x_opt
=
optimize_graph
(
FunctionGraph
(
outputs
=
[
x
],
clone
=
False
),
custom_opt
=
custom_opt
)
assert
x_opt
.
outputs
[
0
]
is
y
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论