Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9578bd3b
提交
9578bd3b
authored
4月 09, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
4月 09, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow specializing shape of predefined tensors types
上级
3c66aa65
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
34 行增加
和
0 行删除
+34
-0
type.py
pytensor/tensor/type.py
+12
-0
test_type.py
tests/tensor/test_type.py
+22
-0
没有找到文件。
pytensor/tensor/type.py
浏览文件 @
9578bd3b
...
@@ -123,6 +123,18 @@ class TensorType(CType[np.ndarray], HasDataType, HasShape):
...
@@ -123,6 +123,18 @@ class TensorType(CType[np.ndarray], HasDataType, HasShape):
self
.
name
=
name
self
.
name
=
name
self
.
numpy_dtype
=
np
.
dtype
(
self
.
dtype
)
self
.
numpy_dtype
=
np
.
dtype
(
self
.
dtype
)
def
__call__
(
self
,
*
args
,
shape
=
None
,
**
kwargs
):
if
shape
is
not
None
:
# Check if shape is compatible with the original type
new_type
=
self
.
clone
(
shape
=
shape
)
if
self
.
is_super
(
new_type
):
return
new_type
(
*
args
,
**
kwargs
)
else
:
raise
ValueError
(
f
"{shape=} is incompatible with original type shape {self.shape=}"
)
return
super
()
.
__call__
(
*
args
,
**
kwargs
)
def
clone
(
def
clone
(
self
,
dtype
=
None
,
shape
=
None
,
broadcastable
=
None
,
**
kwargs
self
,
dtype
=
None
,
shape
=
None
,
broadcastable
=
None
,
**
kwargs
)
->
"TensorType"
:
)
->
"TensorType"
:
...
...
tests/tensor/test_type.py
浏览文件 @
9578bd3b
...
@@ -10,6 +10,10 @@ from pytensor.tensor.shape import SpecifyShape
...
@@ -10,6 +10,10 @@ from pytensor.tensor.shape import SpecifyShape
from
pytensor.tensor.type
import
(
from
pytensor.tensor.type
import
(
TensorType
,
TensorType
,
col
,
col
,
dmatrix
,
drow
,
fmatrix
,
frow
,
matrix
,
matrix
,
row
,
row
,
scalar
,
scalar
,
...
@@ -477,3 +481,21 @@ def test_row_matrix_creator_helpers(helper):
...
@@ -477,3 +481,21 @@ def test_row_matrix_creator_helpers(helper):
match
=
"The second dimension of a `col` must have shape 1, got 5"
match
=
"The second dimension of a `col` must have shape 1, got 5"
with
pytest
.
raises
(
ValueError
,
match
=
match
):
with
pytest
.
raises
(
ValueError
,
match
=
match
):
helper
(
shape
=
(
2
,
5
))
helper
(
shape
=
(
2
,
5
))
def
test_shape_of_predefined_dtype_tensor
():
# Valid: None dimensions can be specialized
assert
fmatrix
(
shape
=
(
1
,
None
))
.
type
==
frow
assert
drow
(
shape
=
(
1
,
5
))
.
type
==
dmatrix
(
shape
=
(
1
,
5
))
.
type
# Invalid: Number of dimensions must match
with
pytest
.
raises
(
ValueError
):
fmatrix
(
shape
=
(
None
,
None
,
None
))
# Invalid: Fixed shapes must match
with
pytest
.
raises
(
ValueError
):
fmatrix
(
shape
=
(
3
,
5
))
.
type
(
shape
=
(
4
,
5
))
# Invalid: Known shapes can't be lost
with
pytest
.
raises
(
ValueError
):
drow
(
shape
=
(
None
,
None
))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论