LoRA for token classification
Last updated
Last updated
Low-Rank Adaptation (LoRA) is a reparametrization method that aims to reduce the number of trainable parameters with low-rank representations. The weight matrix is broken down into low-rank matrices that are trained and updated. All the pretrained model parameters remain frozen. After training, the low-rank matrices are added back to the original weights. This makes it more efficient to store and train a LoRA model because there are significantly fewer parameters.
ð¡ Read to learn more about LoRA.
This guide will show you how to train a model with LoRA on the dataset for token classification.
Before you begin, make sure you have all the necessary libraries installed:
Copied
Letâs start by importing all the necessary libraries youâll need:
ð Transformers for loading the base roberta-large
model and tokenizer, and handling the training loop
ð Datasets for loading and preparing the bionlp2004
dataset for training
ð Evaluate for evaluating the modelâs performance
ð PEFT for setting up the LoRA configuration and creating the PEFT model
Copied
Copied
Copied
Copied
Now you can write an evaluation function to compute the metrics from the model predictions and labels, and return the precision, recall, F1, and accuracy scores:
Copied
Initialize a tokenizer and make sure you set is_split_into_words=True
because the text sequence has already been split into words. However, this doesnât mean it is tokenized yet (even though it may look like it!), and youâll need to further tokenize the words into subwords.
Copied
Youâll also need to write a function to:
Ignore the special tokens by setting them to -100
.
Label the first token of a given entity.
Copied
Copied
Finally, create a data collator to pad the examples to the longest length in a batch:
Copied
Copied
task_type
, token classification (TaskType.TOKEN_CLS
)
r
, the dimension of the low-rank matrices
lora_alpha
, scaling factor for the weight matrices
lora_dropout
, dropout probability of the LoRA layers
bias
, set to all
to train all bias parameters
ð¡ The weight matrix is scaled by lora_alpha/r
, and a higher lora_alpha
value assigns more weight to the LoRA activations. For performance, we recommend setting bias
to None
first, and then lora_only
, before trying all
.
Copied
Copied
Copied
Copied
Once training is complete, you can store and share your model on the Hub if youâd like. Log in to your BOINC AI account and enter your token when prompted:
Copied
Copied
To use your model for inference, load the configuration and model:
Copied
Get some text to tokenize:
Copied
Pass the inputs to the model, and print out the model prediction for each token:
Copied
The dataset includes tokens and tags for biological structures like DNA, RNA and proteins. Load the dataset:
The tags
values are defined in the label ids . The letter that prefixes each label indicates the token position: B
is for the first token of an entity, I
is for a token inside the entity, and 0
is for a token that is not part of an entity.
Then load the framework which includes several metrics - precision, accuracy, F1, and recall - for evaluating sequence labeling tasks.
Map each token to their respective word with the method.
Use to apply the tokenize_and_align_labels
function to the dataset:
Now youâre ready to create a . Start by loading the base roberta-large
model, the number of expected labels, and the id2label
and label2id
dictionaries:
Define the with:
Pass the base model and peft_config
to the get_peft_model()
function to create a . You can check out how much more efficient training the is compared to fully training the base model by printing out the trainable parameters:
From the ð Transformers library, create a class and specify where you want to save the model to, the training hyperparameters, how to evaluate the model, and when to save the checkpoints:
Pass the model, TrainingArguments
, datasets, tokenizer, data collator and evaluation function to the class. The Trainer
handles the training loop for you, and when youâre ready, call to begin!
Upload the model to a specific model repository on the Hub with the method: