Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
29153adc
提交
29153adc
authored
9月 18, 2021
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
9月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Create a function that gets variables in a graph by name
上级
d4696e6b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
103 行增加
和
1 行删除
+103
-1
basic.py
aesara/graph/basic.py
+37
-0
test_basic.py
tests/graph/test_basic.py
+66
-1
没有找到文件。
aesara/graph/basic.py
浏览文件 @
29153adc
...
...
@@ -1648,3 +1648,40 @@ def equal_computations(xs, ys, in_xs=None, in_ys=None):
return
False
return
True
def
get_var_by_name
(
graphs
:
Iterable
[
Variable
],
target_var_id
:
str
,
ids
:
str
=
"CHAR"
)
->
Tuple
[
Variable
]:
r"""Get variables in a graph using their names.
Parameters
----------
graphs:
The graph, or graphs, to search.
target_var_id:
The name to match against either ``Variable.name`` or
``Variable.auto_name``.
Returns
-------
A ``tuple`` containing all the `Variable`\s that match `target_var_id`.
"""
from
aesara.graph.op
import
HasInnerGraph
def
expand
(
r
):
if
r
.
owner
:
res
=
r
.
owner
.
inputs
if
isinstance
(
r
.
owner
.
op
,
HasInnerGraph
):
res
.
extend
(
r
.
owner
.
op
.
inner_outputs
)
return
res
results
=
()
for
var
in
walk
(
graphs
,
expand
,
False
):
if
target_var_id
==
var
.
name
or
target_var_id
==
var
.
auto_name
:
results
+=
(
var
,)
return
results
tests/graph/test_basic.py
浏览文件 @
29153adc
...
...
@@ -15,6 +15,7 @@ from aesara.graph.basic import (
clone
,
equal_computations
,
general_toposort
,
get_var_by_name
,
graph_inputs
,
io_toposort
,
is_in_ancestors
,
...
...
@@ -23,7 +24,7 @@ from aesara.graph.basic import (
vars_between
,
walk
,
)
from
aesara.graph.op
import
Op
from
aesara.graph.op
import
HasInnerGraph
,
Op
from
aesara.graph.type
import
Type
from
aesara.tensor.math
import
max_and_argmax
from
aesara.tensor.type
import
TensorType
,
iscalars
,
matrix
,
scalars
...
...
@@ -70,6 +71,36 @@ class MyOp(Op):
MyOp
=
MyOp
()
class
MyInnerGraphOp
(
Op
,
HasInnerGraph
):
__props__
=
()
def
__init__
(
self
,
inner_inputs
,
inner_outputs
):
self
.
_inner_inputs
=
inner_inputs
self
.
_inner_outputs
=
inner_outputs
def
make_node
(
self
,
*
inputs
):
for
input
in
inputs
:
assert
isinstance
(
input
,
Variable
)
assert
isinstance
(
input
.
type
,
MyType
)
outputs
=
[
MyVariable
(
sum
(
input
.
type
.
thingy
for
input
in
inputs
))]
return
Apply
(
self
,
list
(
inputs
),
outputs
)
def
perform
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
(
"No Python implementation available."
)
@property
def
fn
(
self
):
raise
NotImplementedError
(
"No Python implementation available."
)
@property
def
inner_inputs
(
self
):
return
self
.
_inner_inputs
@property
def
inner_outputs
(
self
):
return
self
.
_inner_outputs
class
X
:
def
leaf_formatter
(
self
,
leaf
):
return
str
(
leaf
.
type
)
...
...
@@ -472,3 +503,37 @@ def test_io_connection_pattern():
@pytest.mark.xfail
(
reason
=
"Not implemented"
)
def
test_view_roots
():
raise
AssertionError
()
def
test_get_var_by_name
():
r1
,
r2
,
r3
=
MyVariable
(
1
),
MyVariable
(
2
),
MyVariable
(
3
)
o1
=
MyOp
(
r1
,
r2
)
o1
.
name
=
"o1"
# Inner graph
igo_in_1
=
MyVariable
(
4
)
igo_in_2
=
MyVariable
(
5
)
igo_out_1
=
MyOp
(
igo_in_1
,
igo_in_2
)
igo_out_1
.
name
=
"igo1"
igo
=
MyInnerGraphOp
([
igo_in_1
,
igo_in_2
],
[
igo_out_1
])
o2
=
igo
(
r3
,
o1
)
o2
.
name
=
"o1"
res
=
get_var_by_name
([
o1
,
o2
],
"blah"
)
assert
res
==
()
res
=
get_var_by_name
([
o1
,
o2
],
"o1"
)
assert
set
(
res
)
==
{
o1
,
o2
}
(
res
,)
=
get_var_by_name
([
o1
,
o2
],
o1
.
auto_name
)
assert
res
==
o1
(
res
,)
=
get_var_by_name
([
o1
,
o2
],
"igo1"
)
assert
res
==
igo_out_1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论