tensorflow: Error in gradient of reduce_prod

vars = tf.Variable([1., 2.])
tf.initialize_all_variables().run()
tf.gradients(tf.reduce_prod(vars), vars)[0].eval()

yields [ 2., 1.] which is correct. But

vars = tf.Variable([0., 2.])
tf.initialize_all_variables().run()
tf.gradients(tf.reduce_prod(vars), vars)[0].eval()

yields [ nan, 0.] which is incorrect. The correct gradient is [ 2., 0.]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (12 by maintainers)

Most upvoted comments

@ibab It does seem like it has to be transpose + reshape + stuff + reshape + transpose. I don’t think a custom tf.reshape_selected makes sense: separate transpose and reshape is cleaner especially since you have to invert it. The tf.cond is indeed necessary in general. This is getting ugly, but I don’t know a cleaner way.