Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
47efd4b1
提交
47efd4b1
authored
9月 07, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
9月 07, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow single variable output in vectorize
Also: * rename `vectorize` kwarg by `replace` * add test for multiple outputs
上级
30e08e23
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
75 行增加
和
10 行删除
+75
-10
replace.py
pytensor/graph/replace.py
+55
-8
blockwise.py
pytensor/tensor/blockwise.py
+1
-1
test_replace.py
tests/graph/test_replace.py
+19
-1
没有找到文件。
pytensor/graph/replace.py
浏览文件 @
47efd4b1
...
@@ -213,9 +213,26 @@ def vectorize_node(node: Apply, *batched_inputs) -> Apply:
...
@@ -213,9 +213,26 @@ def vectorize_node(node: Apply, *batched_inputs) -> Apply:
return
_vectorize_node
(
op
,
node
,
*
batched_inputs
)
return
_vectorize_node
(
op
,
node
,
*
batched_inputs
)
@overload
def
vectorize
(
outputs
:
Variable
,
replace
:
Mapping
[
Variable
,
Variable
],
)
->
Variable
:
...
@overload
def
vectorize
(
def
vectorize
(
outputs
:
Sequence
[
Variable
],
vectorize
:
Mapping
[
Variable
,
Variable
]
outputs
:
Sequence
[
Variable
],
replace
:
Mapping
[
Variable
,
Variable
],
)
->
Sequence
[
Variable
]:
)
->
Sequence
[
Variable
]:
...
def
vectorize
(
outputs
:
Union
[
Variable
,
Sequence
[
Variable
]],
replace
:
Mapping
[
Variable
,
Variable
],
)
->
Union
[
Variable
,
Sequence
[
Variable
]]:
"""Vectorize outputs graph given mapping from old variables to expanded counterparts version.
"""Vectorize outputs graph given mapping from old variables to expanded counterparts version.
Expanded dimensions must be on the left. Behavior is similar to the functional `numpy.vectorize`.
Expanded dimensions must be on the left. Behavior is similar to the functional `numpy.vectorize`.
...
@@ -235,20 +252,44 @@ def vectorize(
...
@@ -235,20 +252,44 @@ def vectorize(
# Vectorized graph
# Vectorized graph
new_x = pt.matrix("new_x")
new_x = pt.matrix("new_x")
[new_y] = vectorize([y],
{x: new_x})
new_y = vectorize(y, replace=
{x: new_x})
fn = pytensor.function([new_x], new_y)
fn = pytensor.function([new_x], new_y)
fn([[0, 1, 2], [2, 1, 0]])
fn([[0, 1, 2], [2, 1, 0]])
# array([[0.09003057, 0.24472847, 0.66524096],
# array([[0.09003057, 0.24472847, 0.66524096],
# [0.66524096, 0.24472847, 0.09003057]])
# [0.66524096, 0.24472847, 0.09003057]])
.. code-block:: python
import pytensor
import pytensor.tensor as pt
from pytensor.graph import vectorize
# Original graph
x = pt.vector("x")
y1 = x[0]
y2 = x[-1]
# Vectorized graph
new_x = pt.matrix("new_x")
[new_y1, new_y2] = vectorize([y1, y2], replace={x: new_x})
fn = pytensor.function([new_x], [new_y1, new_y2])
fn([[-10, 0, 10], [-11, 0, 11]])
# [array([-10., -11.]), array([10., 11.])]
"""
"""
# Avoid circular import
if
isinstance
(
outputs
,
Sequence
):
seq_outputs
=
outputs
else
:
seq_outputs
=
[
outputs
]
inputs
=
truncated_graph_inputs
(
outputs
,
ancestors_to_include
=
vectoriz
e
.
keys
())
inputs
=
truncated_graph_inputs
(
seq_outputs
,
ancestors_to_include
=
replac
e
.
keys
())
new_inputs
=
[
vectoriz
e
.
get
(
inp
,
inp
)
for
inp
in
inputs
]
new_inputs
=
[
replac
e
.
get
(
inp
,
inp
)
for
inp
in
inputs
]
def
transform
(
var
)
:
def
transform
(
var
:
Variable
)
->
Variable
:
if
var
in
inputs
:
if
var
in
inputs
:
return
new_inputs
[
inputs
.
index
(
var
)]
return
new_inputs
[
inputs
.
index
(
var
)]
...
@@ -257,7 +298,13 @@ def vectorize(
...
@@ -257,7 +298,13 @@ def vectorize(
batched_node
=
vectorize_node
(
node
,
*
batched_inputs
)
batched_node
=
vectorize_node
(
node
,
*
batched_inputs
)
batched_var
=
batched_node
.
outputs
[
var
.
owner
.
outputs
.
index
(
var
)]
batched_var
=
batched_node
.
outputs
[
var
.
owner
.
outputs
.
index
(
var
)]
return
batched_var
return
cast
(
Variable
,
batched_var
)
# TODO: MergeOptimization or node caching?
# TODO: MergeOptimization or node caching?
return
[
transform
(
out
)
for
out
in
outputs
]
seq_vect_outputs
=
[
transform
(
out
)
for
out
in
seq_outputs
]
if
isinstance
(
outputs
,
Sequence
):
return
seq_vect_outputs
else
:
[
vect_output
]
=
seq_vect_outputs
return
vect_output
pytensor/tensor/blockwise.py
浏览文件 @
47efd4b1
...
@@ -275,7 +275,7 @@ class Blockwise(Op):
...
@@ -275,7 +275,7 @@ class Blockwise(Op):
igrads
=
vectorize
(
igrads
=
vectorize
(
[
core_igrad
for
core_igrad
in
core_igrads
if
core_igrad
is
not
None
],
[
core_igrad
for
core_igrad
in
core_igrads
if
core_igrad
is
not
None
],
vectoriz
e
=
dict
(
replac
e
=
dict
(
zip
(
core_inputs
+
core_outputs
+
core_ograds
,
inputs
+
outputs
+
ograds
)
zip
(
core_inputs
+
core_outputs
+
core_ograds
,
inputs
+
outputs
+
ograds
)
),
),
)
)
...
...
tests/graph/test_replace.py
浏览文件 @
47efd4b1
...
@@ -4,7 +4,7 @@ import scipy.special
...
@@ -4,7 +4,7 @@ import scipy.special
import
pytensor.tensor
as
pt
import
pytensor.tensor
as
pt
from
pytensor
import
config
,
function
,
shared
from
pytensor
import
config
,
function
,
shared
from
pytensor.graph.basic
import
graph_inputs
from
pytensor.graph.basic
import
equal_computations
,
graph_inputs
from
pytensor.graph.replace
import
clone_replace
,
graph_replace
,
vectorize
from
pytensor.graph.replace
import
clone_replace
,
graph_replace
,
vectorize
from
pytensor.tensor
import
dvector
,
fvector
,
vector
from
pytensor.tensor
import
dvector
,
fvector
,
vector
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
...
@@ -236,9 +236,27 @@ class TestVectorize:
...
@@ -236,9 +236,27 @@ class TestVectorize:
new_x
=
pt
.
matrix
(
"new_x"
)
new_x
=
pt
.
matrix
(
"new_x"
)
[
new_y
]
=
vectorize
([
y
],
{
x
:
new_x
})
[
new_y
]
=
vectorize
([
y
],
{
x
:
new_x
})
# Check we can pass both a sequence or a single variable
alt_new_y
=
vectorize
(
y
,
{
x
:
new_x
})
assert
equal_computations
([
new_y
],
[
alt_new_y
])
fn
=
function
([
new_x
],
new_y
)
fn
=
function
([
new_x
],
new_y
)
test_new_y
=
np
.
array
([[
0
,
1
,
2
],
[
2
,
1
,
0
]])
.
astype
(
config
.
floatX
)
test_new_y
=
np
.
array
([[
0
,
1
,
2
],
[
2
,
1
,
0
]])
.
astype
(
config
.
floatX
)
np
.
testing
.
assert_allclose
(
np
.
testing
.
assert_allclose
(
fn
(
test_new_y
),
fn
(
test_new_y
),
scipy
.
special
.
softmax
(
test_new_y
,
axis
=-
1
),
scipy
.
special
.
softmax
(
test_new_y
,
axis
=-
1
),
)
)
def
test_multiple_outputs
(
self
):
x
=
pt
.
vector
(
"x"
)
y1
=
x
[
0
]
y2
=
x
[
-
1
]
new_x
=
pt
.
matrix
(
"new_x"
)
[
new_y1
,
new_y2
]
=
vectorize
([
y1
,
y2
],
{
x
:
new_x
})
fn
=
function
([
new_x
],
[
new_y1
,
new_y2
])
new_x_test
=
np
.
arange
(
9
)
.
reshape
(
3
,
3
)
.
astype
(
config
.
floatX
)
new_y1_res
,
new_y2_res
=
fn
(
new_x_test
)
np
.
testing
.
assert_allclose
(
new_y1_res
,
[
0
,
3
,
6
])
np
.
testing
.
assert_allclose
(
new_y2_res
,
[
2
,
5
,
8
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论