Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
6834740a
提交
6834740a
authored
10月 11, 2023
作者:
Ricardo Vieira
提交者:
Michael Osthege
10月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix static type shape bug
上级
081a0b48
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
49 行增加
和
6 行删除
+49
-6
op.py
pytensor/tensor/random/op.py
+1
-1
type.py
pytensor/tensor/type.py
+6
-1
test_basic.py
tests/tensor/random/test_basic.py
+10
-0
test_basic.py
tests/tensor/test_basic.py
+11
-4
test_type.py
tests/tensor/test_type.py
+21
-0
没有找到文件。
pytensor/tensor/random/op.py
浏览文件 @
6834740a
...
@@ -191,7 +191,7 @@ class RandomVariable(Op):
...
@@ -191,7 +191,7 @@ class RandomVariable(Op):
return
shape
return
shape
batch_shape
=
[
batch_shape
=
[
s
if
b
is
False
else
constant
(
1
,
"int64"
)
s
if
not
b
else
constant
(
1
,
"int64"
)
for
s
,
b
in
zip
(
shape
[:
-
n
],
p
.
type
.
broadcastable
[:
-
n
])
for
s
,
b
in
zip
(
shape
[:
-
n
],
p
.
type
.
broadcastable
[:
-
n
])
]
]
return
batch_shape
return
batch_shape
...
...
pytensor/tensor/type.py
浏览文件 @
6834740a
...
@@ -109,8 +109,13 @@ class TensorType(CType[np.ndarray], HasDataType, HasShape):
...
@@ -109,8 +109,13 @@ class TensorType(CType[np.ndarray], HasDataType, HasShape):
def
parse_bcast_and_shape
(
s
):
def
parse_bcast_and_shape
(
s
):
if
isinstance
(
s
,
(
bool
,
np
.
bool_
)):
if
isinstance
(
s
,
(
bool
,
np
.
bool_
)):
return
1
if
s
else
None
return
1
if
s
else
None
else
:
elif
isinstance
(
s
,
(
int
,
np
.
int_
)):
return
int
(
s
)
elif
s
is
None
:
return
s
return
s
raise
ValueError
(
f
"TensorType broadcastable/shape must be a boolean, integer or None, got {type(s)} {s}"
)
self
.
shape
=
tuple
(
parse_bcast_and_shape
(
s
)
for
s
in
shape
)
self
.
shape
=
tuple
(
parse_bcast_and_shape
(
s
)
for
s
in
shape
)
self
.
dtype_specs
()
# error checking is done there
self
.
dtype_specs
()
# error checking is done there
...
...
tests/tensor/random/test_basic.py
浏览文件 @
6834740a
...
@@ -16,6 +16,7 @@ from pytensor.graph.fg import FunctionGraph
...
@@ -16,6 +16,7 @@ from pytensor.graph.fg import FunctionGraph
from
pytensor.graph.op
import
get_test_value
from
pytensor.graph.op
import
get_test_value
from
pytensor.graph.replace
import
clone_replace
from
pytensor.graph.replace
import
clone_replace
from
pytensor.graph.rewriting.db
import
RewriteDatabaseQuery
from
pytensor.graph.rewriting.db
import
RewriteDatabaseQuery
from
pytensor.tensor
import
ones
,
stack
from
pytensor.tensor.random.basic
import
(
from
pytensor.tensor.random.basic
import
(
_gamma
,
_gamma
,
bernoulli
,
bernoulli
,
...
@@ -1465,3 +1466,12 @@ def test_rebuild():
...
@@ -1465,3 +1466,12 @@ def test_rebuild():
assert
y_new
.
type
.
shape
==
(
100
,)
assert
y_new
.
type
.
shape
==
(
100
,)
assert
y_new
.
shape
.
eval
({
x_new
:
x_new_test
})
==
(
100
,)
assert
y_new
.
shape
.
eval
({
x_new
:
x_new_test
})
==
(
100
,)
assert
y_new
.
eval
({
x_new
:
x_new_test
})
.
shape
==
(
100
,)
assert
y_new
.
eval
({
x_new
:
x_new_test
})
.
shape
==
(
100
,)
def
test_categorical_join_p_static_shape
():
"""Regression test against a bug caused by misreading a numpy.bool_"""
p
=
ones
(
3
)
/
3
prob
=
stack
([
p
,
1
-
p
],
axis
=-
1
)
assert
prob
.
type
.
shape
==
(
3
,
2
)
x
=
categorical
(
p
=
prob
)
assert
x
.
type
.
shape
==
(
3
,)
tests/tensor/test_basic.py
浏览文件 @
6834740a
...
@@ -2046,8 +2046,14 @@ class TestJoinAndSplit:
...
@@ -2046,8 +2046,14 @@ class TestJoinAndSplit:
def
test_static_shape_inference
(
self
):
def
test_static_shape_inference
(
self
):
a
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
3
))
a
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
3
))
b
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
5
))
b
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
5
))
assert
at
.
join
(
1
,
a
,
b
)
.
type
.
shape
==
(
2
,
8
)
assert
at
.
join
(
-
1
,
a
,
b
)
.
type
.
shape
==
(
2
,
8
)
res
=
at
.
join
(
1
,
a
,
b
)
.
type
.
shape
assert
res
==
(
2
,
8
)
assert
all
(
isinstance
(
s
,
int
)
for
s
in
res
)
res
=
at
.
join
(
-
1
,
a
,
b
)
.
type
.
shape
assert
res
==
(
2
,
8
)
assert
all
(
isinstance
(
s
,
int
)
for
s
in
res
)
# Check early informative errors from static shape info
# Check early informative errors from static shape info
with
pytest
.
raises
(
ValueError
,
match
=
"must match exactly"
):
with
pytest
.
raises
(
ValueError
,
match
=
"must match exactly"
):
...
@@ -2055,8 +2061,9 @@ class TestJoinAndSplit:
...
@@ -2055,8 +2061,9 @@ class TestJoinAndSplit:
# Check partial inference
# Check partial inference
d
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
None
))
d
=
at
.
tensor
(
dtype
=
"int8"
,
shape
=
(
2
,
None
))
assert
at
.
join
(
1
,
a
,
b
,
d
)
.
type
.
shape
==
(
2
,
None
)
res
=
at
.
join
(
1
,
a
,
b
,
d
)
.
type
.
shape
return
assert
res
==
(
2
,
None
)
assert
isinstance
(
res
[
0
],
int
)
def
test_split_0elem
(
self
):
def
test_split_0elem
(
self
):
rng
=
np
.
random
.
default_rng
(
seed
=
utt
.
fetch_seed
())
rng
=
np
.
random
.
default_rng
(
seed
=
utt
.
fetch_seed
())
...
...
tests/tensor/test_type.py
浏览文件 @
6834740a
...
@@ -267,6 +267,27 @@ def test_fixed_shape_basic():
...
@@ -267,6 +267,27 @@ def test_fixed_shape_basic():
assert
t2
.
shape
==
(
2
,
4
)
assert
t2
.
shape
==
(
2
,
4
)
def
test_shape_type_conversion
():
t1
=
TensorType
(
"float64"
,
shape
=
np
.
array
([
3
],
dtype
=
int
))
assert
t1
.
shape
==
(
3
,)
assert
isinstance
(
t1
.
shape
[
0
],
int
)
assert
t1
.
broadcastable
==
(
False
,)
assert
isinstance
(
t1
.
broadcastable
[
0
],
bool
)
t2
=
TensorType
(
"float64"
,
broadcastable
=
np
.
array
([
True
,
False
],
dtype
=
"bool"
))
assert
t2
.
shape
==
(
1
,
None
)
assert
isinstance
(
t2
.
shape
[
0
],
int
)
assert
t2
.
broadcastable
==
(
True
,
False
)
assert
isinstance
(
t2
.
broadcastable
[
0
],
bool
)
assert
isinstance
(
t2
.
broadcastable
[
1
],
bool
)
with
pytest
.
raises
(
ValueError
,
match
=
"TensorType broadcastable/shape must be a boolean, integer or None"
,
):
TensorType
(
"float64"
,
shape
=
(
"1"
,
"2"
))
def
test_fixed_shape_clone
():
def
test_fixed_shape_clone
():
t1
=
TensorType
(
"float64"
,
(
1
,))
t1
=
TensorType
(
"float64"
,
(
1
,))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论