Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d4f87436
提交
d4f87436
authored
3月 11, 2026
作者:
ricardoV94
提交者:
Ricardo Vieira
3月 11, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix connection_pattern for Minimize ops
上级
91b73211
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
32 行增加
和
16 行删除
+32
-16
optimize.py
pytensor/tensor/optimize.py
+32
-16
没有找到文件。
pytensor/tensor/optimize.py
浏览文件 @
d4f87436
...
@@ -213,22 +213,6 @@ class ScipyWrapperOp(Op, HasInnerGraph):
...
@@ -213,22 +213,6 @@ class ScipyWrapperOp(Op, HasInnerGraph):
return
Apply
(
self
,
inputs
,
[
self
.
inner_inputs
[
0
]
.
type
(),
ps
.
bool
(
"success"
)])
return
Apply
(
self
,
inputs
,
[
self
.
inner_inputs
[
0
]
.
type
(),
ps
.
bool
(
"success"
)])
def
connection_pattern
(
self
,
node
=
None
):
"""
All Ops that inherit from ScipyWrapperOp share the same connection pattern logic, because they all share the
same output structure. There are two outputs: the optimized variable, and a success flag. The success flag is
not differentiable, so it is never connected. The optimized variable is connected only to inputs that are
both connected to the objective function and of float dtype.
"""
fgraph
=
self
.
fgraph
fx
=
fgraph
.
outputs
[
0
]
return
[
# Every input is disonnected to the second output (success)
# And may or not be connected to the first output (opt_x)
[
connected
,
False
]
for
[
connected
]
in
io_connection_pattern
(
fgraph
.
inputs
,
[
fx
])
]
class
ScipyScalarWrapperOp
(
ScipyWrapperOp
):
class
ScipyScalarWrapperOp
(
ScipyWrapperOp
):
def
build_fn
(
self
):
def
build_fn
(
self
):
...
@@ -474,6 +458,26 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -474,6 +458,26 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
return
final_grads
return
final_grads
def
_optimizer_connection_pattern
(
fgraph
,
is_minimization
):
"""Compute connection pattern for scipy optimization Ops.
There are two outputs: the optimized variable, and a success flag. The success flag is
not differentiable, so it is never connected. The optimized variable is connected only
to inputs that are connected to the implicit function used in the gradient computation.
For minimization, the implicit function is grad(objective, x), not the objective itself.
An input may be connected to the objective but disconnected from its gradient (e.g. an
additive constant), so the connection pattern must reflect the actual implicit function.
"""
inner_x
=
fgraph
.
inputs
[
0
]
fx
=
fgraph
.
outputs
[
0
]
if
is_minimization
:
fx
=
grad
(
fx
,
inner_x
)
return
[
[
connected
,
False
]
for
[
connected
]
in
io_connection_pattern
(
fgraph
.
inputs
,
[
fx
])
]
class
MinimizeScalarOp
(
ScipyScalarWrapperOp
):
class
MinimizeScalarOp
(
ScipyScalarWrapperOp
):
def
__init__
(
def
__init__
(
self
,
self
,
...
@@ -502,6 +506,9 @@ class MinimizeScalarOp(ScipyScalarWrapperOp):
...
@@ -502,6 +506,9 @@ class MinimizeScalarOp(ScipyScalarWrapperOp):
self
.
_fn
=
None
self
.
_fn
=
None
self
.
_fn_wrapped
=
None
self
.
_fn_wrapped
=
None
def
connection_pattern
(
self
,
node
=
None
):
return
_optimizer_connection_pattern
(
self
.
fgraph
,
is_minimization
=
True
)
def
__str__
(
self
):
def
__str__
(
self
):
return
f
"{self.__class__.__name__}(method={self.method})"
return
f
"{self.__class__.__name__}(method={self.method})"
...
@@ -638,6 +645,9 @@ class MinimizeOp(ScipyVectorWrapperOp):
...
@@ -638,6 +645,9 @@ class MinimizeOp(ScipyVectorWrapperOp):
self
.
_fn
=
None
self
.
_fn
=
None
self
.
_fn_wrapped
=
None
self
.
_fn_wrapped
=
None
def
connection_pattern
(
self
,
node
=
None
):
return
_optimizer_connection_pattern
(
self
.
fgraph
,
is_minimization
=
True
)
def
__str__
(
self
):
def
__str__
(
self
):
str_args
=
", "
.
join
(
str_args
=
", "
.
join
(
[
[
...
@@ -833,6 +843,9 @@ class RootScalarOp(ScipyScalarWrapperOp):
...
@@ -833,6 +843,9 @@ class RootScalarOp(ScipyScalarWrapperOp):
self
.
_fn
=
None
self
.
_fn
=
None
self
.
_fn_wrapped
=
None
self
.
_fn_wrapped
=
None
def
connection_pattern
(
self
,
node
=
None
):
return
_optimizer_connection_pattern
(
self
.
fgraph
,
is_minimization
=
False
)
def
__str__
(
self
):
def
__str__
(
self
):
str_args
=
", "
.
join
(
str_args
=
", "
.
join
(
[
f
"{arg}={getattr(self, arg)}"
for
arg
in
[
"method"
,
"jac"
,
"hess"
]]
[
f
"{arg}={getattr(self, arg)}"
for
arg
in
[
"method"
,
"jac"
,
"hess"
]]
...
@@ -979,6 +992,9 @@ class RootOp(ScipyVectorWrapperOp):
...
@@ -979,6 +992,9 @@ class RootOp(ScipyVectorWrapperOp):
self
.
_fn
=
None
self
.
_fn
=
None
self
.
_fn_wrapped
=
None
self
.
_fn_wrapped
=
None
def
connection_pattern
(
self
,
node
=
None
):
return
_optimizer_connection_pattern
(
self
.
fgraph
,
is_minimization
=
False
)
def
__str__
(
self
):
def
__str__
(
self
):
str_args
=
", "
.
join
(
str_args
=
", "
.
join
(
[
f
"{arg}={getattr(self, arg)}"
for
arg
in
[
"method"
,
"jac"
]]
[
f
"{arg}={getattr(self, arg)}"
for
arg
in
[
"method"
,
"jac"
]]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论