gpt4 book ai didi

python - 如何在 TensorFlow 2.0 中组合两个渐变带

转载 作者:行者123 更新时间:2023-12-01 07:15:47 25 4
gpt4 key购买 nike

如何将以下两个渐变胶带合并为一个:

x = tf.Variable(x, dtype=tf.float32)
with tf.GradientTape() as t:
m, v = DGP.predict(x)
dm_dx = t.gradient(m, x)
with tf.GradientTape() as t:
m, v = DGP.predict(x)
dv_dx = t.gradient(v, x)

这是我更喜欢的,但并不像我写的那样工作:

with tf.GradientTape() as t:
m, v = DGP.predict(x)
dm_dx, dv_dx = t.gradient([m,v], x)

最佳答案

你应该能够做到这一点:

x = tf.Variable(x, dtype=tf.float32)
with tf.GradientTape(persistent=True) as t:
m, v = DGP.predict(x)
dm_dx = t.gradient(m, x)
dv_dx = t.gradient(v, x)

关于python - 如何在 TensorFlow 2.0 中组合两个渐变带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974213/

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