首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow:“ValueError:没有为任何变量提供渐变”

TensorFlow:“ValueError:没有为任何变量提供渐变”
EN

Stack Overflow用户
提问于 2016-06-17 19:33:44
回答 1查看 2.2K关注 0票数 1

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

ValueError: No gradients provided for any variable...

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

模型设置代码可以在这里查看。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-22 16:36:05

我发现问题是,在我的qloss()函数中,我从张量中提取值,对它们进行操作并返回值。虽然值确实依赖于张量,但它们并不是封装在张量中的,因此TensorFlow无法判断它们依赖于图中的张量。

我通过更改qloss()来修正这个问题,这样它就可以直接对张量进行操作,并返回一个张量。以下是新功能:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37889125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档