1

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)
anksh
  • 35
  • 7
  • This error occurs when you try to concatenate scalars, which does not work. Use `tf.stack` instead. See my answer here: https://stackoverflow.com/questions/70190336/tensorflow-zerodivisionerror-during-concat-of-singel-elements-of-tensor – AloneTogether Jun 29 '22 at 08:26

0 Answers0