Repository: huggingface/transformers

Issue:
Examples: label mapping for text classication tasks are not written into configuration
Hi,

I've shortly discussed this with @patrickvonplaten , and we came to the conclusion that the following scenario is a bug:

When using the text classification for a GLUE task, no label mapping will be written into the configuration of the final fine-tuned model.

This leads to an unesthetic "label_0" on the model hub, as it can be seen here:

![Bildschirmfoto_2021-08-27_12-17-09](https://user-images.githubusercontent.com/20651387/131112292-ab2fc8f8-2fad-42cf-b436-0a0a5e0a4475.png)

One has to manually extend the `config.json`:

```json
"id2label": {
    "0": "NEGATIVE",
    "1": "POSITIVE"
  },
  "label2id": {
    "NEGATIVE": 0,
    "POSITIVE": 1
  }
```

to get the following output:

![Bildschirmfoto_2021-08-27_12-17-29](https://user-images.githubusercontent.com/20651387/131112468-bff93d0f-4f3f-428d-ac3b-696ae6e08543.png)

The text classification examples should be extended, so that those label mappings are automatically added to the configuration file.


Discussion:
cc @sgugger @Rocketknight1 

Resolving patch:
diff --git a/examples/pytorch/text-classification/run_glue.py b/examples/pytorch/text-classification/run_glue.py
--- a/examples/pytorch/text-classification/run_glue.py
+++ b/examples/pytorch/text-classification/run_glue.py
@@ -380,6 +380,9 @@ def main():
     if label_to_id is not None:
         model.config.label2id = label_to_id
         model.config.id2label = {id: label for label, id in config.label2id.items()}
+    elif data_args.task_name is not None and not is_regression:
+        model.config.label2id = {l: i for i, l in enumerate(label_list)}
+        model.config.id2label = {id: label for label, id in config.label2id.items()}
 
     if data_args.max_seq_length > tokenizer.model_max_length:
         logger.warning(
diff --git a/examples/pytorch/text-classification/run_glue_no_trainer.py b/examples/pytorch/text-classification/run_glue_no_trainer.py
--- a/examples/pytorch/text-classification/run_glue_no_trainer.py
+++ b/examples/pytorch/text-classification/run_glue_no_trainer.py
@@ -288,6 +288,9 @@ def main():
     if label_to_id is not None:
         model.config.label2id = label_to_id
         model.config.id2label = {id: label for label, id in config.label2id.items()}
+    elif args.task_name is not None and not is_regression:
+        model.config.label2id = {l: i for i, l in enumerate(label_list)}
+        model.config.id2label = {id: label for label, id in config.label2id.items()}
 
     padding = "max_length" if args.pad_to_max_length else False
 
diff --git a/examples/tensorflow/text-classification/run_glue.py b/examples/tensorflow/text-classification/run_glue.py
--- a/examples/tensorflow/text-classification/run_glue.py
+++ b/examples/tensorflow/text-classification/run_glue.py
@@ -355,6 +355,9 @@ def main():
     if label_to_id is not None:
         config.label2id = label_to_id
         config.id2label = {id: label for label, id in config.label2id.items()}
+    elif data_args.task_name is not None and not is_regression:
+        config.label2id = {l: i for i, l in enumerate(label_list)}
+        config.id2label = {id: label for label, id in config.label2id.items()}
 
     if data_args.max_seq_length > tokenizer.model_max_length:
         logger.warning(


Test patch: