Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3082ed5e
Unverified
提交
3082ed5e
authored
10月 21, 2025
作者:
Jesse Grabowski
提交者:
GitHub
10月 21, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename sparse functions to match numpy array API (#1663)
* Rename `mul` -> `multiply` * Rename `sub` -> `subtract` * Space... the final frontier
上级
5547eb08
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
52 行增加
和
23 行删除
+52
-23
basic.py
pytensor/sparse/basic.py
+45
-16
test_basic.py
tests/sparse/test_basic.py
+7
-7
没有找到文件。
pytensor/sparse/basic.py
浏览文件 @
3082ed5e
...
@@ -2268,36 +2268,49 @@ def add(x, y):
...
@@ -2268,36 +2268,49 @@ def add(x, y):
raise
NotImplementedError
()
raise
NotImplementedError
()
def
sub
(
x
,
y
):
def
subtract
(
x
:
SparseVariable
|
TensorVariable
,
y
:
SparseVariable
|
TensorVariable
)
->
SparseVariable
:
"""
"""
Subtract two matrices, at least one of which is sparse.
Subtract two matrices, at least one of which is sparse.
This method will provide the right op according
This method will provide the right op according to the inputs.
to the inputs.
Parameters
Parameters
----------
----------
x
x
: SparseVariable or TensorVariable
A matrix variable.
A matrix variable.
y
y
: SparseVariable or TensorVariable
A matrix variable.
A matrix variable.
Returns
Returns
-------
-------
A sparse matrix
result: SparseVariable
`x` - `y`
Result of `x - y`, as a sparse matrix.
Notes
Notes
-----
-----
At least one of `x` and `y` must be a sparse matrix.
At least one of `x` and `y` must be a sparse matrix.
The grad will be structured only when one of the variable will be a dense
The grad will be structured only when one of the variable will be a dense matrix.
matrix.
"""
"""
return
x
+
(
-
y
)
return
x
+
(
-
y
)
def
sub
(
x
,
y
):
warn
(
"pytensor.sparse.sub is deprecated and will be removed in a future version. Use "
"pytensor.sparse.subtract instead."
,
category
=
DeprecationWarning
,
stacklevel
=
2
,
)
return
subtract
(
x
,
y
)
sub
.
__doc__
=
subtract
.
__doc__
class
MulSS
(
Op
):
class
MulSS
(
Op
):
# mul(sparse, sparse)
# mul(sparse, sparse)
# See the doc of mul() for more detail
# See the doc of mul() for more detail
...
@@ -2491,7 +2504,9 @@ class MulSV(Op):
...
@@ -2491,7 +2504,9 @@ class MulSV(Op):
mul_s_v
=
MulSV
()
mul_s_v
=
MulSV
()
def
mul
(
x
,
y
):
def
multiply
(
x
:
SparseTensorType
|
TensorType
,
y
:
SparseTensorType
|
TensorType
)
->
SparseVariable
:
"""
"""
Multiply elementwise two matrices, at least one of which is sparse.
Multiply elementwise two matrices, at least one of which is sparse.
...
@@ -2499,21 +2514,21 @@ def mul(x, y):
...
@@ -2499,21 +2514,21 @@ def mul(x, y):
Parameters
Parameters
----------
----------
x
x
: SparseVariable
A matrix variable.
A matrix variable.
y
y
: SparseVariable
A matrix variable.
A matrix variable.
Returns
Returns
-------
-------
A sparse matrix
result: SparseVariable
`x` * `y`
The elementwise multiplication of `x` and `y`.
Notes
Notes
-----
-----
At least one of `x` and `y` must be a sparse matrix.
At least one of `x` and `y` must be a sparse matrix.
The grad is regular, i.e. not structured.
The gradient is regular, i.e. not structured.
"""
"""
x
=
as_sparse_or_tensor_variable
(
x
)
x
=
as_sparse_or_tensor_variable
(
x
)
...
@@ -2541,6 +2556,20 @@ def mul(x, y):
...
@@ -2541,6 +2556,20 @@ def mul(x, y):
raise
NotImplementedError
()
raise
NotImplementedError
()
def
mul
(
x
,
y
):
warn
(
"pytensor.sparse.mul is deprecated and will be removed in a future version. Use "
"pytensor.sparse.multiply instead."
,
category
=
DeprecationWarning
,
stacklevel
=
2
,
)
return
multiply
(
x
,
y
)
mul
.
__doc__
=
multiply
.
__doc__
class
__ComparisonOpSS
(
Op
):
class
__ComparisonOpSS
(
Op
):
"""
"""
Used as a superclass for all comparisons between two sparses matrices.
Used as a superclass for all comparisons between two sparses matrices.
...
...
tests/sparse/test_basic.py
浏览文件 @
3082ed5e
...
@@ -65,8 +65,8 @@ from pytensor.sparse import (
...
@@ -65,8 +65,8 @@ from pytensor.sparse import (
gt
,
gt
,
le
,
le
,
lt
,
lt
,
mul
,
mul_s_v
,
mul_s_v
,
multiply
,
sampling_dot
,
sampling_dot
,
sp_ones_like
,
sp_ones_like
,
square_diagonal
,
square_diagonal
,
...
@@ -724,21 +724,21 @@ class TestAddMul:
...
@@ -724,21 +724,21 @@ class TestAddMul:
def
test_MulSS
(
self
):
def
test_MulSS
(
self
):
self
.
_testSS
(
self
.
_testSS
(
mul
,
mul
tiply
,
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
)
)
def
test_MulSD
(
self
):
def
test_MulSD
(
self
):
self
.
_testSD
(
self
.
_testSD
(
mul
,
mul
tiply
,
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
)
)
def
test_MulDS
(
self
):
def
test_MulDS
(
self
):
self
.
_testDS
(
self
.
_testDS
(
mul
,
mul
tiply
,
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
0
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
np
.
array
([[
1.0
,
2
],
[
3
,
0
],
[
0
,
6
]]),
)
)
...
@@ -783,7 +783,7 @@ class TestAddMul:
...
@@ -783,7 +783,7 @@ class TestAddMul:
assert
np
.
all
(
val
.
todense
()
==
array1
+
array2
)
assert
np
.
all
(
val
.
todense
()
==
array1
+
array2
)
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
elif
op
is
mul
:
elif
op
is
mul
tiply
:
assert
np
.
all
(
val
.
todense
()
==
array1
*
array2
)
assert
np
.
all
(
val
.
todense
()
==
array1
*
array2
)
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
False
)
...
@@ -833,7 +833,7 @@ class TestAddMul:
...
@@ -833,7 +833,7 @@ class TestAddMul:
continue
continue
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
elif
op
is
mul
:
elif
op
is
mul
tiply
:
assert
_is_sparse_variable
(
apb
)
assert
_is_sparse_variable
(
apb
)
assert
np
.
all
(
val
.
todense
()
==
b
.
multiply
(
array1
))
assert
np
.
all
(
val
.
todense
()
==
b
.
multiply
(
array1
))
assert
np
.
all
(
assert
np
.
all
(
...
@@ -887,7 +887,7 @@ class TestAddMul:
...
@@ -887,7 +887,7 @@ class TestAddMul:
b
=
b
.
data
b
=
b
.
data
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
if
dtype1
.
startswith
(
"float"
)
and
dtype2
.
startswith
(
"float"
):
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
verify_grad_sparse
(
op
,
[
a
,
b
],
structured
=
True
)
elif
op
is
mul
:
elif
op
is
mul
tiply
:
assert
_is_sparse_variable
(
apb
)
assert
_is_sparse_variable
(
apb
)
ans
=
np
.
array
([[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])
ans
=
np
.
array
([[
1
,
0
],
[
9
,
0
],
[
0
,
36
]])
assert
np
.
all
(
val
.
todense
()
==
(
a
.
multiply
(
array2
)))
assert
np
.
all
(
val
.
todense
()
==
(
a
.
multiply
(
array2
)))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论