Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
22cda11a
提交
22cda11a
authored
11月 21, 2025
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
12月 03, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Handle upcasting of scalar to vector arrays by scipy vector optimizers
上级
e126020a
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
51 行增加
和
10 行删除
+51
-10
optimize.py
pytensor/tensor/optimize.py
+22
-10
test_optimize.py
tests/tensor/test_optimize.py
+29
-0
没有找到文件。
pytensor/tensor/optimize.py
浏览文件 @
22cda11a
...
...
@@ -233,14 +233,8 @@ class ScipyWrapperOp(Op, HasInnerGraph):
class
ScipyScalarWrapperOp
(
ScipyWrapperOp
):
def
build_fn
(
self
):
"""
This is overloaded because scipy converts scalar inputs to lists, changing the return type. The
wrapper function logic is there to handle this.
"""
# We have no control over the inputs to the scipy inner function for scalar_minimize. As a result,
# we need to adjust the graph to work with what scipy will be passing into the inner function --
# always scalar, and always float64
# We need to adjust the graph to work with what scipy will be passing into the inner function --
# always scalar array of float64 type
x
,
*
args
=
self
.
inner_inputs
new_root_x
=
ps
.
float64
(
name
=
"x_scalar"
)
new_x
=
tensor_from_scalar
(
new_root_x
.
astype
(
x
.
type
.
dtype
))
...
...
@@ -255,6 +249,24 @@ class ScipyScalarWrapperOp(ScipyWrapperOp):
self
.
_fn_wrapped
=
LRUCache1
(
fn
)
class
ScipyVectorWrapperOp
(
ScipyWrapperOp
):
def
build_fn
(
self
):
# We need to adjust the graph to work with what scipy will be passing into the inner function --
# always a vector array with size of at least 1
x
,
*
args
=
self
.
inner_inputs
if
x
.
type
.
shape
!=
():
return
super
()
.
build_fn
()
new_root_x
=
x
[
None
]
.
type
()
new_x
=
new_root_x
.
squeeze
()
new_outputs
=
graph_replace
(
self
.
inner_outputs
,
{
x
:
new_x
})
self
.
_fn
=
fn
=
function
([
new_root_x
,
*
args
],
new_outputs
,
trust_input
=
True
)
# Do this reassignment to see the compiled graph in the dprint
# self.fgraph = fn.maker.fgraph
self
.
_fn_wrapped
=
LRUCache1
(
fn
)
def
scalar_implict_optimization_grads
(
inner_fx
:
Variable
,
inner_x
:
Variable
,
...
...
@@ -474,7 +486,7 @@ def minimize_scalar(
return
solution
,
success
class
MinimizeOp
(
ScipyWrapperOp
):
class
MinimizeOp
(
Scipy
Vector
WrapperOp
):
def
__init__
(
self
,
x
:
Variable
,
...
...
@@ -808,7 +820,7 @@ def root_scalar(
return
solution
,
success
class
RootOp
(
ScipyWrapperOp
):
class
RootOp
(
Scipy
Vector
WrapperOp
):
__props__
=
(
"method"
,
"jac"
)
def
__init__
(
...
...
tests/tensor/test_optimize.py
浏览文件 @
22cda11a
...
...
@@ -4,6 +4,8 @@ import pytest
import
pytensor
import
pytensor.tensor
as
pt
from
pytensor
import
config
,
function
from
pytensor.graph
import
Apply
,
Op
from
pytensor.tensor
import
scalar
from
pytensor.tensor.optimize
import
minimize
,
minimize_scalar
,
root
,
root_scalar
from
tests
import
unittest_tools
as
utt
...
...
@@ -219,3 +221,30 @@ def test_root_system_of_equations():
utt
.
verify_grad
(
root_fn
,
[
x0
,
a_val
,
b_val
],
eps
=
1e-6
if
floatX
==
"float64"
else
1e-3
)
@pytest.mark.parametrize
(
"optimize_op"
,
(
minimize
,
root
))
def
test_minimize_0d
(
optimize_op
):
# Scipy vector minimizers upcast 0d x to 1d. We need to work-around this
class
AssertScalar
(
Op
):
view_map
=
{
0
:
[
0
]}
def
make_node
(
self
,
x
):
return
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
[
x
]
=
inputs
assert
x
.
ndim
==
0
output_storage
[
0
][
0
]
=
x
def
L_op
(
self
,
inputs
,
outputs
,
out_grads
):
return
out_grads
x
=
scalar
(
"x"
)
x_check
=
AssertScalar
()(
x
)
opt_x
,
_
=
optimize_op
(
x_check
**
2
,
x
)
opt_x_res
=
opt_x
.
eval
({
x
:
np
.
array
(
5
,
dtype
=
x
.
type
.
dtype
)})
np
.
testing
.
assert_allclose
(
opt_x_res
,
0
,
atol
=
1e-15
if
floatX
==
"float64"
else
1e-6
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论