我遇到了一个错误。
Expected object of scalar type Long but got scalar type Int for argument #3 'index'
这是来自这一行。
targets = torch.zeros(log_probs.size()).scatter_(1, targets.unsqueeze(1).data.cpu(), 1)
我不确定该怎么做,因为我尝试在几个地方将其转换为长整型。我试图放置一个
.long
在最后设置dtype为torch.long,但仍未生效。
与这个问题非常相似,但他没有做任何事情来得到答案: "Expected Long but got Int" while running PyTorch script
我已经改了很多代码,这是我的最后一次尝试,但现在出现了同样的问题。
def forward(self, inputs, targets):
"""
Args:
inputs: prediction matrix (before softmax) with shape (batch_size, num_classes)
targets: ground truth labels with shape (num_classes)
"""
log_probs = self.logsoftmax(inputs)
targets = torch.zeros(log_probs.size()).scatter_(1, targets.unsqueeze(1).data.cpu(), 1)
if self.use_gpu: targets = targets.to(torch.device('cuda'))
targets = (1 - self.epsilon) * targets + self.epsilon / self.num_classes
loss = (- targets * log_probs).mean(0).sum()
return loss