我目前正在用 python 開發一個“四勝”的演員-評論家代理。在嘗試通過先前收集的動作概率分布進行反向傳播時,我遇到了以下錯誤:ZeroDivisionError: integer Division or modulo by zero
我能夠重現錯誤:
import tensorflow as tf
with tf.GradientTape() as tape:
t = tf.Variable([1.])
concat = tf.concat(values=[t[0], t[0]], axis=0)
concat_sum = tf.reduce_sum(concat)
grads = tape.gradient(concat_sum, t)
我知道這個問題在這個代碼示例中可能聽起來微不足道。我仍然無法理解為什么這里有錯誤!如果連接張量的第一個元素并最終添加它們,則不應與:
with tf.GradientTape() as tape:
t = tf.Variable([1.])
result = t t
grads = tape.gradient(result, t)
為什么一個生成有效的梯度而另一個不生成?
我在我的 CPU (Ubuntu 20.04.3 LTS) 上運行Tensorflow 2.7.0 版
uj5u.com熱心網友回復:
當您嘗試連接不受支持的標量時會發生這種情況。Tensorflow 在 Eager 模式下不會引發錯誤,這顯然是一個錯誤。該建議是寧愿使用tf.stack:
import tensorflow as tf
with tf.GradientTape() as tape:
t = tf.Variable([1.])
result = t t
grads = tape.gradient(result, t)
tf.print(grads)
with tf.GradientTape() as tape:
t = tf.Variable([1.])
stack = tf.stack(values=[t[0], t[0]], axis=0)
concat_sum = tf.reduce_sum(stack)
grads = tape.gradient(concat_sum, t)
tf.print(grads)
[2]
[2]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371772.html
上一篇:簡單的神經網路
