使用tensorflow GradientTape在keras中应用权重后,权重不会更改



以下是代码:

with tf.GradientTape() as tape:
predictions = self.p_model(obs)
indices = tf.random.categorical(predictions, 1, dtype=tf.int32)
indices = tf.squeeze(indices)
#print('indices:', indices)
row_indices = tf.range(tf.shape(indices)[0], dtype=tf.int32)
full_indices = tf.stack([row_indices, indices], axis=1)
act_val = tf.gather_nd(predictions, full_indices)
target_preds1 = self.target_model(obs)
target_preds1 = tf.gather_nd(target_preds1, full_indices)
target_preds2 = self.target_model2(obs)
target_preds2 = tf.gather_nd(target_preds2, full_indices)
target_predict = tf.math.minimum(target_preds1, target_preds2)
loss_value = self.alpha*tf.math.log(act_val) - target_predict
loss_value = tf.reduce_mean(loss_value)
grads = tape.gradient(loss_value, self.p_model.trainable_weights)
self.p_optimizer.apply_gradients(
zip(grads, self.p_model.trainable_weights))

我检查梯度张量。它不是零值。但是,应用渐变后,trainable_weights不会更改。keras的版本是2.3.1,tensorflow的版本是1.15.0。

我试着让人们渴望执行,这是有效的。它是由keras还是tensorflow版本引起的?

我解决了这个问题。self.p_optimizer.apply_fgradients返回update_op。从keras.back.Get_session获取tf会话并运行update_op。

grads = tape.gradient(final_loss, self.p_model.trainable_weights)
update_op = self.optimizer.apply_gradients(
zip(grads, self.p_model.trainable_weights))
keras.backend.get_session().run(update_op)

它是有效的,但没有必要在正常情况下运行update_op。

最新更新