Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
044910bf
Unverified
提交
044910bf
authored
4月 30, 2024
作者:
Dhruvanshu-Joshi
提交者:
GitHub
4月 30, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add helper `explicit_graph_inputs` (#712)
上级
27bd9aaf
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
64 行增加
和
0 行删除
+64
-0
basic.py
pytensor/graph/basic.py
+49
-0
test_basic.py
tests/graph/test_basic.py
+15
-0
没有找到文件。
pytensor/graph/basic.py
浏览文件 @
044910bf
...
@@ -936,6 +936,55 @@ def graph_inputs(
...
@@ -936,6 +936,55 @@ def graph_inputs(
yield
from
(
r
for
r
in
ancestors
(
graphs
,
blockers
)
if
r
.
owner
is
None
)
yield
from
(
r
for
r
in
ancestors
(
graphs
,
blockers
)
if
r
.
owner
is
None
)
def
explicit_graph_inputs
(
graph
:
Variable
|
Iterable
[
Variable
],
)
->
Generator
[
Variable
,
None
,
None
]:
"""
Get the root variables needed as inputs to a function that computes `graph`
Parameters
----------
graph : TensorVariable
Output `Variable` instances for which to search backward through
owners.
Returns
-------
iterable
Generator of root Variables (without owner) needed to compile a function that evaluates `graphs`.
Examples
--------
.. code-block:: python
import pytensor
import pytensor.tensor as pt
from pytensor.graph.basic import explicit_graph_inputs
x = pt.vector('x')
y = pt.constant(2)
z = pt.mul(x*y)
inputs = list(explicit_graph_inputs(z))
f = pytensor.function(inputs, z)
eval = f([1, 2, 3])
print(eval)
# [2. 4. 6.]
"""
from
pytensor.compile.sharedvalue
import
SharedVariable
if
isinstance
(
graph
,
Variable
):
graph
=
[
graph
]
return
(
v
for
v
in
graph_inputs
(
graph
)
if
isinstance
(
v
,
Variable
)
and
not
isinstance
(
v
,
Constant
|
SharedVariable
)
)
def
vars_between
(
def
vars_between
(
ins
:
Collection
[
Variable
],
outs
:
Iterable
[
Variable
]
ins
:
Collection
[
Variable
],
outs
:
Iterable
[
Variable
]
)
->
Generator
[
Variable
,
None
,
None
]:
)
->
Generator
[
Variable
,
None
,
None
]:
...
...
tests/graph/test_basic.py
浏览文件 @
044910bf
...
@@ -18,6 +18,7 @@ from pytensor.graph.basic import (
...
@@ -18,6 +18,7 @@ from pytensor.graph.basic import (
clone
,
clone
,
clone_get_equiv
,
clone_get_equiv
,
equal_computations
,
equal_computations
,
explicit_graph_inputs
,
general_toposort
,
general_toposort
,
get_var_by_name
,
get_var_by_name
,
graph_inputs
,
graph_inputs
,
...
@@ -522,6 +523,20 @@ def test_graph_inputs():
...
@@ -522,6 +523,20 @@ def test_graph_inputs():
assert
res_list
==
[
r3
,
r1
,
r2
]
assert
res_list
==
[
r3
,
r1
,
r2
]
def
test_explicit_graph_inputs
():
x
=
pt
.
fscalar
()
y
=
pt
.
constant
(
2
)
z
=
shared
(
1
)
a
=
pt
.
sum
(
x
+
y
+
z
)
b
=
pt
.
true_div
(
x
,
y
)
res
=
list
(
explicit_graph_inputs
([
a
]))
res1
=
list
(
explicit_graph_inputs
(
b
))
assert
res
==
[
x
]
assert
res1
==
[
x
]
def
test_variables_and_orphans
():
def
test_variables_and_orphans
():
r1
,
r2
,
r3
=
MyVariable
(
1
),
MyVariable
(
2
),
MyVariable
(
3
)
r1
,
r2
,
r3
=
MyVariable
(
1
),
MyVariable
(
2
),
MyVariable
(
3
)
o1
=
MyOp
(
r1
,
r2
)
o1
=
MyOp
(
r1
,
r2
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论