gpt4 book ai didi

python - 如何在 TensorFlow GradientTape 中使用多个渐变?

转载 作者:行者123 更新时间:2023-12-05 02:43:29 27 4
gpt4 key购买 nike

按照以下代码连接了 3 个神经网络,我们如何从初始网络中获取两个梯度?第一个梯度有效,但第二个梯度返回 None 张量。似乎它们彼此无关以获得梯度。我该如何解决这个问题?

with tf.GradientTape() as tape1:
with tf.GradientTape() as tape2:
output1 = NN_model1(input1, training=True)
output2 = NN_model2(output1, training=True)
output3 = NN_model3([input1, output1, output2], training=True)
loss1 = -tf.math.reduce_mean(output3)
loss2 = -tf.math.reduce_mean(output2)
grad1 = tape2.gradient(loss1, NN_model1.trainable_variables)
grad2 = tape1.gradient(loss2, grad1)
optimizer.apply_gradients(zip(grad2, NN_model1.trainable_variables))

最佳答案

我认为正确的做法应该是这样的:

with tf.GradientTape() as tape:
output1 = NN_model1(input1, training=True)
output2 = NN_model2(output1, training=True)
output3 = NN_model3([input1, output1, output2], training=True)
loss1 = -tf.math.reduce_mean(output3)
loss2 = -tf.math.reduce_mean(output2)
grad = tape.gradient([loss1, loss2], NN_model1.trainable_variables)
optimizer.apply_gradients(zip(grad, NN_model1.trainable_variables))

关于python - 如何在 TensorFlow GradientTape 中使用多个渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66901737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com