IndexError: Target 4 is out of bounds
Hi, I am getting error while training model for token classificationfrom torch.utils.data import DataLoaderfrom transformers import DistilBertForTokenClassification, AdamWdevice = torch.device(‘cuda’) if torch.cuda.is_available() else torch.device(‘cpu’)model = DistilBertForTokenClassification.from_pretrained(‘distilbert-base-uncased’)model.to(device)model.train()train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)optim = AdamW(model.parameters(), lr=5e-5)for epoch in range(3):for batch in train_loader:optim.zero_grad()input_ids = batch[‘input_ids’].to(device)attention_mask = batch[‘attention_mask’].to(device)labels = batch[‘labels’].to(device)outputs = model(input_ids, attention_mask=attention_mask, labels=labels)loss = outputs[0]loss.backward()optim.step()model.eval()Error:IndexError                                Traceback (most recent call last)in18         attention_mask = batch[‘attention_mask’].to(device)19         labels = batch[‘labels’].to(device)—> 20         outputs = model(input_ids, attention_mask=attention_mask, labels=labels)21         loss = outputs[0]22         loss.backward()~\Anaconda3\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_hooks1050                 or _global_forward_hooks or _global_forward_pre_hooks):→ 1051             return forward_call(*input, **kwargs)1052         # Do not call functions when jit is used1053         full_backward_hooks, non_full_backward_hooks = [], []~\Anaconda3\lib\site-packages\transformers\models\distilbert\modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict)838                     active_loss, labels.view(-1), torch.tensor(loss_fct.ignore_index).type_as(labels)839                 )→ 840                 loss = loss_fct(active_logits, active_labels)841             else:842                 loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))~\Anaconda3\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_hooks1050                 or _global_forward_hooks or _global_forward_pre_hooks):→ 1051             return forward_call(*input, **kwargs)1052         # Do not call functions when jit is used1053         full_backward_hooks, non_full_backward_hooks = [], []~\Anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)11181119     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\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)28252826IndexError: Target 4 is out of bounds.
2023-05-02T11:11:37Z
Have you found a solution to that?@sunnykumar