【问题标题】:expected scalar type Long but found Float in PyTorch, using nn.CrossEntropyLoss()预期的标量类型 Long 但在 PyTorch 中使用 nn.CrossEntropyLoss() 找到 Float
【发布时间】:2021-08-24 02:41:07
【问题描述】:

我想制作一个标签火炬张量。我选择了两种不同的方法,第一种方法在使用nn.CrossEntropyLoss() 计算损失的部分出错。我想知道为什么会这样,虽然张量结果是一样的。

第一种方法:

labels = torch.hstack((torch.zeros(100),torch.ones(100),1+torch.ones(100)))

第二种方法:

labels_np = np.vstack((np.zeros((100,1)),np.ones((100,1)),1+np.ones((100,1))))
labels = torch.squeeze(torch.tensor(labels_np).long())

错误

expected scalar type Long but found Float in Pytoch

【问题讨论】:

    标签: python deep-learning pytorch


    【解决方案1】:

    要修复您的第一种方法,您应该通过调用labels = labels.long() 来转换数据类型来更改类型:

    labels = torch.hstack((torch.zeros(100),torch.ones(100),1+torch.ones(100)))
    labels = labels.long()
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2021-02-14
      • 2020-06-11
      • 2019-09-04
      • 2020-05-31
      • 2019-10-16
      • 2021-09-16
      • 2020-12-02
      • 1970-01-01
      相关资源
      最近更新 更多