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
Градиенты Tensorflow: без автоматической неявной суммы