【问题标题】:How to use PyTorch Tensor.index_select()?如何使用 PyTorch Tensor.index_select()?
【发布时间】:2018-05-10 18:39:16
【问题描述】:

目前正在尝试使用 PyTorch 实现 REINFORCE 算法。我希望能够在打折奖励后收集负责任的输出。因此,考虑到动作记忆,我创建了一个索引张量,并尝试使用 Tensor.index_select,但没有成功。任何人都可以帮忙吗?

    rH = np.array(rH) # discounted reward
    aH = np.array(aH) # action_holder
    sH = np.vstack(np.array(sH)) # states holder

    statesTensor = Variable(torch.from_numpy(sH).type(torch.FloatTensor))
    out = model.forward(statesTensor)

    indexes = GuiltyOnes(out, aH)
    flat = out.view(1,-1)

    respos = torch.index_select(flat, 1, torch.from_numpy(indexes).type(torch.LongTensor))

我收到以下错误:

    return IndexSelect.apply(self, dim, index)
    RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition

【问题讨论】:

    标签: python indexing reinforcement-learning pytorch


    【解决方案1】:

    您的情况可能类似于this one,因此,您应该改用Variable

    i = Variable(torch.from_numpy(indexes).long())
    respos = torch.index_select(flat, 1, i)
    

    请记住,pytorch 错误消息并不总是很准确。在这种情况下,这很容易误导 imo。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2020-05-18
      • 2018-07-27
      • 2019-12-28
      • 2022-01-24
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多