You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add multi-label text classification support to pytorch example (#24770)
* Add text classification example
* set the problem type and finetuning task
* ruff reformated
* fix bug for unseting label_to_id for regression
* update README.md
* fixed finetuning task
* update comment
* check if label exists in feature before removing
* add useful logging
Copy file name to clipboardExpand all lines: examples/pytorch/text-classification/README.md
+49Lines changed: 49 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,6 +81,55 @@ python run_glue.py \
81
81
82
82
> If your model classification head dimensions do not fit the number of labels in the dataset, you can specify `--ignore_mismatched_sizes` to adapt it.
83
83
84
+
## Text classification
85
+
As an alternative, we can use the script [`run_classification.py`](./run_classification.py) to fine-tune models on a single/multi-label classification task.
86
+
87
+
The following example fine-tunes BERT on the `en` subset of [`amazon_reviews_multi`](https://huggingface.co/datasets/amazon_reviews_multi) dataset.
88
+
We can specify the metric, the label column and aso choose which text columns to use jointly for classification.
Training for 1 epoch results in acc of around 0.5958 for review_body only and 0.659 for title+body+category.
110
+
111
+
The following is a multi-label classification example. It fine-tunes BERT on the `reuters21578` dataset hosted on our [hub](https://huggingface.co/datasets/reuters21578):
112
+
```bash
113
+
dataset="reuters21578"
114
+
subset="ModApte"
115
+
python run_classification.py \
116
+
--model_name_or_path bert-base-uncased \
117
+
--dataset_name ${dataset} \
118
+
--dataset_config_name ${subset} \
119
+
--shuffle_train_dataset \
120
+
--remove_splits "unused" \
121
+
--metric_name f1 \
122
+
--text_column_name text \
123
+
--label_column_name topics \
124
+
--do_train \
125
+
--do_eval \
126
+
--max_seq_length 512 \
127
+
--per_device_train_batch_size 32 \
128
+
--learning_rate 2e-5 \
129
+
--num_train_epochs 15 \
130
+
--output_dir /tmp/${dataset}_${subset}/
131
+
```
132
+
It results in a Micro F1 score of around 0.82 without any text and label filtering. Note that you have to explictly remove the "unused" split from the dataset, since it is not used for classification.
0 commit comments