我很困惑为什么以下函数会抛出错误 ValueError: No gradients provided for any variable
。
@tf.function
def train_network(model, optimizer, states, actions, rewards):
with tf.GradientTape() as tape:
predictions = model(states)
indices = tf.stack([tf.range(predictions.shape[0], dtype=tf.int64), actions], axis=1)
action_rewards = tf.scatter_nd(indices, rewards, predictions.shape)
loss = tf.keras.losses.MSE(predictions, action_rewards)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
return loss
我传递了模型和优化器,因为它们是类对象的一部分。但正如我发现的那样,您不能将 tf.function 作为类函数。
如果我用 keras fit 函数尝试同样的事情,它就可以正常工作,例如。 self.model.fit(np.array(states_epoch), np.array(rewards_epoch), batch_size=50)
。
我想用 tf 函数运行它,因为我的训练实际上更复杂,但对于这个例子,我尽可能地精简了它。
更多信息:
状态是 float32,动作是采取的行动的整数列表,奖励是收到的奖励的整数列表。
网络是一个简单的 DNN,优化器是 keras。