为什么下面的函数打印None
?
a = tf.constant(4)
b = tf.constant(2)
gr = tf.gradients(a + b, [a, b])
print(sess.run(gr))
但是当我改变
-a = tf.constant(4)
-b = tf.constant(2)
+a = tf.constant(4.0)
+b = tf.constant(2.0)
它打印渐变。是什么原因呢?
最佳答案
根据https://github.com/tensorflow/tensorflow/issues/20524
出于这个原因,tensorflow团队使tf.gradients
与整数张量不兼容:
实际上,允许整数张量上的梯度导致
tf.while_loop中的错误,并且没有令人满意的方法
无需更改即可解决它们。
关于python - 为什么tf.gradients不适用于整数常量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57665770/