Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7ecb9f8c
提交
7ecb9f8c
authored
11月 24, 2023
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
11月 24, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix vectorized shape
上级
861f95c2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
66 行增加
和
16 行删除
+66
-16
shape.py
pytensor/tensor/shape.py
+15
-2
test_shape.py
tests/tensor/test_shape.py
+51
-14
没有找到文件。
pytensor/tensor/shape.py
浏览文件 @
7ecb9f8c
...
@@ -8,7 +8,7 @@ import numpy as np
...
@@ -8,7 +8,7 @@ import numpy as np
import
pytensor
import
pytensor
from
pytensor.gradient
import
DisconnectedType
from
pytensor.gradient
import
DisconnectedType
from
pytensor.graph.basic
import
Apply
,
Variable
from
pytensor.graph.basic
import
Apply
,
Variable
from
pytensor.graph.replace
import
_vectorize_node
,
_vectorize_not_needed
from
pytensor.graph.replace
import
_vectorize_node
from
pytensor.graph.type
import
HasShape
from
pytensor.graph.type
import
HasShape
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.op
import
COp
from
pytensor.link.c.params_type
import
ParamsType
from
pytensor.link.c.params_type
import
ParamsType
...
@@ -155,7 +155,20 @@ def _get_vector_length_Shape(op, var):
...
@@ -155,7 +155,20 @@ def _get_vector_length_Shape(op, var):
return
var
.
owner
.
inputs
[
0
]
.
type
.
ndim
return
var
.
owner
.
inputs
[
0
]
.
type
.
ndim
_vectorize_node
.
register
(
Shape
,
_vectorize_not_needed
)
@_vectorize_node.register
(
Shape
)
def
vectorize_shape
(
op
,
node
,
batched_x
):
from
pytensor.tensor.extra_ops
import
broadcast_to
[
old_x
]
=
node
.
inputs
core_ndims
=
old_x
.
type
.
ndim
batch_ndims
=
batched_x
.
type
.
ndim
-
core_ndims
batched_x_shape
=
shape
(
batched_x
)
if
not
batch_ndims
:
return
batched_x_shape
.
owner
else
:
batch_shape
=
batched_x_shape
[:
batch_ndims
]
core_shape
=
batched_x_shape
[
batch_ndims
:]
return
broadcast_to
(
core_shape
,
(
*
batch_shape
,
core_ndims
))
.
owner
def
shape_tuple
(
x
:
TensorVariable
)
->
tuple
[
Variable
,
...
]:
def
shape_tuple
(
x
:
TensorVariable
)
->
tuple
[
Variable
,
...
]:
...
...
tests/tensor/test_shape.py
浏览文件 @
7ecb9f8c
...
@@ -709,29 +709,66 @@ def test_shape_tuple():
...
@@ -709,29 +709,66 @@ def test_shape_tuple():
class
TestVectorize
:
class
TestVectorize
:
@pytensor.config.change_flags
(
cxx
=
""
)
# For faster eval
def
test_shape
(
self
):
def
test_shape
(
self
):
vec
=
tensor
(
shape
=
(
None
,))
vec
=
tensor
(
shape
=
(
None
,),
dtype
=
"float64"
)
mat
=
tensor
(
shape
=
(
None
,
None
))
mat
=
tensor
(
shape
=
(
None
,
None
),
dtype
=
"float64"
)
node
=
shape
(
vec
)
.
owner
node
=
shape
(
vec
)
.
owner
vect_node
=
vectorize_node
(
node
,
mat
)
assert
equal_computations
(
vect_node
.
outputs
,
[
shape
(
mat
)])
[
vect_out
]
=
vectorize_node
(
node
,
mat
)
.
outputs
assert
equal_computations
(
[
vect_out
],
[
broadcast_to
(
mat
.
shape
[
1
:],
(
*
mat
.
shape
[:
1
],
1
))]
)
mat_test_value
=
np
.
ones
((
5
,
3
))
ref_fn
=
np
.
vectorize
(
lambda
vec
:
np
.
asarray
(
vec
.
shape
),
signature
=
"(vec)->(1)"
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
mat
:
mat_test_value
}),
ref_fn
(
mat_test_value
),
)
mat
=
tensor
(
shape
=
(
None
,
None
),
dtype
=
"float64"
)
tns
=
tensor
(
shape
=
(
None
,
None
,
None
,
None
),
dtype
=
"float64"
)
node
=
shape
(
mat
)
.
owner
[
vect_out
]
=
vectorize_node
(
node
,
tns
)
.
outputs
assert
equal_computations
(
[
vect_out
],
[
broadcast_to
(
tns
.
shape
[
2
:],
(
*
tns
.
shape
[:
2
],
2
))]
)
tns_test_value
=
np
.
ones
((
4
,
6
,
5
,
3
))
ref_fn
=
np
.
vectorize
(
lambda
vec
:
np
.
asarray
(
vec
.
shape
),
signature
=
"(m1,m2)->(2)"
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
tns
:
tns_test_value
}),
ref_fn
(
tns_test_value
),
)
@pytensor.config.change_flags
(
cxx
=
""
)
# For faster eval
def
test_reshape
(
self
):
def
test_reshape
(
self
):
x
=
scalar
(
"x"
,
dtype
=
int
)
x
=
scalar
(
"x"
,
dtype
=
int
)
vec
=
tensor
(
shape
=
(
None
,))
vec
=
tensor
(
shape
=
(
None
,)
,
dtype
=
"float64"
)
mat
=
tensor
(
shape
=
(
None
,
None
))
mat
=
tensor
(
shape
=
(
None
,
None
)
,
dtype
=
"float64"
)
shape
=
(
2
,
x
)
shape
=
(
-
1
,
x
)
node
=
reshape
(
vec
,
shape
)
.
owner
node
=
reshape
(
vec
,
shape
)
.
owner
vect_node
=
vectorize_node
(
node
,
mat
,
shape
)
assert
equal_computations
(
[
vect_out
]
=
vectorize_node
(
node
,
mat
,
shape
)
.
outputs
vect_node
.
outputs
,
[
reshape
(
mat
,
(
*
mat
.
shape
[:
1
],
2
,
x
))]
assert
equal_computations
([
vect_out
],
[
reshape
(
mat
,
(
*
mat
.
shape
[:
1
],
-
1
,
x
))])
x_test_value
=
2
mat_test_value
=
np
.
ones
((
5
,
6
))
ref_fn
=
np
.
vectorize
(
lambda
x
,
vec
:
vec
.
reshape
(
-
1
,
x
),
signature
=
"(),(vec1)->(mat1,mat2)"
)
np
.
testing
.
assert_array_equal
(
vect_out
.
eval
({
x
:
x_test_value
,
mat
:
mat_test_value
}),
ref_fn
(
x_test_value
,
mat_test_value
),
)
)
new_shape
=
(
5
,
2
,
x
)
new_shape
=
(
5
,
-
1
,
x
)
vect_node
=
vectorize_node
(
node
,
mat
,
new_shape
)
[
vect_out
]
=
vectorize_node
(
node
,
mat
,
new_shape
)
.
outputs
assert
equal_computations
(
vect_node
.
outputs
,
[
reshape
(
mat
,
new_shape
)])
assert
equal_computations
(
[
vect_out
]
,
[
reshape
(
mat
,
new_shape
)])
with
pytest
.
raises
(
NotImplementedError
):
with
pytest
.
raises
(
NotImplementedError
):
vectorize_node
(
node
,
vec
,
broadcast_to
(
as_tensor
([
5
,
2
,
x
]),
(
2
,
3
)))
vectorize_node
(
node
,
vec
,
broadcast_to
(
as_tensor
([
5
,
2
,
x
]),
(
2
,
3
)))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论