【问题标题】:Pytorch, Unable to get repr for <class 'torch.Tensor'>Pytorch,无法获得 <class 'torch.Tensor'> 的 repr
【发布时间】:2018-12-03 05:10:12
【问题描述】:

我正在 PyTorch 中实现一些 RL,并且必须编写我自己的 mse_loss 函数(我在 Stackoverflow 上找到的;))。 损失函数为:

def mse_loss(input_, target_):    
    return torch.sum(
        (input_ - target_) * (input_ - target_)) / input_.data.nelement()

现在,在我的训练循环中,第一个输入类似于:

tensor([-1.7610e+10]), tensor([-6.5097e+10])

使用这个输入我会得到错误:

Unable to get repr for <class 'torch.Tensor'>

计算 a = (input_ - target_) 工作正常,而 b = a * ab = torch.pow(a, 2) 将因上述错误而失败。

有人知道解决这个问题的方法吗?

非常感谢!

更新: 我刚刚尝试使用torch.nn.functional.mse_loss,这将导致相同的错误..

【问题讨论】:

  • repr 不是内置函数吗? (docs.python.org/3/reference/…)
  • 好像.. 但它为什么会尝试调用这个方法呢?张量是 float32 类型,此错误不仅发生在调试模式下,而且在运行“正常”时也会发生..
  • 这是非常奇怪的 PyTorch 行为。也许更新?

标签: pytorch mse


【解决方案1】:

您使用的是 GPU 吗?

我遇到了类似的问题(但我正在使用收集操作),当我将张量移动到 CPU 时,我会收到一条正确的错误消息。我修复了错误,切换回 GPU 并且没问题。 当它来自 GPU 内部时,也许 pytorch 无法输出正确的错误。

【讨论】:

  • 不,不幸的是我在 cpu 上运行我的代码。此外,目前还没有将其“移动”到 cpu 的代码 - 这可能是个问题吗?
【解决方案2】:

当我使用下面的代码时,我遇到了同样的错误

criterion = torch.nn.CrossEntropyLoss().cuda()
output=output.cuda()
target=target.cuda()
loss=criterion(output, target)

但我终于发现我的错误了:output is like tensor([[0.5746,0.4254]]) and target is like tensor([2]), the number 2 is out of output of output

当我不使用 GPU 时,这个错误信息是:

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at /opt/conda/conda-bld/pytorch-nightly_1547458468907/work/aten/src/THNN/generic/ClassNLLCriterion.c:93

【讨论】:

  • 我在不同的上下文中遇到了同样的错误。问题还在于我试图访问索引大于张量长度的张量。
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2021-01-26
  • 2018-07-07
相关资源
最近更新 更多