This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-label text classification (#401)
* Add toxic comments example * Updates * Clean * Add docs * Add docs * Update CHANGELOG.md * Fix test Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bec7142
commit d23319d
Showing
13 changed files
with
232 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../reference/multi_label_classification.rst → ...ence/image_classification_multi_label.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
.. _text_classification_multi_label: | ||
|
||
############################### | ||
Multi-label Text Classification | ||
############################### | ||
|
||
******** | ||
The task | ||
******** | ||
|
||
Multi-label classification is the task of assigning a number of labels from a fixed set to each data point, which can be in any modality. | ||
In this example, we will look at the task of classifying comment toxicity. | ||
|
||
----- | ||
|
||
******** | ||
The data | ||
******** | ||
The data we will use in this example is from the kaggle toxic comment classification challenge by jigsaw: `www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge <https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge>`_. | ||
|
||
------ | ||
|
||
********* | ||
Inference | ||
********* | ||
|
||
We can load a pretrained :class:`~flash.text.classification.model.TextClassifier` and perform inference on any string sequence using :func:`~flash.text.classification.model.TextClassifier.predict`: | ||
|
||
.. literalinclude:: ../../../flash_examples/predict/text_classification_multi_label.py | ||
:language: python | ||
:lines: 14- | ||
|
||
For more advanced inference options, see :ref:`predictions`. | ||
|
||
----- | ||
|
||
********** | ||
Finetuning | ||
********** | ||
|
||
Now let's look at how we can finetune a model on the toxic comments data. | ||
Once we download the data using :func:`~flash.core.data.download_data`, we can create our :meth:`~flash.text.classification.data.TextClassificationData` using :meth:`~flash.core.data.data_module.DataModule.from_csv`. | ||
The backbone can be any BERT classification model from Huggingface. | ||
We use ``"unitary/toxic-bert"`` as the backbone since it's already trained on the toxic comments data. | ||
Now all we need to do is fine-tune our model! | ||
|
||
.. literalinclude:: ../../../flash_examples/finetuning/text_classification_multi_label.py | ||
:language: python | ||
:lines: 14- | ||
|
||
---- | ||
|
||
To run the example: | ||
|
||
.. code-block:: bash | ||
python flash_examples/finetuning/text_classification_multi_label.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
flash_examples/finetuning/text_classification_multi_label.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from torchmetrics import F1 | ||
|
||
import flash | ||
from flash.core.data.utils import download_data | ||
from flash.text import TextClassificationData, TextClassifier | ||
|
||
# 1. Download the data from the Kaggle Toxic Comment Classification Challenge: | ||
# https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge | ||
download_data("https://pl-flash-data.s3.amazonaws.com/jigsaw_toxic_comments.zip", "data/") | ||
|
||
# 2. Load the data | ||
datamodule = TextClassificationData.from_csv( | ||
"comment_text", | ||
["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"], | ||
train_file="data/jigsaw_toxic_comments/train.csv", | ||
test_file="data/jigsaw_toxic_comments/test.csv", | ||
predict_file="data/jigsaw_toxic_comments/predict.csv", | ||
batch_size=16, | ||
val_split=0.1, | ||
backbone="unitary/toxic-bert", | ||
) | ||
|
||
# 3. Build the model | ||
model = TextClassifier( | ||
num_classes=datamodule.num_classes, | ||
multi_label=True, | ||
metrics=F1(num_classes=datamodule.num_classes), | ||
backbone="unitary/toxic-bert", | ||
) | ||
|
||
# 4. Create the trainer | ||
trainer = flash.Trainer(fast_dev_run=True) | ||
|
||
# 5. Fine-tune the model | ||
trainer.finetune(model, datamodule=datamodule, strategy="freeze") | ||
|
||
# 6. Generate predictions for a few comments! | ||
predictions = model.predict([ | ||
"No, he is an arrogant, self serving, immature idiot. Get it right.", | ||
"U SUCK HANNAH MONTANA", | ||
"Would you care to vote? Thx.", | ||
]) | ||
print(predictions) | ||
|
||
# 7. Save it! | ||
trainer.save_checkpoint("text_classification_multi_label_model.pt") |
Oops, something went wrong.