Keras& TensorFlow:获得f(x)wrt x的二阶导数,其中dim(x)=(1,n)

时间:2017-07-18 13:12:33

标签: python tensorflow keras derivative hessian-matrix

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

为此我需要找到关于特定层输出的损失(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,我不认为我可以摆脱它。

我无法重塑s因为它打破了渐变的流动,例如:

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

给出:

[None]

关于在这种情况下我该怎么做的任何想法?

1 个答案:

答案 0 :(得分:0)

目前不支持此功能。请参阅this report