P-tuning for sequence classification
Last updated
Last updated
It is challenging to finetune large language models for downstream tasks because they have so many parameters. To work around this, you can use prompts to steer the model toward a particular downstream task without fully finetuning a model. Typically, these prompts are handcrafted, which may be impractical because you need very large validation sets to find the best prompts. P-tuning is a method for automatically searching and optimizing for better prompts in a continuous space.
đź’ˇ Read to learn more about p-tuning.
This guide will show you how to train a model (but you can also use any of the GPT, OPT, or BLOOM models) with p-tuning on the mrpc
configuration of the benchmark.
Before you begin, make sure you have all the necessary libraries installed:
Copied
To get started, import 🌍 Transformers to create the base model, 🌍 Datasets to load a dataset, 🌍 Evaluate to load an evaluation metric, and 🌍 PEFT to create a and setup the configuration for p-tuning.
Define the model, dataset, and some basic training hyperparameters:
Copied
Copied
From 🌍 Evaluate, load a metric for evaluating the model’s performance. The evaluation module returns the accuracy and F1 scores associated with this specific task.
Copied
Now you can use the metric
to write a function that computes the accuracy and F1 scores. The compute_metric
function calculates the scores from the model predictions and labels:
Copied
Initialize the tokenizer and configure the padding token to use. If you’re using a GPT, OPT, or BLOOM model, you should set the padding_side
to the left; otherwise it’ll be set to the right. Tokenize the sentence pairs and truncate them to the maximum length.
Copied
Copied
Copied
task_type
: the type of task you’re training on, in this case it is sequence classification or SEQ_CLS
num_virtual_tokens
: the number of virtual tokens to use, or in other words, the prompt
encoder_hidden_size
: the hidden size of the encoder used to optimize the prompt parameters
Copied
Copied
Copied
Copied
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
Once the model has been uploaded to the Hub, anyone can easily use it for inference. Load the configuration and model:
Copied
Get some text and tokenize it:
Copied
Pass the inputs to the model to classify the sentences:
Copied
Next, load the mrpc
configuration - a corpus of sentence pairs labeled according to whether they’re semantically equivalent or not - from the benchmark:
Use to apply the tokenize_function
to the dataset, and remove the unprocessed columns because the model won’t need those. You should also rename the label
column to labels
because that is the expected name for the labels by models in the 🌍 Transformers library.
Create a collator function with to pad the examples in the batches to the longest
sequence in the batch:
P-tuning uses a prompt encoder to optimize the prompt parameters, so you’ll need to initialize the with several arguments:
Create the base roberta-large
model from , and then wrap the base model and peft_config
with get_peft_model()
to create a . If you’re curious to see how many parameters you’re actually training compared to training on all the model parameters, you can print it out with :
From the 🌍 Transformers library, set up the class with where you want to save the model to, the training hyperparameters, how to evaluate the model, and when to save the checkpoints:
Then pass the model, TrainingArguments
, datasets, tokenizer, data collator, and evaluation function to the class, which’ll handle the entire training loop for you. Once you’re ready, call to start training!
Upload the model to a specifc model repository on the Hub with the function: