Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2d3cb775
提交
2d3cb775
authored
4月 19, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
4月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow customizable object naming in aesara.link.utils.fgraph_to_python
上级
b49bb6f8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
24 行增加
和
10 行删除
+24
-10
utils.py
aesara/link/utils.py
+20
-9
test_utils.py
tests/link/test_utils.py
+4
-1
没有找到文件。
aesara/link/utils.py
浏览文件 @
2d3cb775
...
...
@@ -590,6 +590,21 @@ def compile_function_src(src, function_name, global_env=None, local_env=None):
return
local_env
[
function_name
]
def
get_name_for_object
(
x
:
Any
):
"""Get the name for an arbitrary object."""
if
isinstance
(
x
,
Variable
):
name
=
re
.
sub
(
"[^0-9a-zA-Z]+"
,
"_"
,
x
.
name
)
if
x
.
name
else
""
name
=
name
if
(
name
.
isidentifier
()
and
not
iskeyword
(
name
))
else
x
.
auto_name
else
:
name
=
getattr
(
x
,
"__name__"
,
None
)
if
not
name
or
(
not
name
.
isidentifier
()
or
iskeyword
(
name
)):
name
=
type
(
x
)
.
__name__
return
name
def
fgraph_to_python
(
fgraph
:
FunctionGraph
,
op_conversion_fn
:
Callable
,
...
...
@@ -602,6 +617,7 @@ def fgraph_to_python(
fgraph_name
:
str
=
"fgraph_to_python"
,
global_env
:
Optional
[
Dict
[
Any
,
Any
]]
=
None
,
local_env
:
Optional
[
Dict
[
Any
,
Any
]]
=
None
,
get_name_for_object
:
Callable
[[
Any
],
str
]
=
get_name_for_object
,
**
kwargs
,
)
->
FunctionType
:
"""Convert a ``FunctionGraph`` into a regular Python function.
...
...
@@ -634,6 +650,9 @@ def fgraph_to_python(
local_env
The local environment used when the function is constructed.
The default is ``locals()``.
get_name_for_object
A function used to provide names for the objects referenced within the
generated function.
**kwargs
The remaining keywords are passed to `python_conversion_fn`
"""
...
...
@@ -648,15 +667,7 @@ def fgraph_to_python(
if
x
in
obj_to_names
:
return
obj_to_names
[
x
]
if
isinstance
(
x
,
Variable
):
name
=
re
.
sub
(
"[^0-9a-zA-Z]+"
,
"_"
,
x
.
name
)
if
x
.
name
else
""
name
=
(
name
if
(
name
.
isidentifier
()
and
not
iskeyword
(
name
))
else
x
.
auto_name
)
elif
isinstance
(
x
,
FunctionType
):
name
=
x
.
__name__
else
:
name
=
type
(
x
)
.
__name__
name
=
get_name_for_object
(
x
)
name_suffix
=
names_counter
.
get
(
name
,
""
)
local_name
=
f
"{name}{name_suffix}"
...
...
tests/link/test_utils.py
浏览文件 @
2d3cb775
...
...
@@ -6,7 +6,7 @@ from aesara import config
from
aesara.graph.basic
import
Apply
from
aesara.graph.fg
import
FunctionGraph
from
aesara.graph.op
import
Op
from
aesara.link.utils
import
fgraph_to_python
from
aesara.link.utils
import
fgraph_to_python
,
get_name_for_object
from
aesara.scalar.basic
import
Add
from
aesara.tensor.elemwise
import
Elemwise
from
aesara.tensor.type
import
scalar
,
vector
...
...
@@ -50,6 +50,9 @@ def test_fgraph_to_python_names():
)
assert
(
1
,
2
,
3
,
4
,
5
)
==
out_jx
(
1
,
2
,
3
,
4
,
5
)
obj
=
object
()
assert
get_name_for_object
(
obj
)
==
type
(
obj
)
.
__name__
def
test_fgraph_to_python_once
():
"""Make sure that an output is only computed once when it's referenced multiple times."""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论