[example] run_ner.py raised error: IndexError: Target 3 is out of bounds.
Author: gongel
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: 4.3.0.dev0 or 4.2.2
- Platform: MacOS
- Python version: 3.6
- PyTorch version (GPU?):   1.7.1
- Tensorflow version (GPU?): 2.4.1
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No

### Who can help

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj


## To reproduce

Steps to reproduce the behavior:

1. bash [run.sh](https://github.com/huggingface/transformers/blob/master/examples/token-classification/run.sh) to [run_ner.py](https://github.com/huggingface/transformers/blob/master/examples/token-classification/run_ner.py)

## Error

```
[INFO|trainer.py:837] 2021-02-07 22:22:31,755 >> ***** Running training *****
[INFO|trainer.py:838] 2021-02-07 22:22:31,755 >>   Num examples = 14041
[INFO|trainer.py:839] 2021-02-07 22:22:31,755 >>   Num Epochs = 3
[INFO|trainer.py:840] 2021-02-07 22:22:31,755 >>   Instantaneous batch size per device = 8
[INFO|trainer.py:841] 2021-02-07 22:22:31,755 >>   Total train batch size (w. parallel, distributed & accumulation) = 8
[INFO|trainer.py:842] 2021-02-07 22:22:31,755 >>   Gradient Accumulation steps = 1
[INFO|trainer.py:843] 2021-02-07 22:22:31,755 >>   Total optimization steps = 5268
  0%|                                                                                                                                                                                          | 0/5268 [00:00<?, ?it/s]Traceback (most recent call last):
  File "run_ner.py", line 443, in <module>
    main()
  File "run_ner.py", line 377, in main
    train_result = trainer.train(resume_from_checkpoint=checkpoint)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 940, in train
    tr_loss += self.training_step(model, inputs)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 1304, in training_step
    loss = self.compute_loss(model, inputs)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 1334, in compute_loss
    outputs = model(**inputs)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/bytedance/transformers/src/transformers/models/bert/modeling_bert.py", line 1701, in forward
    loss = loss_fct(active_logits, active_labels)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 962, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 2468, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 2264, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target 3 is out of bounds.
Exception ignored in: <bound method tqdm.__del__ of   0%|                                                                                                                                                                                          | 0/5268 [00:01<?, ?it/s]>
Traceback (most recent call last):
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/tqdm/std.py", line 1086, in __del__
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/tqdm/std.py", line 1270, in close
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/tqdm/std.py", line 572, in _decr_instances
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/tqdm/_monitor.py", line 51, in exit
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/threading.py", line 521, in set
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/threading.py", line 364, in notify_all
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/threading.py", line 347, in notify
TypeError: 'NoneType' object is not callable

```

Comments:
Which version of the datasets library are you using? The script runs fine on my side.

Hi @sgugger,
I use Version 1.2.1 of the datasets library.

I tried the different version of the datasets library. It seems that it is not due to the version of the datasets library.
- Version 1.2.1, 1.2.0, 1.1.3
```
Traceback (most recent call last):
  File "run_ner.py", line 443, in <module>
    main()
  File "run_ner.py", line 377, in main
    train_result = trainer.train(resume_from_checkpoint=checkpoint)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 940, in train
    tr_loss += self.training_step(model, inputs)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 1304, in training_step
    loss = self.compute_loss(model, inputs)
  File "/Users/bytedance/transformers/src/transformers/trainer.py", line 1334, in compute_loss
    outputs = model(**inputs)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/bytedance/transformers/src/transformers/models/bert/modeling_bert.py", line 1701, in forward
    loss = loss_fct(active_logits, active_labels)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 962, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 2468, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/Users/bytedance/opt/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 2264, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target 3 is out of bounds.
```
- Version 1.1.2, 1.1.1, 1.1.0
```
Traceback (most recent call last):
  File "run_ner.py", line 443, in <module>
    main()
  File "run_ner.py", line 230, in main
    if isinstance(features[label_column_name].feature, ClassLabel):
AttributeError: 'Value' object has no attribute 'feature'
```


And to be clear you are just running the non-modified version of `token-classification/run.sh`?

> And to be clear you are just running the non-modified version of `token-classification/run.sh`?

Yes, I am just running the non-modified version of token-classification/run.sh.

Hi! The error you have for versions 1.2.1, 1.2.0 and 1.1.3 is probably the same as #10050. 
See https://github.com/huggingface/transformers/issues/10050#issuecomment-775034308 for how to resolve it.

> Hi! The error you have for versions 1.2.1, 1.2.0 and 1.1.3 is probably the same as #10050.
> See [#10050 (comment)](https://github.com/huggingface/transformers/issues/10050#issuecomment-775034308) for how to resolve it.

I tried. It doesn't work.😭

I am trying to perform token classification task through fine tuning pretrained model.  My input data is in conll format in which first column consist token and second column indicate it's grammatical category (value is separated by tab). I am passing these info

from transformers import DistilBertForTokenClassification, Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir='./results',          # output directory
    num_train_epochs=3,              # total number of training epochs
    per_device_train_batch_size=16,  # batch size per device during training
    per_device_eval_batch_size=16,   # batch size for evaluation
    warmup_steps=500,                # number of warmup steps for learning rate scheduler
    weight_decay=0.01,               # strength of weight decay
    logging_dir='./logs',            # directory for storing logs
    logging_steps=10,
)

model = DistilBertForTokenClassification.from_pretrained("distilbert-base-uncased")

trainer = Trainer(
    model=model,                         # the instantiated 🤗 Transformers model to be trained
    args=training_args,                  # training arguments, defined above
    train_dataset=train_dataset,         # training dataset
    eval_dataset=val_dataset             # evaluation dataset
)

trainer.train()


after running the trainer.train() in google colab with GPU runtime I am getting index error 
"IndexError: Target 6 is out of bounds."

How to get rid of this problem, Can anyone help me to get rid from this issue.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.

> I am trying to perform token classification task through fine tuning pretrained model. My input data is in conll format in which first column consist token and second column indicate it's grammatical category (value is separated by tab). I am passing these info
> 
> from transformers import DistilBertForTokenClassification, Trainer, TrainingArguments
> 
> training_args = TrainingArguments(
> output_dir='./results', # output directory
> num_train_epochs=3, # total number of training epochs
> per_device_train_batch_size=16, # batch size per device during training
> per_device_eval_batch_size=16, # batch size for evaluation
> warmup_steps=500, # number of warmup steps for learning rate scheduler
> weight_decay=0.01, # strength of weight decay
> logging_dir='./logs', # directory for storing logs
> logging_steps=10,
> )
> 
> model = DistilBertForTokenClassification.from_pretrained("distilbert-base-uncased")
> 
> trainer = Trainer(
> model=model, # the instantiated 🤗 Transformers model to be trained
> args=training_args, # training arguments, defined above
> train_dataset=train_dataset, # training dataset
> eval_dataset=val_dataset # evaluation dataset
> )
> 
> trainer.train()
> 
> after running the trainer.train() in google colab with GPU runtime I am getting index error
> "IndexError: Target 6 is out of bounds."
> 
> How to get rid of this problem, Can anyone help me to get rid from this issue.

Hi HuggingFace,
I am having exact same issue. While `running trainer.train()` in Google Colab using GPU I get this error. I tried with TPU as well, get the same issue.

Running on my own data with 10 classes. My classes are labelled from 0 to 9. There are 297 training items, 97 testing and 75 Validation items. Please help us me to fix this error. 

By the way, I solved it.. 

Solution:
While creating the model, pass `num_labels` arugment

Something like this 
```
model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=n_labels)
```