TensorFlow 2:来自TF的列表的错误计算梯度。
下面的代码有什么问题?
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)
上面的错误是:“ ZerodivisionError:Integer Division或Modulo”零“
官方Tensorflow页面”列表“ Concat”具有定义的梯度
https://www.tensorflow.org/api_docs/python/tf/raw_ops
以下可行的工作原理:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论