我在tensorflow中实现DeepMind的DQN算法,并在我调用optimizer.minimize(self.loss)的行中遇到此错误:

ValueError: No gradients provided for any variable...

通过阅读有关此错误的其他文章,我了解到,这意味着损失函数不依赖于用于建立模型的任何张量,但是在我的代码中,我看不到这是怎么回事。 qloss()函数显然取决于对predict()函数的调用,该调用取决于所有层张量进行计算。

The model setup code can be viewed here

最佳答案

我发现问题是,在我的qloss()函数中,我正在从张量中拉出值,对它们进行运算并返回值。尽管值确实取决于张量,但它们本身并未封装在张量中,因此TensorFlow不能说出它们取决于图中的张量。

我通过更改qloss()来解决此问题,以便它直接在张量上执行操作并返回张量。这是新功能:

def qloss(actions, rewards, target_Qs, pred_Qs):
    """
    Q-function loss with target freezing - the difference between the observed
    Q value, taking into account the recently received r (while holding future
    Qs at target) and the predicted Q value the agent had for (s, a) at the time
    of the update.

    Params:
    actions   - The action for each experience in the minibatch
    rewards   - The reward for each experience in the minibatch
    target_Qs - The target Q value from s' for each experience in the minibatch
    pred_Qs   - The Q values predicted by the model network

    Returns:
    A list with the Q-function loss for each experience clipped from [-1, 1]
    and squared.
    """
    ys = rewards + DISCOUNT * target_Qs

    #For each list of pred_Qs in the batch, we want the pred Q for the action
    #at that experience. So we create 2D list of indeces [experience#, action#]
    #to filter the pred_Qs tensor.
    gather_is = tf.squeeze(np.dstack([tf.range(BATCH_SIZE), actions]))
    action_Qs = tf.gather_nd(pred_Qs, gather_is)

    losses = ys - action_Qs
    clipped_squared_losses = tf.square(tf.minimum(tf.abs(losses), 1))

    return clipped_squared_losses

10-07 15:07