Translation
Last updated
Last updated
Translation converts a sequence of text from one language to another. It is one of several tasks you can formulate as a sequence-to-sequence problem, a powerful framework for returning some output from an input, like translation or summarization. Translation systems are commonly used for translation between different language texts, but it can also be used for speech or some combination in between like text-to-speech or speech-to-text.
This guide will show you how to:
Finetune on the English-French subset of the dataset to translate English text to French.
Use your finetuned model for inference.
The task illustrated in this tutorial is supported by the following model architectures:
, , , , , , , , , , , , , , , , , , , , , , ,
Before you begin, make sure you have all the necessary libraries installed:
Copied
We encourage you to login to your BOINC AI account so you can upload and share your model with the community. When prompted, enter your token to login:
Copied
Start by loading the English-French subset of the dataset from the 🌍 Datasets library:
Copied
Copied
Then take a look at an example:
Copied
translation
: an English and French translation of the text.
The next step is to load a T5 tokenizer to process the English-French language pairs:
Copied
The preprocessing function you want to create needs to:
Prefix the input with a prompt so T5 knows this is a translation task. Some models capable of multiple NLP tasks require prompting for specific tasks.
Tokenize the input (English) and target (French) separately because you can’t tokenize French text with a tokenizer pretrained on an English vocabulary.
Truncate sequences to be no longer than the maximum length set by the max_length
parameter.
Copied
Copied
PytorchHide Pytorch contentCopied
TensorFlowHide TensorFlow contentCopied
Copied
Then create a function that passes your predictions and labels to compute
to calculate the SacreBLEU score:
Copied
Your compute_metrics
function is ready to go now, and you’ll return to it when you setup your training.
PytorchHide Pytorch content
Copied
At this point, only three steps remain:
Copied
Copied
TensorFlowHide TensorFlow content
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:Copied
Copied
Copied
Copied
Copied
Copied
Then bundle your callbacks together:
Copied
Copied
Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!
Great, now that you’ve finetuned a model, you can use it for inference!
Come up with some text you’d like to translate to another language. For T5, you need to prefix your input depending on the task you’re working on. For translation from English to French, you should prefix your input as shown below:
Copied
Copied
You can also manually replicate the results of the pipeline
if you’d like:
PytorchHide Pytorch content
Tokenize the text and return the input_ids
as PyTorch tensors:
Copied
Copied
Decode the generated token ids back into text:
Copied
TensorFlowHide TensorFlow content
Tokenize the text and return the input_ids
as TensorFlow tensors:
Copied
Copied
Decode the generated token ids back into text:
Copied
Split the dataset into a train and test set with the method:
To apply the preprocessing function over the entire dataset, use 🌍Datasets method. You can speed up the map
function by setting batched=True
to process multiple elements of the dataset at once:
Now create a batch of examples using . It’s more efficient to dynamically pad the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length.
Including a metric during training is often helpful for evaluating your model’s performance. You can quickly load a evaluation method with the 🌍 library. For this task, load the metric (see the 🌍 Evaluate to learn more about how to load and compute a metric):
If you aren’t familiar with finetuning a model with the , take a look at the basic tutorial !
You’re ready to start training your model now! Load T5 with :
Define your training hyperparameters in . The only required parameter is output_dir
which specifies where to save your model. You’ll push this model to the Hub by setting push_to_hub=True
(you need to be signed in to BOINC AI to upload your model). At the end of each epoch, the will evaluate the SacreBLEU metric and save the training checkpoint.
Pass the training arguments to along with the model, dataset, tokenizer, data collator, and compute_metrics
function.
Call to finetune your model.
Once training is completed, share your model to the Hub with the method so everyone can use your model:
If you aren’t familiar with finetuning a model with Keras, take a look at the basic tutorial !
Then you can load T5 with :
Convert your datasets to the tf.data.Dataset
format with :
Configure the model for training with . Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to:
The last two things to setup before you start training is to compute the SacreBLEU metric from the predictions, and provide a way to push your model to the Hub. Both are done by using .
Pass your compute_metrics
function to :
Specify where to push your model and tokenizer in the :
Finally, you’re ready to start training your model! Call with your training and validation datasets, the number of epochs, and your callbacks to finetune the model:
For a more in-depth example of how to finetune a model for translation, take a look at the corresponding or .
The simplest way to try out your finetuned model for inference is to use it in a . Instantiate a pipeline
for translation with your model, and pass your text to it:
Use the method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the API.
Use the method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the API.