Tensorflow gradients: without automatic implicit sum
<p>In tensorflow, if one has two tensors <code>x</code> and <code>y</code> and one want to have the gradients of <code>y</code> with respect to <code>x</code> using <code>tf.gradients(y,x)</code>. Then what one actually gets is :</p>

<pre><code>gradient[n,m] = sum_ij d y[i,j]/ d x[n,m]
</code></pre>

<p>There is a sum over the indices of y, is there a way to avoid this implicit sum? To get the whole gradient tensor <code>gradient[i,j,n,m]</code>?</p>

<p>Here is my work around just taking the derivative of each component (as also mentionned by @Yaroslav) and then packing them all together again in the case of rank 2 tensors (Matrices):</p>

<pre><code>import tensorflow as tf

def twodtensor2list(tensor,m,n):
    s = [[tf.slice(tensor,[j,i],[1,1]) for i in range(n)] for j in range(m)]
    fs = []
    for l in s:
        fs.extend(l)
    return fs

def grads_all_comp(y, shapey, x, shapex):
    yl = twodtensor2list(y,shapey[0],shapey[1])
    grads = [tf.gradients(yle,x)[0] for yle in yl]
    gradsp = tf.pack(grads)
    gradst = tf.reshape(gradsp,shape=(shapey[0],shapey[1],shapex[0],shapex[1]))
    return gradst
</code></pre>

<p>Now <code>grads_all_comp(y, shapey, x, shapex)</code> will output the rank 4 tensor in the desired format. It is a very inefficient way because everything needs to be sliced up and repacked together, so if someone finds a better I would be very interested to see it. </p>

python tensorflow gradients