Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
91b73211
提交
91b73211
authored
3月 08, 2026
作者:
ricardoV94
提交者:
Ricardo Vieira
3月 11, 2026
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tweak ScipyVectorWrapperOp compute_implicit_gradients for readability
上级
ac942196
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
13 行增加
和
14 行删除
+13
-14
optimize.py
pytensor/tensor/optimize.py
+13
-14
没有找到文件。
pytensor/tensor/optimize.py
浏览文件 @
91b73211
...
@@ -361,7 +361,7 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -361,7 +361,7 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
Notes
Notes
-----
-----
The grad
ents are computed using the implicit function theorem. Given a fu
ction `f(x, theta) = 0`, and a function
The grad
ients are computed using the implicit function theorem. Given a fun
ction `f(x, theta) = 0`, and a function
`x_star(theta)` that, given input parameters theta returns `x` such that `f(x_star(theta), theta) = 0`, we can
`x_star(theta)` that, given input parameters theta returns `x` such that `f(x_star(theta), theta) = 0`, we can
compute the gradients of `x_star` with respect to `theta` as follows:
compute the gradients of `x_star` with respect to `theta` as follows:
...
@@ -387,8 +387,12 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -387,8 +387,12 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
fgraph
=
self
.
fgraph
fgraph
=
self
.
fgraph
inner_x
,
*
inner_args
=
self
.
inner_inputs
inner_x
,
*
inner_args
=
self
.
inner_inputs
implicit_f
=
self
.
inner_outputs
[
0
]
implicit_f
=
self
.
inner_outputs
[
0
]
if
is_minimization
:
# The implicit function in minimization is grad(x, theta) == 0
implicit_f
=
grad
(
implicit_f
,
inner_x
)
df_dx
,
*
arg_grads
=
grad
(
# Call grad to see what arguments are connected
_
,
*
arg_grads
=
grad
(
implicit_f
.
sum
(),
implicit_f
.
sum
(),
[
inner_x
,
*
inner_args
],
[
inner_x
,
*
inner_args
],
disconnected_inputs
=
"ignore"
,
disconnected_inputs
=
"ignore"
,
...
@@ -404,9 +408,6 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -404,9 +408,6 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
# No differentiable arguments, return disconnected/null gradients
# No differentiable arguments, return disconnected/null gradients
return
arg_grads
return
arg_grads
if
is_minimization
:
implicit_f
=
grad
(
implicit_f
,
inner_x
)
# Gradients are computed using the inner graph of the optimization op, not the actual inputs/outputs of the op.
# Gradients are computed using the inner graph of the optimization op, not the actual inputs/outputs of the op.
packed_inner_args
,
packed_arg_shapes
,
implicit_f
=
pack_inputs_of_objective
(
packed_inner_args
,
packed_arg_shapes
,
implicit_f
=
pack_inputs_of_objective
(
implicit_f
,
implicit_f
,
...
@@ -426,11 +427,11 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -426,11 +427,11 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
# Replace inner inputs (abstract dummies) with outer inputs (the actual user-provided symbols)
# Replace inner inputs (abstract dummies) with outer inputs (the actual user-provided symbols)
# at the solution point. Innner arguments aren't needed anymore, delete them to avoid accidental references.
# at the solution point. Innner arguments aren't needed anymore, delete them to avoid accidental references.
del
inner_x
del
inner_x
,
inner_args
del
inner_args
inner_to_outer_map
=
tuple
(
zip
(
fgraph
.
inputs
,
(
x_star
,
*
args
)))
inner_to_outer_map
=
tuple
(
zip
(
fgraph
.
inputs
,
(
x_star
,
*
args
)))
df_dx_star
,
df_dtheta_star
=
graph_replace
(
df_dx_star
,
df_dtheta_star
=
graph_replace
(
[
df_dx
,
df_dtheta
],
inner_to_outer_map
[
df_dx
,
df_dtheta
],
replace
=
inner_to_outer_map
,
)
)
if
df_dtheta_star
.
ndim
==
0
or
df_dx_star
.
ndim
==
0
:
if
df_dtheta_star
.
ndim
==
0
or
df_dx_star
.
ndim
==
0
:
...
@@ -455,13 +456,11 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
...
@@ -455,13 +456,11 @@ class ScipyVectorWrapperOp(ScipyWrapperOp):
for
i
,
(
arg
,
to_diff
)
in
enumerate
(
zip
(
args
,
args_to_diff
)):
for
i
,
(
arg
,
to_diff
)
in
enumerate
(
zip
(
args
,
args_to_diff
)):
if
not
to_diff
:
if
not
to_diff
:
# Store the null grad we got from the initial `grad` call
# Store the null grad we got from the initial `grad` call
null_grad
=
arg_grads
[
i
]
g
=
arg_grads
[
i
]
assert
isinstance
(
null_grad
.
type
,
NullType
|
DisconnectedType
)
assert
isinstance
(
g
.
type
,
NullType
|
DisconnectedType
)
final_grads
.
append
(
null_grad
)
else
:
continue
# Compute non-null grad and chain with output_grad
arg_grad
=
next
(
grad_wrt_args_iter
)
arg_grad
=
next
(
grad_wrt_args_iter
)
if
arg_grad
.
ndim
>
0
and
output_grad
.
ndim
>
0
:
if
arg_grad
.
ndim
>
0
and
output_grad
.
ndim
>
0
:
g
=
tensordot
(
output_grad
,
arg_grad
,
[[
0
],
[
0
]])
g
=
tensordot
(
output_grad
,
arg_grad
,
[[
0
],
[
0
]])
else
:
else
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论