【发布时间】:2021-05-27 05:08:35
【问题描述】:
我正在将一些 TF1 代码迁移到 TF2。有关完整代码,您可以查看here 行 [155-176]。 TF1 中有一条线在给定损失(浮点值)和 (m, n) 张量的情况下获得梯度
编辑:问题仍然存在
注意: TF2 代码应该兼容并且应该在 tf.function 中工作
g = tf.gradients(-loss, f) # loss being a float and f being a (m, n) tensor
k = -f_pol / (f + eps) # f_pol another (m, n) tensor and eps a float
k_dot_g = tf.reduce_sum(k * g, axis=-1)
adj = tf.maximum(
0.0,
(tf.reduce_sum(k * g, axis=-1) - delta)
/ (tf.reduce_sum(tf.square(k), axis=-1) + eps),
)
g = g - tf.reshape(adj, [nenvs * nsteps, 1]) * k
grads_f = -g / (nenvs * nsteps)
grads_policy = tf.gradients(f, params, grads_f) # params being the model parameters
在我正在尝试的 TF2 代码中:
with tf.GradientTape() as tape:
f = calculate_f()
f_pol = calculate_f_pol()
others = do_further_calculations()
loss = calculate_loss()
g = tape.gradient(-loss, f)
但是,无论我使用 tape.watch(f) 还是创建具有 f 值的 tf.Variable 或什至在 tf.gradients() 中使用 tf.gradients(),我都会不断收到 g = [None],否则它会抱怨。
【问题讨论】:
-
-loss是对张量的操作,它应该在磁带上下文中,以便跟踪反向传播。试试loss = -calculate_loss()然后g = tape.gradient(loss, f),或者如果你更喜欢loss = calculate_loss(); nloss = -loss然后g = tape.gradient(nloss, f)。 -
你能用一些随机数据添加一个最小的例子吗? :)
-
@Roelant 我已经修复了错误,我怀疑损失计算的某些方面发生在触发错误的
tf.GradientTape上下文之外。
标签: python tensorflow migration tensorflow2.x tensorflow1.15