Изменить: проблема сохраняется
Примечание: код 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
Код: Выделить всё
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)
Подробнее здесь: https://stackoverflow.com/questions/663 ... turns-none
Мобильная версия