6

我在 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 * a分别b = torch.pow(a, 2)会因上面提到的错误而失败。

有谁知道解决这个问题?

非常感谢!

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

4

2 回答 2

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]), number 2is out of index 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
于 2019-01-21T14:18:35.230 回答
0

你在使用 GPU 吗?

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

于 2018-06-27T15:42:13.570 回答