【发布时间】:2021-02-03 01:34:53
【问题描述】:
感谢您对此问题的关注。
我要计算tensorflow.keras.Model的hessian矩阵
对于高阶导数,我尝试了嵌套 GradientTape.# 示例图和输入
xs = tf.constant(tf.random.normal([100,24]))
ex_model = Sequential()
ex_model.add(Input(shape=(24)))
ex_model.add(Dense(10))
ex_model.add(Dense(1))
with tf.GradientTape(persistent=True) as tape:
tape.watch(xs)
ys = ex_model(xs)
g = tape.gradient(ys, xs)
h = tape.jacobian(g, xs)
print(g.shape)
print(h.shape)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-dbf443f1ddab> in <module>
5 h = tape.jacobian(g, xs)
6 print(g.shape)
----> 7 print(h.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
还有,另一个试验......
with tf.GradientTape() as tape1:
with tf.GradientTape() as tape2:
tape2.watch(xs)
ys = ex_model(xs)
g = tape2.gradient(ys, xs)
h = tape1.jacobian(g, xs)
print(g.shape)
print(h.shape)
(100, 24)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-c5bbb17404bc> in <module>
7
8 print(g.shape)
----> 9 print(h.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
为什么我不能计算 g wrt x 的梯度?
【问题讨论】:
标签: tensorflow autodiff