S
S
SemenAnnigilator2021-10-16 21:26:11
Python
SemenAnnigilator, 2021-10-16 21:26:11

Pytorch: RuntimeError: 1D target tensor expected, multi-target not supported?

Please tell me how to solve this problem, here is the file with the code and csv files: https://disk.yandex.ru/d/fb48bm2uukHgVg
There is an error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6048/2412480834.py in <module>
      7 )
      8 classifier.preparation(input_ids, y_train, attention_mask)
----> 9 classifier.train()

~\AppData\Local\Temp/ipykernel_6048/3467817459.py in train(self)
     93         for epoch in range(self.epochs):
     94             print(f'Epoch {epoch + 1}/{self.epochs}')
---> 95             train_acc, train_loss = self.fit()
     96             print(f'Train loss {train_loss} accuracy {train_acc}')
     97 

~\AppData\Local\Temp/ipykernel_6048/3467817459.py in fit(self)
     48 
     49             preds = torch.argmax(outputs.logits, dim=1)
---> 50             loss = self.loss_fn(outputs.logits, targets)
     51 
     52             correct_predictions += torch.sum(preds == targets)

~\anaconda3\envs\LikeProject\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\envs\LikeProject\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
   1118 
   1119     def forward(self, input: Tensor, target: Tensor) -> Tensor:
-> 1120         return F.cross_entropy(input, target, weight=self.weight,
   1121                                ignore_index=self.ignore_index, reduction=self.reduction)
   1122 

~\anaconda3\envs\LikeProject\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2822     if size_average is not None or reduce is not None:
   2823         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2824     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2825 
   2826 

RuntimeError: 1D target tensor expected, multi-target not supported

I am attaching the files so that I can study the problem myself (I explain specifically for respected moderators)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xandox, 2021-10-19
@xandox

So it is written - CrossEntropyLoss expects that the target will be LongTensor(B, 1), where B is the size of the batch. It is Long because there should be class labels.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question