How to compute all second derivatives (only the diagonal of the Hessian matrix) in Tensorflow? : <p>I have a <strong>loss</strong> value/function and I would like to compute all the second derivatives with respect to a tensor <strong>f</strong> (of size n). I managed to use tf.gradients twice, but when applying it for the second time, it sums the derivatives across the first input (see <em>second_derivatives</em> in my code). </p>

<p>Also I managed to retrieve the Hessian matrix, but I would like to only compute its diagonal to avoid extra-computation.</p>

<pre><code>import tensorflow as tf
import numpy as np

f = tf.Variable(np.array([[1., 2., 0]]).T)
loss = tf.reduce_prod(f ** 2 - 3 * f + 1)

first_derivatives = tf.gradients(loss, f)[0]

second_derivatives = tf.gradients(first_derivatives, f)[0]

hessian = [tf.gradients(first_derivatives[i,0], f)[0][:,0] for i in range(3)]

model = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(model)
    print "\nloss\n", sess.run(loss)
    print "\nloss'\n", sess.run(first_derivatives)
    print "\nloss''\n", sess.run(second_derivatives)
    hessian_value = np.array(map(list, sess.run(hessian)))
    print "\nHessian\n", hessian_value
</code></pre>

<p>My thinking was that <strong>tf.gradients(first_derivatives, f[0, 0])[0]</strong> would work to retrieve for instance the second derivative with respect to f_0 but it seems that tensorflow doesn't allow to derive from a slice of a tensor.</p>
