在第二次迭代中,张量流概率的急切执行停止记录梯度

2024-09-30 20:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

不知怎的,我的火车圈里没有梯度。下面是一个简单的例子。你知道吗

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))

如果我们用一个“标准”的张量流模型来代替模型m,情况就不是这样了

m = tf.keras.Sequential([tf.keras.layers.Dense(1)])

我使用的是tensorflow=1.13.1和tensorflow概率=0.6.0


Tags: importoutputsizelayerstftensorflowasnn