“火炬”功能的CPU版本

2024-04-27 03:09:57 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有接收CPU输入的torch.\u C.\u nn.nll\u损耗功能?我没有足够的GPU内存来运行我的函数,所以我尝试在CPU上运行所有的东西。 这是我的具体错误(查看anaconda文件)

Traceback (most recent call last):
  File "plot_parametric_pytorch.py", line 395, in <module>
    val_result = validate(val_loader, model, criterion, 0)
  File "plot_parametric_pytorch.py", line 228, in validate
    training=False, optimizer=None)
  File "plot_parametric_pytorch.py", line 169, in forward
    loss = criterion(output, target_var)
  File "/home/klee/anaconda3/envs/sharpenv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/klee/anaconda3/envs/sharpenv/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 932, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/klee/anaconda3/envs/sharpenv/lib/python3.7/site-packages/torch/nn/functional.py", line 2317, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/klee/anaconda3/envs/sharpenv/lib/python3.7/site-packages/torch/nn/functional.py", line 2115, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _thnn_nll_loss_forward

Tags: inpyhomeliblinenntorchfile
1条回答
网友
1楼 · 发布于 2024-04-27 03:09:57

nll_loss适用于CPU和GPU,但输入和目标需要在同一设备上。您的设备位于不同的设备上,其中第一个(output)位于CPU上,而第二个(target_var)位于GPU上

您需要将target_var放到CPU上

loss = criterion(output, target_var.cpu())

相关问题 更多 >