Repository: huggingface/transformers

Issue:
`run_glue.py` fails when using my own dataset of regression task
## Environment info
     
- `transformers` version: 4.1.1
- Platform: Linux-4.15.0-123-generic-x86_64-with-glibc2.10
- Python version: 3.8.3
- PyTorch version (GPU?): 1.7.0 (True)
- Tensorflow version (GPU?): 2.3.1 (True)
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

### Who can help
 albert, bert, GPT2, XLM: @LysandreJik 
 examples/token-classification: @stefan-it

(Excuse me if I'm asking someone who is not in charge. I couldn't find `examples/text-classification` in the list.)

## Information

Model I am using (Bert, XLNet ...): Bert

The problem arises when using:
* [x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

It seems that an error occurs when I use `run_glue.py` with my own dataset of regression task.

``` sh
CUDA_VISIBLE_DEVICES=0 python run_glue.py  \
  --model_name_or_path bert-base-cased \
  --train_file ****.csv \
  --validation_file ****.csv \
  --do_train \
  --do_eval \
  --max_seq_length 64 \
  --per_device_train_batch_size 32 \
  --learning_rate 2e-5 \
  --num_train_epochs **** \
  --logging_steps **** \
  --save_steps **** \
  --save_total_limit **** \
  --output_dir ****/v4.1.1/****
```

An example of the train/valid CSV file is as below:

``` csv
id,label,sentence1
__id_as_string__,3.0,__string__
```

Sorry for the lack of details. I use this heavily masked notation to take into account the licensing of the dataset.
You can see that the columns contain `label` and `sentence1`, and the value of `label` is `float`.
I confirmed that `is_regression` is `True` in this case.

The error message says:

``` sh
Traceback (most recent call last):
  File "run_glue.py", line 419, in <module>
    main()
  File "run_glue.py", line 293, in main
    label_to_id = {v: i for i, v in enumerate(label_list)}
UnboundLocalError: local variable 'label_list' referenced before assignment
```

It seems that the case `data_args.task_name is None` and `is_regression is True` has not been considered in the example.
Excuse me if I misunderstand something.

https://github.com/huggingface/transformers/blob/master/examples/text-classification/run_glue.py#L277

```
    if (
        model.config.label2id != PretrainedConfig(num_labels=num_labels).label2id
        and data_args.task_name is not None
        and is_regression
    ):
        # Some have all caps in their config, some don't.
        label_name_to_id = {k.lower(): v for k, v in model.config.label2id.items()}
        if list(sorted(label_name_to_id.keys())) == list(sorted(label_list)):
            label_to_id = {i: label_name_to_id[label_list[i]] for i in range(num_labels)}
        else:
            logger.warn(
                "Your model seems to have been trained with labels, but they don't match the dataset: ",
                f"model labels: {list(sorted(label_name_to_id.keys()))}, dataset labels: {list(sorted(label_list))}."
                "\nIgnoring the model labels as a result.",
            )
    elif data_args.task_name is None:
        label_to_id = {v: i for i, v in enumerate(label_list)}
```

When I modified the last two lines as below, I could go to the next step.
May I ask you that is it the correct way to avoid the error?

```
    elif data_args.task_name is None:
        # No definition for 'data_args.task_name is None' and 'is_regression is True'?
        if not is_regression:
            label_to_id = {v: i for i, v in enumerate(label_list)}
```

## Expected behavior

`run_glue.py` can be used for our own dataset of regression task.


Discussion:
This is the correct fix indeed (though we can group this with the previous test with `elif data_args.task_name is None and not is_regression`)! Thanks for flagging this, do you want to open a PR with the fix you found?
@sgugger 
Thank you for checking this issue and giving the comment.
I'd love to open a PR. 
I'm sorry but could you please wait for a while? I think I can open it by the end of the week.

Resolving patch:
diff --git a/examples/text-classification/run_glue.py b/examples/text-classification/run_glue.py
--- a/examples/text-classification/run_glue.py
+++ b/examples/text-classification/run_glue.py
@@ -289,7 +289,7 @@ def main():
                 f"model labels: {list(sorted(label_name_to_id.keys()))}, dataset labels: {list(sorted(label_list))}."
                 "\nIgnoring the model labels as a result.",
             )
-    elif data_args.task_name is None:
+    elif data_args.task_name is None and not is_regression:
         label_to_id = {v: i for i, v in enumerate(label_list)}
 
     def preprocess_function(examples):


Test patch: