Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a3bf6bb6
提交
a3bf6bb6
authored
1月 12, 2026
作者:
jessegrabowski
提交者:
Jesse Grabowski
1月 18, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add TRSYL Op, refactor linear control Ops
上级
afabe83f
全部展开
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
78 行增加
和
52 行删除
+78
-52
linalg.py
pytensor/tensor/rewriting/linalg.py
+3
-3
slinalg.py
pytensor/tensor/slinalg.py
+0
-0
test_slinalg.py
tests/tensor/test_slinalg.py
+75
-49
没有找到文件。
pytensor/tensor/rewriting/linalg.py
浏览文件 @
a3bf6bb6
...
@@ -55,8 +55,8 @@ from pytensor.tensor.slinalg import (
...
@@ -55,8 +55,8 @@ from pytensor.tensor.slinalg import (
LUFactor
,
LUFactor
,
Solve
,
Solve
,
SolveBase
,
SolveBase
,
SolveBilinearDiscreteLyapunov
,
SolveTriangular
,
SolveTriangular
,
_bilinear_solve_discrete_lyapunov
,
block_diag
,
block_diag
,
cholesky
,
cholesky
,
solve
,
solve
,
...
@@ -1045,10 +1045,10 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
...
@@ -1045,10 +1045,10 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
return
[
eye_input
*
(
non_eye_input
**
0.5
)]
return
[
eye_input
*
(
non_eye_input
**
0.5
)]
@node_rewriter
([
_bilinear_solve_discrete_l
yapunov
])
@node_rewriter
([
SolveBilinearDiscreteL
yapunov
])
def
jax_bilinaer_lyapunov_to_direct
(
fgraph
:
FunctionGraph
,
node
:
Apply
):
def
jax_bilinaer_lyapunov_to_direct
(
fgraph
:
FunctionGraph
,
node
:
Apply
):
"""
"""
Replace
BilinearSolve
DiscreteLyapunov with a direct computation that is supported by JAX
Replace
SolveBilinear
DiscreteLyapunov with a direct computation that is supported by JAX
"""
"""
A
,
B
=
(
cast
(
TensorVariable
,
x
)
for
x
in
node
.
inputs
)
A
,
B
=
(
cast
(
TensorVariable
,
x
)
for
x
in
node
.
inputs
)
result
=
solve_discrete_lyapunov
(
A
,
B
,
method
=
"direct"
)
result
=
solve_discrete_lyapunov
(
A
,
B
,
method
=
"direct"
)
...
...
pytensor/tensor/slinalg.py
浏览文件 @
a3bf6bb6
差异被折叠。
点击展开。
tests/tensor/test_slinalg.py
浏览文件 @
a3bf6bb6
...
@@ -36,6 +36,7 @@ from pytensor.tensor.slinalg import (
...
@@ -36,6 +36,7 @@ from pytensor.tensor.slinalg import (
solve_continuous_lyapunov
,
solve_continuous_lyapunov
,
solve_discrete_are
,
solve_discrete_are
,
solve_discrete_lyapunov
,
solve_discrete_lyapunov
,
solve_sylvester
,
solve_triangular
,
solve_triangular
,
)
)
from
pytensor.tensor.type
import
dmatrix
,
matrix
,
tensor
,
vector
from
pytensor.tensor.type
import
dmatrix
,
matrix
,
tensor
,
vector
...
@@ -916,6 +917,67 @@ def test_expm_grad(mode):
...
@@ -916,6 +917,67 @@ def test_expm_grad(mode):
utt
.
verify_grad
(
expm
,
[
A
],
rng
=
rng
,
abs_tol
=
1e-5
,
rel_tol
=
1e-5
)
utt
.
verify_grad
(
expm
,
[
A
],
rng
=
rng
,
abs_tol
=
1e-5
,
rel_tol
=
1e-5
)
@pytest.mark.parametrize
(
"shape, use_complex"
,
[((
5
,
5
),
False
),
((
5
,
5
),
True
),
((
5
,
5
,
5
),
False
)],
ids
=
[
"float"
,
"complex"
,
"batch_float"
],
)
def
test_solve_continuous_sylvester
(
shape
:
tuple
[
int
],
use_complex
:
bool
):
# batch-complex case got an error from BatchedDot not implemented for complex numbers
rng
=
np
.
random
.
default_rng
()
dtype
=
config
.
floatX
if
use_complex
:
dtype
=
"complex128"
if
dtype
==
"float64"
else
"complex64"
A1
,
A2
=
rng
.
normal
(
size
=
(
2
,
*
shape
))
B1
,
B2
=
rng
.
normal
(
size
=
(
2
,
*
shape
))
Q1
,
Q2
=
rng
.
normal
(
size
=
(
2
,
*
shape
))
if
use_complex
:
A_val
=
A1
+
1
j
*
A2
B_val
=
B1
+
1
j
*
B2
Q_val
=
Q1
+
1
j
*
Q2
else
:
A_val
=
A1
B_val
=
B1
Q_val
=
Q1
A
=
pt
.
tensor
(
"A"
,
shape
=
shape
,
dtype
=
dtype
)
B
=
pt
.
tensor
(
"B"
,
shape
=
shape
,
dtype
=
dtype
)
Q
=
pt
.
tensor
(
"Q"
,
shape
=
shape
,
dtype
=
dtype
)
X
=
solve_sylvester
(
A
,
B
,
Q
)
Q_recovered
=
A
@
X
+
X
@
B
fn
=
function
([
A
,
B
,
Q
],
[
X
,
Q_recovered
])
X_val
,
Q_recovered_val
=
fn
(
A_val
,
B_val
,
Q_val
)
vec_sylvester
=
np
.
vectorize
(
scipy_linalg
.
solve_sylvester
,
signature
=
"(m,m),(m,m),(m,m)->(m,m)"
)
np
.
testing
.
assert_allclose
(
Q_recovered_val
,
Q_val
,
atol
=
1e-8
,
rtol
=
1e-8
)
np
.
testing
.
assert_allclose
(
X_val
,
vec_sylvester
(
A_val
,
B_val
,
Q_val
),
atol
=
1e-8
,
rtol
=
1e-8
)
@pytest.mark.parametrize
(
"shape"
,
[(
5
,
5
),
(
5
,
5
,
5
)],
ids
=
[
"matrix"
,
"batched"
])
@pytest.mark.parametrize
(
"use_complex"
,
[
False
,
True
],
ids
=
[
"float"
,
"complex"
])
def
test_solve_continuous_sylvester_grad
(
shape
:
tuple
[
int
],
use_complex
):
if
config
.
floatX
==
"float32"
:
pytest
.
skip
(
reason
=
"Not enough precision in float32 to get a good gradient"
)
if
use_complex
:
pytest
.
skip
(
reason
=
"Complex numbers are not supported in the gradient test"
)
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
A
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
B
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
Q
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
utt
.
verify_grad
(
solve_sylvester
,
pt
=
[
A
,
B
,
Q
],
rng
=
rng
)
def
recover_Q
(
A
,
X
,
continuous
=
True
):
def
recover_Q
(
A
,
X
,
continuous
=
True
):
if
continuous
:
if
continuous
:
return
A
@
X
+
X
@
A
.
conj
()
.
T
return
A
@
X
+
X
@
A
.
conj
()
.
T
...
@@ -985,60 +1047,24 @@ def test_solve_discrete_lyapunov_gradient(
...
@@ -985,60 +1047,24 @@ def test_solve_discrete_lyapunov_gradient(
)
)
@pytest.mark.parametrize
(
"shape"
,
[(
5
,
5
),
(
5
,
5
,
5
)],
ids
=
[
"matrix"
,
"batched"
])
def
test_solve_continuous_lyapunov
():
@pytest.mark.parametrize
(
"use_complex"
,
[
False
,
True
],
ids
=
[
"float"
,
"complex"
])
# solve_continuous_lyapunov just calls solve_sylvester, so extensive tests are not needed.
def
test_solve_continuous_lyapunov
(
shape
:
tuple
[
int
],
use_complex
:
bool
):
A
=
pt
.
tensor
(
"A"
,
shape
=
(
3
,
5
,
5
))
dtype
=
config
.
floatX
Q
=
pt
.
tensor
(
"Q"
,
shape
=
(
3
,
5
,
5
))
if
use_complex
and
dtype
==
"float32"
:
pytest
.
skip
(
"Not enough precision in complex64 to do schur decomposition "
"(ill-conditioned matrix errors arise)"
)
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
if
use_complex
:
precision
=
int
(
dtype
[
-
2
:])
# 64 or 32
dtype
=
f
"complex{int(2 * precision)}"
A1
,
A2
=
rng
.
normal
(
size
=
(
2
,
*
shape
))
Q1
,
Q2
=
rng
.
normal
(
size
=
(
2
,
*
shape
))
if
use_complex
:
A
=
A1
+
1
j
*
A2
Q
=
Q1
+
1
j
*
Q2
else
:
A
=
A1
Q
=
Q1
A
,
Q
=
A
.
astype
(
dtype
),
Q
.
astype
(
dtype
)
a
=
pt
.
tensor
(
name
=
"a"
,
shape
=
shape
,
dtype
=
dtype
)
q
=
pt
.
tensor
(
name
=
"q"
,
shape
=
shape
,
dtype
=
dtype
)
x
=
solve_continuous_lyapunov
(
a
,
q
)
f
=
function
([
a
,
q
],
x
)
X
=
f
(
A
,
Q
)
Q_recovered
=
vec_recover_Q
(
A
,
X
,
continuous
=
True
)
atol
=
rtol
=
1e-2
if
config
.
floatX
==
"float32"
else
1e-8
np
.
testing
.
assert_allclose
(
Q_recovered
.
squeeze
(),
Q
,
atol
=
atol
,
rtol
=
rtol
)
X
=
solve_continuous_lyapunov
(
A
,
Q
)
Q_recovered
=
A
@
X
+
X
@
A
.
conj
()
.
mT
@pytest.mark.parametrize
(
"shape"
,
[(
5
,
5
),
(
5
,
5
,
5
)],
ids
=
[
"matrix"
,
"batched"
])
fn
=
function
([
A
,
Q
],
[
X
,
Q_recovered
])
@pytest.mark.parametrize
(
"use_complex"
,
[
False
,
True
],
ids
=
[
"float"
,
"complex"
])
def
test_solve_continuous_lyapunov_grad
(
shape
:
tuple
[
int
],
use_complex
):
if
config
.
floatX
==
"float32"
:
pytest
.
skip
(
reason
=
"Not enough precision in float32 to get a good gradient"
)
if
use_complex
:
pytest
.
skip
(
reason
=
"Complex numbers are not supported in the gradient test"
)
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
rng
=
np
.
random
.
default_rng
(
utt
.
fetch_seed
())
A
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
A_val
=
rng
.
normal
(
size
=
(
3
,
5
,
5
))
.
astype
(
config
.
floatX
)
Q
=
rng
.
normal
(
size
=
shape
)
.
astype
(
config
.
floatX
)
Q_val
=
rng
.
normal
(
size
=
(
3
,
5
,
5
))
.
astype
(
config
.
floatX
)
_
,
Q_recovered_val
=
fn
(
A_val
,
Q_val
)
utt
.
verify_grad
(
solve_continuous_lyapunov
,
pt
=
[
A
,
Q
],
rng
=
rng
)
atol
=
rtol
=
1e-2
if
config
.
floatX
==
"float32"
else
1e-8
np
.
testing
.
assert_allclose
(
Q_recovered_val
,
Q_val
,
atol
=
atol
,
rtol
=
rtol
)
utt
.
verify_grad
(
solve_continuous_lyapunov
,
pt
=
[
A_val
,
Q_val
],
rng
=
rng
)
@pytest.mark.parametrize
(
"add_batch_dim"
,
[
False
,
True
])
@pytest.mark.parametrize
(
"add_batch_dim"
,
[
False
,
True
])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论