不知何故,我在训练循环中得不到梯度。下面是一个简单的例子。
import tensorflow as tf
import tensorflow_probability as tfp
tf.enable_eager_execution()
hidden_size = 32
output_size = 1
m = tf.keras.Sequential(
[
tfp.layers.DenseLocalReparameterization(hidden_size, tf.nn.leaky_relu),
tfp.layers.DenseLocalReparameterization(hidden_size, tf.nn.leaky_relu),
tfp.layers.DenseLocalReparameterization(output_size)
]
)如果我运行梯度记录步骤两次,第二次没有显示任何梯度。返回一个包含None类型的列表。
for _ in range(2):
with tf.GradientTape() as tape:
loss_value = m(tf.ones((1, 2))) * 2
print(tape.gradient(loss_value, m.trainable_variables))如果我们用一个“标准的”tensorflow模型替换模型m,情况就不是这样了。
m = tf.keras.Sequential([tf.keras.layers.Dense(1)])
我使用的是tensorflow=1.13.1和tensorflow-probability=0.6.0
发布于 2019-06-19 15:51:33
这似乎是一个暂时的bug。我上传了tensorflow和tensorflow probability的夜间版本,问题就解决了。
https://stackoverflow.com/questions/56662065
复制相似问题