Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5fd729d0
提交
5fd729d0
authored
6月 26, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
7月 08, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add helper to build hessian vector product
Co-authored-by:
Adrian Seyboldt
<
aseyboldt@users.noreply.github.com
>
上级
db1c161e
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
128 行增加
和
0 行删除
+128
-0
gradients.rst
doc/tutorial/gradients.rst
+10
-0
gradient.py
pytensor/gradient.py
+79
-0
test_gradient.py
tests/test_gradient.py
+39
-0
没有找到文件。
doc/tutorial/gradients.rst
浏览文件 @
5fd729d0
...
...
@@ -267,6 +267,16 @@ or, making use of the R-operator:
>>> f([4, 4], [2, 2])
array([ 4., 4.])
There is a builtin helper that uses the first method
>>> x = pt.dvector('x')
>>> v = pt.dvector('v')
>>> y = pt.sum(x ** 2)
>>> Hv = pytensor.gradient.hessian_vector_product(y, x, v)
>>> f = pytensor.function([x, v], Hv)
>>> f([4, 4], [2, 2])
array([ 4., 4.])
Final Pointers
==============
...
...
pytensor/gradient.py
浏览文件 @
5fd729d0
...
...
@@ -2050,6 +2050,85 @@ def hessian(cost, wrt, consider_constant=None, disconnected_inputs="raise"):
return
as_list_or_tuple
(
using_list
,
using_tuple
,
hessians
)
def
hessian_vector_product
(
cost
,
wrt
,
p
,
**
grad_kwargs
):
"""Return the expression of the Hessian times a vector p.
Notes
-----
This function uses backward autodiff twice to obtain the desired expression.
You may want to manually build the equivalent expression by combining backward
followed by forward (if all Ops support it) autodiff.
See {ref}`docs/_tutcomputinggrads#Hessian-times-a-Vector` for how to do this.
Parameters
----------
cost: Scalar (0-dimensional) variable.
wrt: Vector (1-dimensional tensor) 'Variable' or list of Vectors
p: Vector (1-dimensional tensor) 'Variable' or list of Vectors
Each vector will be used for the hessp wirt to exach input variable
**grad_kwargs:
Keyword arguments passed to `grad` function.
Returns
-------
:class:` Vector or list of Vectors
The Hessian times p of the `cost` with respect to (elements of) `wrt`.
Examples
--------
.. testcode::
import numpy as np
from scipy.optimize import minimize
from pytensor import function
from pytensor.tensor import vector
from pytensor.gradient import grad, hessian_vector_product
x = vector('x')
p = vector('p')
rosen = (100 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2).sum()
rosen_jac = grad(rosen, x)
rosen_hessp = hessian_vector_product(rosen, x, p)
rosen_fn = function([x], rosen)
rosen_jac_fn = function([x], rosen_jac)
rosen_hessp_fn = function([x, p], rosen_hessp)
x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
res = minimize(
rosen_fn,
x0,
method="Newton-CG",
jac=rosen_jac_fn,
hessp=rosen_hessp_fn,
options={"xtol": 1e-8},
)
print(res.x)
.. testoutput::
[1. 1. 1. 0.99999999 0.99999999]
"""
wrt_list
=
wrt
if
isinstance
(
wrt
,
Sequence
)
else
[
wrt
]
p_list
=
p
if
isinstance
(
p
,
Sequence
)
else
[
p
]
grad_wrt_list
=
grad
(
cost
,
wrt
=
wrt_list
,
**
grad_kwargs
)
hessian_cost
=
pytensor
.
tensor
.
add
(
*
[
(
grad_wrt
*
p
)
.
sum
()
for
grad_wrt
,
p
in
zip
(
grad_wrt_list
,
p_list
,
strict
=
True
)
]
)
Hp_list
=
grad
(
hessian_cost
,
wrt
=
wrt_list
,
**
grad_kwargs
)
if
isinstance
(
wrt
,
Variable
):
return
Hp_list
[
0
]
return
Hp_list
def
_is_zero
(
x
):
"""
Returns 'yes', 'no', or 'maybe' indicating whether x
...
...
tests/test_gradient.py
浏览文件 @
5fd729d0
import
numpy
as
np
import
pytest
from
scipy.optimize
import
rosen_hess_prod
import
pytensor
import
pytensor.tensor.basic
as
ptb
...
...
@@ -20,6 +21,7 @@ from pytensor.gradient import (
grad_scale
,
grad_undefined
,
hessian
,
hessian_vector_product
,
jacobian
,
subgraph_grad
,
zero_grad
,
...
...
@@ -1079,3 +1081,40 @@ def test_jacobian_disconnected_inputs():
func_s
=
pytensor
.
function
([
s2
],
jacobian_s
)
val
=
np
.
array
(
1.0
)
.
astype
(
pytensor
.
config
.
floatX
)
assert
np
.
allclose
(
func_s
(
val
),
np
.
zeros
(
1
))
class
TestHessianVectorProdudoct
:
def
test_rosen
(
self
):
x
=
vector
(
"x"
,
dtype
=
"float64"
)
rosen
=
(
100
*
(
x
[
1
:]
-
x
[:
-
1
]
**
2
)
**
2
+
(
1
-
x
[:
-
1
])
**
2
)
.
sum
()
p
=
vector
(
"p"
,
dtype
=
"float64"
)
rosen_hess_prod_pt
=
hessian_vector_product
(
rosen
,
wrt
=
x
,
p
=
p
)
x_test
=
0.1
*
np
.
arange
(
9
)
p_test
=
0.5
*
np
.
arange
(
9
)
np
.
testing
.
assert_allclose
(
rosen_hess_prod_pt
.
eval
({
x
:
x_test
,
p
:
p_test
}),
rosen_hess_prod
(
x_test
,
p_test
),
)
def
test_multiple_wrt
(
self
):
x
=
vector
(
"x"
,
dtype
=
"float64"
)
y
=
vector
(
"y"
,
dtype
=
"float64"
)
p_x
=
vector
(
"p_x"
,
dtype
=
"float64"
)
p_y
=
vector
(
"p_y"
,
dtype
=
"float64"
)
cost
=
(
x
**
2
-
y
**
2
)
.
sum
()
hessp_x
,
hessp_y
=
hessian_vector_product
(
cost
,
wrt
=
[
x
,
y
],
p
=
[
p_x
,
p_y
])
hessp_fn
=
pytensor
.
function
([
x
,
y
,
p_x
,
p_y
],
[
hessp_x
,
hessp_y
])
test
=
{
# x, y don't matter
"x"
:
np
.
full
((
3
,),
np
.
nan
),
"y"
:
np
.
full
((
3
,),
np
.
nan
),
"p_x"
:
[
1
,
2
,
3
],
"p_y"
:
[
3
,
2
,
1
],
}
hessp_x_eval
,
hessp_y_eval
=
hessp_fn
(
**
test
)
np
.
testing
.
assert_allclose
(
hessp_x_eval
,
[
2
,
4
,
6
])
np
.
testing
.
assert_allclose
(
hessp_y_eval
,
[
-
6
,
-
4
,
-
2
])
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论