Tensorflow 2: Error computing gradients from a list of tf.Variables

Viewed 18

What's wrong with the code below ?

vars = [tf.Variable(0.0), tf.Variable(1.0)]

with tf.GradientTape() as g:
  y = tf.reduce_mean(tf.concat(vars,axis=0))

grads = g.gradient(y, vars)

The above gives error : "ZeroDivisionError: integer division or modulo by zero"

The official Tensorflow page lists that "concat" has gradients defined

https://www.tensorflow.org/api_docs/python/tf/raw_ops

The following works as expected:

vars = [tf.Variable(0.0), tf.Variable(1.0)]

with tf.GradientTape() as g:
  y = 0.5*(vars[0] + vars[1])

grads = g.gradient(y, vars)
0 Answers
Related