首页 > 解决方案 > Tensorflow:ValueError:没有为任何变量提供梯度

问题描述

我很困惑为什么以下函数会引发错误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 函数运行它,因为我的训练实际上更复杂,但对于这个例子,我尽可能地精简它。

更多信息:

states 是 float32,actions 是采取的行动的整数列表,reward 是收到的奖励的整数列表。

网络是一个简单的 DNN,优化器是 keras。

标签: pythontensorflow

解决方案


推荐阅读