簡體   English   中英

從 tf.gradients() 到 tf.GradientTape() 的轉換返回 None

[英]Conversion from tf.gradients() to tf.GradientTape() returns None

我正在將一些 TF1 代碼遷移到 TF2。 有關完整代碼,您可以在此處查看第 [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)還是創建一個值為ftf.Variable ,甚至在tf.function中使用tf.gradients() ,我都會不斷得到g = [None] ,否則它會抱怨。

很可能是以下情況之一

  1. 在由@tf.funtion tf.Variable的 function 中區分 tf.Variable 嗎?
  2. 一些變量是 numpy.array 而不是 tf.Tensor
  3. 您在裝飾的 function 中更改了一些外部變量(即全局變量)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM