Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ccf53d19
提交
ccf53d19
authored
1月 06, 2026
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
1月 11, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SplitDims: Fix scalar tensor shape
上级
2ecf8523
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
20 行增加
和
19 行删除
+20
-19
reshape.py
pytensor/tensor/reshape.py
+17
-17
test_reshape.py
tests/tensor/test_reshape.py
+3
-2
没有找到文件。
pytensor/tensor/reshape.py
浏览文件 @
ccf53d19
...
@@ -3,7 +3,7 @@ from itertools import pairwise
...
@@ -3,7 +3,7 @@ from itertools import pairwise
from
typing
import
TypeAlias
from
typing
import
TypeAlias
import
numpy
as
np
import
numpy
as
np
from
numpy.lib._array_utils_impl
import
normalize_axis_tuple
from
numpy.lib._array_utils_impl
import
normalize_axis_
index
,
normalize_axis_
tuple
from
pytensor
import
Variable
from
pytensor
import
Variable
from
pytensor.gradient
import
DisconnectedType
from
pytensor.gradient
import
DisconnectedType
...
@@ -13,7 +13,6 @@ from pytensor.graph.replace import _vectorize_node
...
@@ -13,7 +13,6 @@ from pytensor.graph.replace import _vectorize_node
from
pytensor.scalar
import
ScalarVariable
from
pytensor.scalar
import
ScalarVariable
from
pytensor.tensor
import
TensorLike
,
as_tensor_variable
from
pytensor.tensor
import
TensorLike
,
as_tensor_variable
from
pytensor.tensor.basic
import
expand_dims
,
infer_static_shape
,
join
,
split
from
pytensor.tensor.basic
import
expand_dims
,
infer_static_shape
,
join
,
split
from
pytensor.tensor.extra_ops
import
squeeze
from
pytensor.tensor.math
import
prod
from
pytensor.tensor.math
import
prod
from
pytensor.tensor.type
import
tensor
from
pytensor.tensor.type
import
tensor
from
pytensor.tensor.variable
import
TensorVariable
from
pytensor.tensor.variable
import
TensorVariable
...
@@ -166,11 +165,16 @@ class SplitDims(Op):
...
@@ -166,11 +165,16 @@ class SplitDims(Op):
def
make_node
(
self
,
x
,
shape
):
def
make_node
(
self
,
x
,
shape
):
x
=
as_tensor_variable
(
x
)
x
=
as_tensor_variable
(
x
)
shape
=
as_tensor_variable
(
shape
,
dtype
=
int
,
ndim
=
1
)
shape
=
as_tensor_variable
(
shape
,
dtype
=
int
)
if
shape
.
type
.
numpy_dtype
.
kind
not
in
"iu"
:
if
shape
.
type
.
numpy_dtype
.
kind
not
in
"iu"
:
raise
TypeError
(
"shape must be an integer tensor"
)
raise
TypeError
(
"shape must be an integer tensor"
)
if
shape
.
type
.
ndim
!=
1
:
raise
TypeError
(
f
"shape must be a 1-D tensor, got {shape} with {shape.type.ndim} dimensions"
)
axis
=
self
.
axis
axis
=
self
.
axis
_
,
constant_shape
=
infer_static_shape
(
shape
)
_
,
constant_shape
=
infer_static_shape
(
shape
)
...
@@ -262,25 +266,21 @@ def split_dims(
...
@@ -262,25 +266,21 @@ def split_dims(
x
=
as_tensor_variable
(
x
)
x
=
as_tensor_variable
(
x
)
if
axis
is
None
:
if
axis
is
None
:
if
x
.
ndim
!=
1
:
if
x
.
type
.
ndim
!=
1
:
raise
ValueError
(
raise
ValueError
(
"split_dims can only be called with axis=None for 1d inputs"
"split_dims can only be called with axis=None for 1d inputs"
)
)
axis
=
0
axis
=
0
if
isinstance
(
shape
,
int
):
shape
=
[
shape
]
else
:
else
:
shape
=
list
(
shape
)
# type: ignore[arg-type]
axis
=
normalize_axis_index
(
axis
,
x
.
ndim
)
if
not
shape
:
# Convert scalar shape to 1d tuple (shape,)
# If we get an empty shape, there is potentially a dummy dimension at the requested axis. This happens for
if
not
isinstance
(
shape
,
Sequence
):
# example when splitting a packed tensor that had its dims expanded before packing (e.g. when packing shapes
if
isinstance
(
shape
,
TensorVariable
|
np
.
ndarray
):
# (3, ) and (3, 3) to (3, 4)
if
shape
.
ndim
==
0
:
return
squeeze
(
x
,
axis
=
axis
)
# type: ignore[no-any-return]
shape
=
(
shape
,)
elif
isinstance
(
shape
,
int
|
np
.
integer
|
ScalarVariable
):
[
axis
]
=
normalize_axis_tuple
(
axis
,
x
.
ndim
)
# type: ignore[misc]
shape
=
(
shape
,)
shape
=
as_tensor_variable
(
shape
,
dtype
=
"int64"
,
ndim
=
1
)
# type: ignore[arg-type]
return
SplitDims
(
axis
=
axis
)(
x
,
shape
)
# type: ignore[return-value]
return
SplitDims
(
axis
=
axis
)(
x
,
shape
)
# type: ignore[return-value]
...
...
tests/tensor/test_reshape.py
浏览文件 @
ccf53d19
...
@@ -61,9 +61,10 @@ def test_join_dims():
...
@@ -61,9 +61,10 @@ def test_join_dims():
[
[
(
0
,
pt
.
as_tensor
([
2
,
3
]),
(
2
,
3
,
4
,
6
)),
(
0
,
pt
.
as_tensor
([
2
,
3
]),
(
2
,
3
,
4
,
6
)),
(
2
,
[
2
,
3
],
(
6
,
4
,
2
,
3
)),
(
2
,
[
2
,
3
],
(
6
,
4
,
2
,
3
)),
(
-
1
,
pt
.
as_tensor
(
6
),
(
6
,
4
,
6
)),
(
-
1
,
6
,
(
6
,
4
,
6
)),
(
-
1
,
6
,
(
6
,
4
,
6
)),
],
],
ids
=
[
"tensor
"
,
"list
"
,
"integer"
],
ids
=
[
"tensor
list"
,
"integer list"
,
"tensor
"
,
"integer"
],
)
)
def
test_split_dims
(
axis
,
shape
,
expected_shape
):
def
test_split_dims
(
axis
,
shape
,
expected_shape
):
rng
=
np
.
random
.
default_rng
()
rng
=
np
.
random
.
default_rng
()
...
@@ -95,7 +96,7 @@ def test_split_dims(axis, shape, expected_shape):
...
@@ -95,7 +96,7 @@ def test_split_dims(axis, shape, expected_shape):
def
test_split_size_zero_shape
():
def
test_split_size_zero_shape
():
x
=
pt
.
tensor
(
"x"
,
shape
=
(
1
,
4
,
6
))
x
=
pt
.
tensor
(
"x"
,
shape
=
(
1
,
4
,
6
))
x_split
=
split_dims
(
x
,
axis
=
0
,
shape
=
pt
.
as_tensor
(
np
.
zeros
((
0
,))))
x_split
=
split_dims
(
x
,
axis
=
0
,
shape
=
pt
.
as_tensor
(
np
.
zeros
((
0
,)
,
dtype
=
"int32"
)))
assert
x_split
.
type
.
shape
==
(
4
,
6
)
assert
x_split
.
type
.
shape
==
(
4
,
6
)
x_value
=
np
.
empty
((
1
,
4
,
6
),
dtype
=
config
.
floatX
)
x_value
=
np
.
empty
((
1
,
4
,
6
),
dtype
=
config
.
floatX
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论