【问题标题】:Pytorch - RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target' in call to _thnn_nll_loss_forwardPytorch - RuntimeError: 标量类型 Long 的预期对象,但在调用 _thnn_nll_loss_forward 时为参数 #2 'target' 获得了标量类型 Float
【发布时间】:2021-09-16 05:34:12
【问题描述】:

我正在尝试使用 PyTorch 进行一些实验,并在其中创建了自己的输入和目标。我将这些输入提供给模型(这是一个具有 2 个隐藏层的基本 ANN,这没有错)。但由于某种原因,我无法计算 CrossEntropyLoss()。我无法弄清楚为什么。我知道 StakcOverflow 上的其他一些问题与我的标题相同或有类似的问题。我已经经历了,但对我来说没有任何结果。很多人对数据集有疑问,这似乎不是我的问题。

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

class Net(nn.Module):
    def __init__(self) -> None:
        super(Net, self).__init__()
        self.layer1 = nn.Linear(2, 10)
        self.layer2 = nn.Linear(10, 1)

    def forward(self, x):
        x = F.relu(self.layer1(x))
        x = self.layer2(x)
        return x
    
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device=device)
loss_fn = nn.CrossEntropyLoss()
learning_rate = 1e-3
epochs = 20
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
inputs = torch.Tensor([
    [0,0],
    [0,1],
    [1,0],
    [1,1]
], ).to(device=device)

targets = torch.Tensor([
    0,
    1,
    1,
    0
]).to(device=device)

model.train()
for epoch in range(epochs):

    pred_output = model(inputs)
    print(pred_output.dtype)
    print(targets.dtype)
    loss = loss_fn(pred_output, targets)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    print()
    break

我看到的错误是,

torch.float32
torch.float32
Traceback (most recent call last):
  File ".\main.py", line 57, in <module>
    loss = loss_fn(pred_output, targets)
  File "C:\Users\user\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Users\user\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 1047, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\Users\user\anaconda3\lib\site-packages\torch\nn\functional.py", line 2693, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "C:\Users\user\anaconda3\lib\site-packages\torch\nn\functional.py", line 2388, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target' in call to _thnn_nll_loss_forward

【问题讨论】:

    标签: python pytorch loss cross-entropy


    【解决方案1】:

    我可以使用此代码复制您的错误。

    import torch.nn as nn
    loss = nn.CrossEntropyLoss()
    input = torch.randn(3, 5, requires_grad=True)
    target = torch.tensor([1., 2., 3.])
    loss(input, target)
    

    错误:

    RuntimeError: 预期的标量类型 Long 但发现 Float

    将目标的数据类型更改为target = torch.tensor([1., 2., 3.], dtype=torch.long),一切正常。我相信目标变量确实需要长数据类型,因为将输入更改为浮点数也可以。

    #this will also work
    input = torch.randn(3, 5, requires_grad=True, dtype=torch.float)
    target = torch.tensor([1., 2., 3.], dtype=torch.long)
    loss(input, target)  
    

    注意文档在示例代码中也有这个torch.long dtype。 https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html

    #编辑 1 它不起作用的原因是您在代码中定义输入/目标张量的方式。使用带有小“t”的torch.tensor 而不是torch.Tensor。详细讨论见What is the difference between torch.tensor and torch.Tensor?

    #this will work. Also notice the decimal. otherwise it will be interpreted differently by pytorch
    inputs = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]]).to(device=device)
    targets = torch.tensor([0.,1.,1.,0.], dtype=torch.long).to(device=device)
    

    【讨论】:

    • 我试过了,py targets = torch.Tensor([ 0, 1, 1, 0 ], dtype=torch.long).to(device=device)
    • 但仍然面临另一个问题,cmd Traceback (most recent call last): File ".\main.py", line 44, in &lt;module&gt; targets = torch.Tensor([ TypeError: new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of: * (*, torch.device device) didn't match because some of the keywords were incorrect: dtype * (torch.Storage storage) * (Tensor other) * (tuple of ints size, *, torch.device device) * (object data, *, torch.device device)
    • 添加to(device=device) 没有问题。你在用什么 - 'cpu' 或 'cuda' ?
    • cuda,我目前正在使用 GPU
    • 嗯。我正在使用一个cpu,它工作。如果我没有将目标分配给设备,这也会用 gpu 唤醒。
    猜你喜欢
    • 2021-08-08
    • 2020-12-02
    • 2020-05-31
    • 2020-10-21
    • 2020-06-15
    • 2023-03-21
    • 2019-09-09
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多