【问题标题】:Expected object of scalar type Long but got scalar type Byte for argument #2 'target'标量类型 Long 的预期对象,但参数 #2 'target' 的标量类型 Byte
【发布时间】:2019-09-09 18:26:11
【问题描述】:

我在 colab 上运行 nn,遇到了这个错误,当我在本地系统上运行相同的代码时,该错误不存在。我也尝试过减少批量大小,但错误仍然存​​在。

Loading dataset
Start training
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-37432f9d142a> in <module>()
     70           start_epoch=start_epoch, log=log_interval,
     71           checkpoint_path=os.path.join(dataset_dir, "cnn_block_frame_flow"),
---> 72           validate=True, resume=False, flow=True, use_cuda=cuda)
     73 
     74     #model = models.model()

/content/KTH-Action-Recognition/main/train_helper.py in train(model, num_epochs, train_set, dev_set, lr, batch_size, start_epoch, log, checkpoint_path, validate, resume, flow, use_cuda)
    107             outputs = get_outputs(model, samples["instance"], flow=flow,
    108                                   use_cuda=use_cuda)
--> 109             loss = criterion(outputs, labels)
    110             loss.backward()
    111             optimizer.step()

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
    902     def forward(self, input, target):
    903         return F.cross_entropy(input, target, weight=self.weight,
--> 904                                ignore_index=self.ignore_index, reduction=self.reduction)
    905 
    906 

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   1968     if size_average is not None or reduce is not None:
   1969         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 1970     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   1971 
   1972 

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1788                          .format(input.size(0), target.size(0)))
   1789     if dim == 2:
-> 1790         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   1791     elif dim == 4:
   1792         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

RuntimeError: Expected object of scalar type Long but got scalar type Byte for argument #2 'target'

谁能告诉我是什么导致了这个错误?谢谢你

【问题讨论】:

    标签: python tensorflow deep-learning pytorch google-colaboratory


    【解决方案1】:

    您的问题的标题是说明导致此错误的原因。 target 的类型应该是 torch.LongTensor,但它却是 torch.ByteTensor。在致电nll_loss 之前:

    target = target.type(torch.LongTensor)
    

    【讨论】:

    • targettorch.nn.module 中的一个变量。所以在colab中我应该在哪里编辑它也可以修改labels的类型吗?
    • 第二个参数。你没有显示代码,所以我不知道你怎么称呼它。你用什么损失? nll_losscrossentropyloss?
    • 我刚刚检查了文档,是的,真正的标签。你在做二进制分类吗?
    • crossentropyloss。但为什么 colab 会出现这样的错误。我已经在我的系统中运行了运行良好的代码,但是训练集非常小,这就是我选择 colabs 的原因
    • 只使用loss.item(),而不是loss.item(data[0])。这个没有在你的本地机器上工作,否则你会得到同样的错误
    猜你喜欢
    • 2020-12-02
    • 2021-08-08
    • 2021-09-16
    • 2020-01-29
    • 1970-01-01
    • 2020-06-15
    • 2019-11-06
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多