gpt4 book ai didi

python - Keras & TensorFlow : getting 2nd derivative of f(x) wrt x, 其中 dim(x) = (1, n)

转载 作者:太空狗 更新时间:2023-10-30 01:36:46 25 4
gpt4 key购买 nike

我在 Keras 工作,在后台使用 TensorFlow。我有一个深度神经模型(预测自动编码器)。我正在做一些类似于此的事情:https://arxiv.org/abs/1612.00796 -- 我试图了解给定层中的变量对输出的影响。

为此,我需要找到关于特定层 (s) 输出的损失 (L) 的二阶导数 (Hessian):enter image description here

对角线条目就足够了。 L为标量,s为1乘以n。

我首先尝试的是:

dLds = tf.gradients(L, s)  # works fine to get first order derivatives
d2Lds2 = tf.gradients(dLds, s) # throws an error
TypeError: Second-order gradient for while loops not supported.

我也试过:

d2Lds2 = tf.hessians(L, s)
ValueError: Computing hessians is currently only supported for one-dimensional tensors. Element number 0 of `xs` has 2 dimensions.

我不能改变 s 的形状,因为它是神经网络的一部分(LSTM 的状态)。第一个维度 (batch_size) 已经设置为 1,我认为我无法摆脱它。

我不能 reshape s 因为它破坏了渐变的流动,例如:

tf.gradients(L, tf.reduce_sum(s, axis=0))

给出:

[None]

在这种情况下我能做什么?

最佳答案

目前不支持。参见 this report .

关于python - Keras & TensorFlow : getting 2nd derivative of f(x) wrt x, 其中 dim(x) = (1, n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com