Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7d0edb87
提交
7d0edb87
authored
9月 16, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
9月 17, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make SparseTensorType.filter match TensorType in error behavior
上级
3e5ad997
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
5 行删除
+39
-5
type.py
aesara/sparse/type.py
+10
-5
test_type.py
tests/sparse/test_type.py
+29
-0
没有找到文件。
aesara/sparse/type.py
浏览文件 @
7d0edb87
...
@@ -6,6 +6,7 @@ from typing_extensions import Literal
...
@@ -6,6 +6,7 @@ from typing_extensions import Literal
import
aesara
import
aesara
from
aesara
import
scalar
as
aes
from
aesara
import
scalar
as
aes
from
aesara.graph.basic
import
Variable
from
aesara.graph.type
import
HasDataType
from
aesara.graph.type
import
HasDataType
from
aesara.tensor.type
import
DenseTensorType
,
TensorType
from
aesara.tensor.type
import
DenseTensorType
,
TensorType
...
@@ -98,6 +99,13 @@ class SparseTensorType(TensorType, HasDataType):
...
@@ -98,6 +99,13 @@ class SparseTensorType(TensorType, HasDataType):
return
type
(
self
)(
format
,
dtype
,
shape
=
shape
,
**
kwargs
)
return
type
(
self
)(
format
,
dtype
,
shape
=
shape
,
**
kwargs
)
def
filter
(
self
,
value
,
strict
=
False
,
allow_downcast
=
None
):
def
filter
(
self
,
value
,
strict
=
False
,
allow_downcast
=
None
):
if
isinstance
(
value
,
Variable
):
raise
TypeError
(
"Expected an array-like object, but found a Variable: "
"maybe you are trying to call a function on a (possibly "
"shared) variable instead of a numeric array?"
)
if
(
if
(
isinstance
(
value
,
self
.
format_cls
[
self
.
format
])
isinstance
(
value
,
self
.
format_cls
[
self
.
format
])
and
value
.
dtype
==
self
.
dtype
and
value
.
dtype
==
self
.
dtype
...
@@ -117,13 +125,10 @@ class SparseTensorType(TensorType, HasDataType):
...
@@ -117,13 +125,10 @@ class SparseTensorType(TensorType, HasDataType):
data
=
self
.
format_cls
[
self
.
format
](
value
)
data
=
self
.
format_cls
[
self
.
format
](
value
)
up_dtype
=
aes
.
upcast
(
self
.
dtype
,
data
.
dtype
)
up_dtype
=
aes
.
upcast
(
self
.
dtype
,
data
.
dtype
)
if
up_dtype
!=
self
.
dtype
:
if
up_dtype
!=
self
.
dtype
:
raise
NotImplementedError
(
raise
TypeError
(
f
"Expected {self.dtype} dtype but got {data.dtype}"
)
f
"Expected {self.dtype} dtype but got {data.dtype}"
)
sp
=
data
.
astype
(
up_dtype
)
sp
=
data
.
astype
(
up_dtype
)
if
sp
.
format
!=
self
.
format
:
assert
sp
.
format
==
self
.
format
raise
NotImplementedError
()
return
sp
return
sp
...
...
tests/sparse/test_type.py
浏览文件 @
7d0edb87
import
pytest
import
pytest
import
scipy
as
sp
from
aesara.sparse
import
matrix
as
sp_matrix
from
aesara.sparse
import
matrix
as
sp_matrix
from
aesara.sparse.type
import
SparseTensorType
from
aesara.sparse.type
import
SparseTensorType
...
@@ -52,3 +53,31 @@ def test_SparseTensorType_convert_variable():
...
@@ -52,3 +53,31 @@ def test_SparseTensorType_convert_variable():
# we would need to added sparse/dense logic to `TensorType`, and we don't
# we would need to added sparse/dense logic to `TensorType`, and we don't
# want to do that.
# want to do that.
assert
x
.
type
.
convert_variable
(
y
)
is
y
assert
x
.
type
.
convert_variable
(
y
)
is
y
def
test_SparseTensorType_filter
():
y
=
sp_matrix
(
"csc"
,
dtype
=
"float64"
,
name
=
"y"
)
z
=
sp_matrix
(
"csr"
,
dtype
=
"float64"
,
name
=
"z"
)
w
=
sp_matrix
(
"csr"
,
dtype
=
"float32"
,
name
=
"z"
)
with
pytest
.
raises
(
TypeError
,
match
=
"Expected an array-like"
):
y
.
type
.
filter
(
dmatrix
())
x
=
sp
.
sparse
.
csc_matrix
(
sp
.
sparse
.
eye
(
5
,
3
))
x_res
=
y
.
type
.
filter
(
x
)
assert
x
is
x_res
x_res
=
z
.
type
.
filter
(
x
)
assert
x_res
.
format
==
"csr"
with
pytest
.
raises
(
TypeError
):
x_res
=
z
.
type
.
filter
(
x
,
strict
=
True
)
x_res
=
w
.
type
.
filter
(
x
,
allow_downcast
=
True
)
assert
x_res
.
dtype
==
"float32"
x_res
=
z
.
type
.
filter
(
x
.
astype
(
"float32"
),
allow_downcast
=
True
)
assert
x_res
.
dtype
==
"float64"
with
pytest
.
raises
(
TypeError
,
match
=
".*dtype but got.*"
):
w
.
type
.
filter
(
x
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论