# String indices must be integers in BertPreTrainedModel

**URL:** <https://discuss.huggingface.co/t/string-indices-must-be-integers-in-bertpretrainedmodel/77053>\
**Category:** Models\
**Created:** [March 12, 2024, 9:22am UTC](https://discuss.huggingface.co/t/string-indices-must-be-integers-in-bertpretrainedmodel/77053 "2024-03-12T09:22:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahnaf702](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ahnaf702/32/24766_2.png) [@ahnaf702](https://discuss.huggingface.co/u/ahnaf702)\
**Post date:** [March 12, 2024, 9:22am UTC](https://discuss.huggingface.co/t/string-indices-must-be-integers-in-bertpretrainedmodel/77053/1 "2024-03-12T09:22:34Z")

</div>

While training the model in text classification task, it kept saying this error, but since I ran this code few days ago , i am not understanding why it’s not working anymore

```auto
Epoch: 0%| | 0/10 [00:02<?, ?it/s]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-cdf49a404347> in <cell line: 1>()
----> 1 train(model=model, 
      2 criterion=criterion,
      3 optimizer=optimizer,
      4 train_loader=train_loader,
      5 val_loader=valid_loader,

3 frames
<ipython-input-30-e26e65f9e3c5> in train(model, criterion, optimizer, train_loader, val_loader, epochs, device)
      9 input_ids, attention_mask, labels = input_ids.to(device), attention_mask.to(device), labels.to(device)
     10 
---> 11 logits = model(input_ids=input_ids, attention_mask=attention_mask)
     12 
     13 loss = criterion(logits, labels)

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
   1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
   1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
   1519 
   1520 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
   1525 or _global_backward_pre_hooks or _global_backward_hooks
   1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
   1528 
   1529 try:

<ipython-input-26-ca436c0e67a1> in forward(self, input_ids, attention_mask)
     10 reps, _ = self.bert(input_ids=input_ids, attention_mask=attention_mask)
     11 #Obtain the representations of [CLS] heads
---> 12 cls_reps = reps[:, 0]
     13 logits = self.cls_layer(cls_reps)
     14 return logits

TypeError: string indices must be integers

```

thanks in advance
