Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a6975da3
提交
a6975da3
authored
6月 23, 2023
作者:
Ricardo Vieira
提交者:
Thomas Wiecki
9月 06, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CholeskySolve inherits from BaseSolve
上级
d6f0185b
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
51 行增加
和
95 行删除
+51
-95
slinalg.py
pytensor/tensor/slinalg.py
+47
-91
test_nlinalg.py
tests/link/numba/test_nlinalg.py
+3
-3
test_slinalg.py
tests/tensor/test_slinalg.py
+1
-1
没有找到文件。
pytensor/tensor/slinalg.py
浏览文件 @
a6975da3
...
@@ -49,7 +49,7 @@ class Cholesky(Op):
...
@@ -49,7 +49,7 @@ class Cholesky(Op):
__props__
=
(
"lower"
,
"destructive"
,
"on_error"
)
__props__
=
(
"lower"
,
"destructive"
,
"on_error"
)
def
__init__
(
self
,
lower
=
True
,
on_error
=
"raise"
):
def
__init__
(
self
,
*
,
lower
=
True
,
on_error
=
"raise"
):
self
.
lower
=
lower
self
.
lower
=
lower
self
.
destructive
=
False
self
.
destructive
=
False
if
on_error
not
in
(
"raise"
,
"nan"
):
if
on_error
not
in
(
"raise"
,
"nan"
):
...
@@ -127,77 +127,8 @@ class Cholesky(Op):
...
@@ -127,77 +127,8 @@ class Cholesky(Op):
return
[
grad
]
return
[
grad
]
cholesky
=
Cholesky
()
def
cholesky
(
x
,
lower
=
True
,
on_error
=
"raise"
):
return
Cholesky
(
lower
=
lower
,
on_error
=
on_error
)(
x
)
class
CholeskySolve
(
Op
):
__props__
=
(
"lower"
,
"check_finite"
)
def
__init__
(
self
,
lower
=
True
,
check_finite
=
True
,
):
self
.
lower
=
lower
self
.
check_finite
=
check_finite
def
__repr__
(
self
):
return
"CholeskySolve{
%
s}"
%
str
(
self
.
_props
())
def
make_node
(
self
,
C
,
b
):
C
=
as_tensor_variable
(
C
)
b
=
as_tensor_variable
(
b
)
assert
C
.
ndim
==
2
assert
b
.
ndim
in
(
1
,
2
)
# infer dtype by solving the most simple
# case with (1, 1) matrices
o_dtype
=
scipy
.
linalg
.
solve
(
np
.
eye
(
1
)
.
astype
(
C
.
dtype
),
np
.
eye
(
1
)
.
astype
(
b
.
dtype
)
)
.
dtype
x
=
tensor
(
dtype
=
o_dtype
,
shape
=
b
.
type
.
shape
)
return
Apply
(
self
,
[
C
,
b
],
[
x
])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
C
,
b
=
inputs
rval
=
scipy
.
linalg
.
cho_solve
(
(
C
,
self
.
lower
),
b
,
check_finite
=
self
.
check_finite
,
)
output_storage
[
0
][
0
]
=
rval
def
infer_shape
(
self
,
fgraph
,
node
,
shapes
):
Cshape
,
Bshape
=
shapes
rows
=
Cshape
[
1
]
if
len
(
Bshape
)
==
1
:
# b is a Vector
return
[(
rows
,)]
else
:
cols
=
Bshape
[
1
]
# b is a Matrix
return
[(
rows
,
cols
)]
cho_solve
=
CholeskySolve
()
def
cho_solve
(
c_and_lower
,
b
,
check_finite
=
True
):
"""Solve the linear equations A x = b, given the Cholesky factorization of A.
Parameters
----------
(c, lower) : tuple, (array, bool)
Cholesky factorization of a, as given by cho_factor
b : array
Right-hand side
check_finite : bool, optional
Whether to check that the input matrices contain only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
"""
A
,
lower
=
c_and_lower
return
CholeskySolve
(
lower
=
lower
,
check_finite
=
check_finite
)(
A
,
b
)
class
SolveBase
(
Op
):
class
SolveBase
(
Op
):
...
@@ -210,6 +141,7 @@ class SolveBase(Op):
...
@@ -210,6 +141,7 @@ class SolveBase(Op):
def
__init__
(
def
__init__
(
self
,
self
,
*
,
lower
=
False
,
lower
=
False
,
check_finite
=
True
,
check_finite
=
True
,
):
):
...
@@ -276,28 +208,56 @@ class SolveBase(Op):
...
@@ -276,28 +208,56 @@ class SolveBase(Op):
return
[
A_bar
,
b_bar
]
return
[
A_bar
,
b_bar
]
def
__repr__
(
self
):
return
f
"{type(self).__name__}{self._props()}"
class
CholeskySolve
(
SolveBase
):
def
__init__
(
self
,
**
kwargs
):
kwargs
.
setdefault
(
"lower"
,
True
)
super
()
.
__init__
(
**
kwargs
)
def
perform
(
self
,
node
,
inputs
,
output_storage
):
C
,
b
=
inputs
rval
=
scipy
.
linalg
.
cho_solve
(
(
C
,
self
.
lower
),
b
,
check_finite
=
self
.
check_finite
,
)
output_storage
[
0
][
0
]
=
rval
def
L_op
(
self
,
*
args
,
**
kwargs
):
raise
NotImplementedError
()
def
cho_solve
(
c_and_lower
,
b
,
*
,
check_finite
=
True
):
"""Solve the linear equations A x = b, given the Cholesky factorization of A.
Parameters
----------
(c, lower) : tuple, (array, bool)
Cholesky factorization of a, as given by cho_factor
b : array
Right-hand side
check_finite : bool, optional
Whether to check that the input matrices contain only finite numbers.
Disabling may give a performance gain, but may result in problems
(crashes, non-termination) if the inputs do contain infinities or NaNs.
"""
A
,
lower
=
c_and_lower
return
CholeskySolve
(
lower
=
lower
,
check_finite
=
check_finite
)(
A
,
b
)
class
SolveTriangular
(
SolveBase
):
class
SolveTriangular
(
SolveBase
):
"""Solve a system of linear equations."""
"""Solve a system of linear equations."""
__props__
=
(
__props__
=
(
"lower"
,
"trans"
,
"trans"
,
"unit_diagonal"
,
"unit_diagonal"
,
"lower"
,
"check_finite"
,
"check_finite"
,
)
)
def
__init__
(
def
__init__
(
self
,
*
,
trans
=
0
,
unit_diagonal
=
False
,
**
kwargs
):
self
,
super
()
.
__init__
(
**
kwargs
)
trans
=
0
,
lower
=
False
,
unit_diagonal
=
False
,
check_finite
=
True
,
):
super
()
.
__init__
(
lower
=
lower
,
check_finite
=
check_finite
)
self
.
trans
=
trans
self
.
trans
=
trans
self
.
unit_diagonal
=
unit_diagonal
self
.
unit_diagonal
=
unit_diagonal
...
@@ -326,6 +286,7 @@ class SolveTriangular(SolveBase):
...
@@ -326,6 +286,7 @@ class SolveTriangular(SolveBase):
def
solve_triangular
(
def
solve_triangular
(
a
:
TensorVariable
,
a
:
TensorVariable
,
b
:
TensorVariable
,
b
:
TensorVariable
,
*
,
trans
:
Union
[
int
,
str
]
=
0
,
trans
:
Union
[
int
,
str
]
=
0
,
lower
:
bool
=
False
,
lower
:
bool
=
False
,
unit_diagonal
:
bool
=
False
,
unit_diagonal
:
bool
=
False
,
...
@@ -373,16 +334,11 @@ class Solve(SolveBase):
...
@@ -373,16 +334,11 @@ class Solve(SolveBase):
"check_finite"
,
"check_finite"
,
)
)
def
__init__
(
def
__init__
(
self
,
*
,
assume_a
=
"gen"
,
**
kwargs
):
self
,
assume_a
=
"gen"
,
lower
=
False
,
check_finite
=
True
,
):
if
assume_a
not
in
(
"gen"
,
"sym"
,
"her"
,
"pos"
):
if
assume_a
not
in
(
"gen"
,
"sym"
,
"her"
,
"pos"
):
raise
ValueError
(
f
"{assume_a} is not a recognized matrix structure"
)
raise
ValueError
(
f
"{assume_a} is not a recognized matrix structure"
)
super
()
.
__init__
(
lower
=
lower
,
check_finite
=
check_finite
)
super
()
.
__init__
(
**
kwargs
)
self
.
assume_a
=
assume_a
self
.
assume_a
=
assume_a
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
...
@@ -396,7 +352,7 @@ class Solve(SolveBase):
...
@@ -396,7 +352,7 @@ class Solve(SolveBase):
)
)
def
solve
(
a
,
b
,
assume_a
=
"gen"
,
lower
=
False
,
check_finite
=
True
):
def
solve
(
a
,
b
,
*
,
assume_a
=
"gen"
,
lower
=
False
,
check_finite
=
True
):
"""Solves the linear equation set ``a * x = b`` for the unknown ``x`` for square ``a`` matrix.
"""Solves the linear equation set ``a * x = b`` for the unknown ``x`` for square ``a`` matrix.
If the data matrix is known to be a particular type then supplying the
If the data matrix is known to be a particular type then supplying the
...
...
tests/link/numba/test_nlinalg.py
浏览文件 @
a6975da3
...
@@ -46,7 +46,7 @@ rng = np.random.default_rng(42849)
...
@@ -46,7 +46,7 @@ rng = np.random.default_rng(42849)
],
],
)
)
def
test_Cholesky
(
x
,
lower
,
exc
):
def
test_Cholesky
(
x
,
lower
,
exc
):
g
=
slinalg
.
Cholesky
(
lower
)(
x
)
g
=
slinalg
.
Cholesky
(
lower
=
lower
)(
x
)
if
isinstance
(
g
,
list
):
if
isinstance
(
g
,
list
):
g_fg
=
FunctionGraph
(
outputs
=
g
)
g_fg
=
FunctionGraph
(
outputs
=
g
)
...
@@ -91,7 +91,7 @@ def test_Cholesky(x, lower, exc):
...
@@ -91,7 +91,7 @@ def test_Cholesky(x, lower, exc):
],
],
)
)
def
test_Solve
(
A
,
x
,
lower
,
exc
):
def
test_Solve
(
A
,
x
,
lower
,
exc
):
g
=
slinalg
.
Solve
(
lower
)(
A
,
x
)
g
=
slinalg
.
Solve
(
lower
=
lower
)(
A
,
x
)
if
isinstance
(
g
,
list
):
if
isinstance
(
g
,
list
):
g_fg
=
FunctionGraph
(
outputs
=
g
)
g_fg
=
FunctionGraph
(
outputs
=
g
)
...
@@ -125,7 +125,7 @@ def test_Solve(A, x, lower, exc):
...
@@ -125,7 +125,7 @@ def test_Solve(A, x, lower, exc):
],
],
)
)
def
test_SolveTriangular
(
A
,
x
,
lower
,
exc
):
def
test_SolveTriangular
(
A
,
x
,
lower
,
exc
):
g
=
slinalg
.
SolveTriangular
(
lower
)(
A
,
x
)
g
=
slinalg
.
SolveTriangular
(
lower
=
lower
)(
A
,
x
)
if
isinstance
(
g
,
list
):
if
isinstance
(
g
,
list
):
g_fg
=
FunctionGraph
(
outputs
=
g
)
g_fg
=
FunctionGraph
(
outputs
=
g
)
...
...
tests/tensor/test_slinalg.py
浏览文件 @
a6975da3
...
@@ -361,7 +361,7 @@ class TestCholeskySolve(utt.InferShapeTester):
...
@@ -361,7 +361,7 @@ class TestCholeskySolve(utt.InferShapeTester):
super
()
.
setup_method
()
super
()
.
setup_method
()
def
test_repr
(
self
):
def
test_repr
(
self
):
assert
repr
(
CholeskySolve
())
==
"CholeskySolve
{(True, True)}
"
assert
repr
(
CholeskySolve
())
==
"CholeskySolve
(lower=True,check_finite=True)
"
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论