import os
GPT2 model (Custom Single Head)
This notebook contains some example of how to use the GPT2-based models in this NLP library
In this series, we walk through some of the capability of this library: single-head classification, multi-head classification, multi-label classification, and regression. If you want a more detailed tutorial, check this out
#This will specify a (or a list) of GPUs for training
'CUDA_VISIBLE_DEVICES'] = "0" os.environ[
from that_nlp_library.text_transformation import *
from that_nlp_library.text_augmentation import *
from that_nlp_library.text_main import *
from that_nlp_library.utils import seed_everything
from underthesea import text_normalize
from functools import partial
from pathlib import Path
import pandas as pd
import numpy as np
import nlpaug.augmenter.char as nac
from datasets import load_dataset
import random
from transformers import AutoTokenizer
from datasets import Dataset
Define the custom augmentation function
def nlp_aug_stochastic(x,aug=None,p=0.5):
if not isinstance(x,list):
if random.random()<p: return aug.augment(x)[0]
return x
=[]
news=[]
originalsfor _x in x:
if random.random()<p: news.append(_x)
else: originals.append(_x)
# only perform augmentation when needed
if len(news): news = aug.augment(news)
return news+originals
= nac.KeyboardAug(aug_char_max=3,aug_char_p=0.1,aug_word_p=0.07)
aug = partial(nlp_aug_stochastic,aug=aug,p=0.3) nearby_aug_func
Create a TextDataController object
We will reuse the data and the preprocessings in this tutorial
= load_dataset('sample_data',data_files=['Womens_Clothing_Reviews.csv'],split='train') dset
= TextDataController(dset,
tdc ='Review Text',
main_text='Department Name',
label_names='classification',
sup_types={'Review Text': lambda x: x is not None,
filter_dict'Department Name': lambda x: x is not None,
},=['Title','Division Name'],
metadatas=[text_normalize,str.lower],
content_transformations= [nearby_aug_func,str.lower],
content_augmentations# add "str.lower" here because nearby_aug might return uppercase character
=0.2,
val_ratio=1000,
batch_size=42,
seed=20,
num_proc=False
verbose )
Define our tokenizer for GPT2
= AutoTokenizer.from_pretrained('gpt2') _tokenizer
/home/quan/anaconda3/envs/nlp_dev/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
= _tokenizer.eos_token
_tokenizer.pad_token = 'left' _tokenizer.padding_side
print(_tokenizer)
print(len(_tokenizer))
GPT2TokenizerFast(name_or_path='gpt2', vocab_size=50257, model_max_length=1024, is_fast=True, padding_side='left', truncation_side='right', special_tokens={'bos_token': '<|endoftext|>', 'eos_token': '<|endoftext|>', 'unk_token': '<|endoftext|>', 'pad_token': '<|endoftext|>'}, clean_up_tokenization_spaces=True), added_tokens_decoder={
50256: AddedToken("<|endoftext|>", rstrip=False, lstrip=False, single_word=False, normalized=True, special=True),
}
50257
Process and tokenize our dataset
=100,shuffle_trn=True) tdc.process_and_tokenize(_tokenizer,max_length
tdc.main_ddict
DatasetDict({
train: Dataset({
features: ['Title', 'Review Text', 'Division Name', 'Department Name', 'label', 'input_ids', 'attention_mask'],
num_rows: 18102
})
validation: Dataset({
features: ['Title', 'Review Text', 'Division Name', 'Department Name', 'label', 'input_ids', 'attention_mask'],
num_rows: 4526
})
})
Model Experiment: GPT2 Single-Head Classification
Define and train a vanilla GPT2 model
from transformers.models.gpt2.modeling_gpt2 import GPT2Model
from that_nlp_library.models.roberta.classifiers import ConcatHeadSimple
from that_nlp_library.model_main import *
from that_nlp_library.models.gpt2.classifiers import *
from sklearn.metrics import f1_score, accuracy_score
Using HuggingFace model initialization
from transformers.models.gpt2.modeling_gpt2 import GPT2ForSequenceClassification
= len(tdc.label_lists[0])
num_classes num_classes
6
42)
seed_everything(= GPT2ForSequenceClassification.from_pretrained('gpt2',num_labels=num_classes)
model = model.to('cuda:0') model
/home/quan/anaconda3/envs/nlp_dev/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
Some weights of GPT2ForSequenceClassification were not initialized from the model checkpoint at gpt2 and are newly initialized: ['score.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
= model.config.eos_token_id model.config.pad_token_id
len(_tokenizer)) model.resize_token_embeddings(
Embedding(50257, 768)
= [partial(f1_score,average='macro'),accuracy_score]
metric_funcs = ModelController(model,tdc,seed=42) controller
And we can start training our model
= 8e-5
lr =32
bs=0.01
wd= 3
epochs
controller.fit(epochs,lr,=metric_funcs,
metric_funcs=bs,
batch_size=wd,
weight_decay=False,
save_checkpoint=compute_metrics,
compute_metrics )
[849/849 02:55, Epoch 3/3]
Epoch | Training Loss | Validation Loss | F1 Score Department name | Accuracy Score Department name |
---|---|---|---|---|
1 | No log | 0.283675 | 0.739092 | 0.910075 |
2 | 0.656600 | 0.261791 | 0.749196 | 0.920901 |
3 | 0.656600 | 0.263783 | 0.751478 | 0.922448 |
Using the GPT2Base model (designed for not only single-head but multi-head, multi-label …)
= GPT2Model.from_pretrained('gpt2') gpt2body
/home/quan/anaconda3/envs/nlp_dev/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
# our model is more complex, so it's best to define some of its arguments
={
_model_kwargs# overall model hyperparams
'head_class_sizes':num_classes,
# classfication head hyperparams
'classifier_dropout':0.1
}
= model_init_classification(model_class = GPT2BaseForSequenceClassification,
model = 'gpt2',
cpoint_path =False, # since we are not using 'hidden layer contatenation' technique
output_hidden_states=42,
seed=gpt2body,
body_model= _model_kwargs) model_kwargs
Loading body weights. This assumes the body is the very first block of your custom architecture
Total parameters: 124444416
Total trainable parameters: 124444416
# resize token embedding
len(_tokenizer)) model.body_model.resize_token_embeddings(
Embedding(50257, 768)
Create ModelController and start training
= [partial(f1_score,average='macro'),accuracy_score]
metric_funcs = ModelController(model,tdc,seed=42) controller
And we can start training our model
= 8e-5
lr =32
bs=0.01
wd= 3
epochs
controller.fit(epochs,lr,=metric_funcs,
metric_funcs=bs,
batch_size=wd,
weight_decay=False,
save_checkpoint=compute_metrics,
compute_metrics )
[849/849 03:08, Epoch 3/3]
Epoch | Training Loss | Validation Loss | F1 Score Department name | Accuracy Score Department name |
---|---|---|---|---|
1 | No log | 0.293438 | 0.736128 | 0.910296 |
2 | 0.743200 | 0.263558 | 0.748740 | 0.918692 |
3 | 0.743200 | 0.264788 | 0.746244 | 0.917587 |
Make predictions
= controller.predict_ddict(ds_type='validation') df_val
-------------------- Start making predictions --------------------
= df_val.to_pandas()
df_val df_val.head()
Title | Review Text | Division Name | Department Name | label | input_ids | attention_mask | pred_Department Name | pred_prob_Department Name | |
---|---|---|---|---|---|---|---|---|---|
0 | general petite . . such a fun jacket ! great t... | general petite | Intimate | 2 | [50256, 50256, 50256, 50256, 50256, 50256, 502... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... | Jackets | 0.879402 | |
1 | simple and elegant | general petite . simple and elegant . i though... | general petite | Tops | 4 | [24622, 4273, 578, 764, 2829, 290, 19992, 764,... | [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... | Tops | 0.998374 |
2 | retro and pretty | general . retro and pretty . this top has a bi... | general | Tops | 4 | [50256, 50256, 50256, 50256, 50256, 50256, 502... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... | Tops | 0.999834 |
3 | summer/fall wear | general petite . summer / fall wear . i first ... | general petite | Dresses | 1 | [50256, 50256, 50256, 50256, 50256, 50256, 502... | [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, ... | Dresses | 0.949195 |
4 | perfect except slip | general petite . perfect except slip . this is... | general petite | Dresses | 1 | [50256, 50256, 50256, 50256, 50256, 50256, 502... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... | Dresses | 0.993209 |
You can try to get your metric to see if it matches your last traing epoch’s above
'Department Name'],df_val['pred_Department Name'],average='macro') f1_score(df_val[
0.7462441580902758