Quick tour
Get up and running with 🌎 Transformers! Whether you’re a developer or an everyday user, this quick tour will help you get started and show you how to use the pipeline() for inference, load a pretrained model and preprocessor with an AutoClass, and quickly train a model with PyTorch or TensorFlow. If you’re a beginner, we recommend checking out our tutorials or course next for more in-depth explanations of the concepts introduced here.
Before you begin, make sure you have all the necessary libraries installed:
Copied
You’ll also need to install your preferred machine learning framework:
PytorchHide Pytorch contentCopied
TensorFlowHide TensorFlow contentCopied
Pipeline
The pipeline() is the easiest and fastest way to use a pretrained model for inference. You can use the pipeline() out-of-the-box for many tasks across different modalities, some of which are shown in the table below:
For a complete list of available tasks, check out the pipeline API reference.
Task | Description | Modality | Pipeline identifier |
Text classification | assign a label to a given sequence of text | NLP | pipeline(task=“sentiment-analysis”) |
Text generation | generate text given a prompt | NLP | pipeline(task=“text-generation”) |
Summarization | generate a summary of a sequence of text or document | NLP | pipeline(task=“summarization”) |
Image classification | assign a label to an image | Computer vision | pipeline(task=“image-classification”) |
Image segmentation | assign a label to each individual pixel of an image (supports semantic, panoptic, and instance segmentation) | Computer vision | pipeline(task=“image-segmentation”) |
Object detection | predict the bounding boxes and classes of objects in an image | Computer vision | pipeline(task=“object-detection”) |
Audio classification | assign a label to some audio data | Audio | pipeline(task=“audio-classification”) |
Automatic speech recognition | transcribe speech into text | Audio | pipeline(task=“automatic-speech-recognition”) |
Visual question answering | answer a question about the image, given an image and a question | Multimodal | pipeline(task=“vqa”) |
Document question answering | answer a question about the document, given a document and a question | Multimodal | pipeline(task=“document-question-answering”) |
Image captioning | generate a caption for a given image | Multimodal | pipeline(task=“image-to-text”) |
Start by creating an instance of pipeline() and specifying a task you want to use it for. In this guide, you’ll use the pipeline() for sentiment analysis as an example:
Copied
The pipeline() downloads and caches a default pretrained model and tokenizer for sentiment analysis. Now you can use the classifier
on your target text:
Copied
If you have more than one input, pass your inputs as a list to the pipeline() to return a list of dictionaries:
Copied
The pipeline() can also iterate over an entire dataset for any task you like. For this example, let’s choose automatic speech recognition as our task:
Copied
Load an audio dataset (see the 🌎 Datasets Quick Start for more details) you’d like to iterate over. For example, load the MInDS-14 dataset:
Copied
You need to make sure the sampling rate of the dataset matches the sampling rate facebook/wav2vec2-base-960h
was trained on:
Copied
The audio files are automatically loaded and resampled when calling the "audio"
column. Extract the raw waveform arrays from the first 4 samples and pass it as a list to the pipeline:
Copied
For larger datasets where the inputs are big (like in speech or vision), you’ll want to pass a generator instead of a list to load all the inputs in memory. Take a look at the pipeline API reference for more information.
Use another model and tokenizer in the pipeline
The pipeline() can accommodate any model from the Hub, making it easy to adapt the pipeline() for other use-cases. For example, if you’d like a model capable of handling French text, use the tags on the Hub to filter for an appropriate model. The top filtered result returns a multilingual BERT model finetuned for sentiment analysis you can use for French text:
Copied
PytorchHide Pytorch content
Use AutoModelForSequenceClassification and AutoTokenizer to load the pretrained model and it’s associated tokenizer (more on an AutoClass
in the next section):
Copied
TensorFlowHide TensorFlow content
Use TFAutoModelForSequenceClassification and AutoTokenizer to load the pretrained model and it’s associated tokenizer (more on an TFAutoClass
in the next section):
Copied
Specify the model and tokenizer in the pipeline(), and now you can apply the classifier
on French text:
Copied
If you can’t find a model for your use-case, you’ll need to finetune a pretrained model on your data. Take a look at our finetuning tutorial to learn how. Finally, after you’ve finetuned your pretrained model, please consider sharing the model with the community on the Hub to democratize machine learning for everyone!🌎
AutoClass
Under the hood, the AutoModelForSequenceClassification and AutoTokenizer classes work together to power the pipeline() you used above. An AutoClass is a shortcut that automatically retrieves the architecture of a pretrained model from its name or path. You only need to select the appropriate AutoClass
for your task and it’s associated preprocessing class.
Let’s return to the example from the previous section and see how you can use the AutoClass
to replicate the results of the pipeline().
AutoTokenizer
A tokenizer is responsible for preprocessing text into an array of numbers as inputs to a model. There are multiple rules that govern the tokenization process, including how to split a word and at what level words should be split (learn more about tokenization in the tokenizer summary). The most important thing to remember is you need to instantiate a tokenizer with the same model name to ensure you’re using the same tokenization rules a model was pretrained with.
Load a tokenizer with AutoTokenizer:
Copied
Pass your text to the tokenizer:
Copied
The tokenizer returns a dictionary containing:
input_ids: numerical representations of your tokens.
attention_mask: indicates which tokens should be attended to.
A tokenizer can also accept a list of inputs, and pad and truncate the text to return a batch with uniform length:
PytorchHide Pytorch contentCopied
TensorFlowHide TensorFlow contentCopied
Check out the preprocess tutorial for more details about tokenization, and how to use an AutoImageProcessor, AutoFeatureExtractor and AutoProcessor to preprocess image, audio, and multimodal inputs.
AutoModel
PytorchHide Pytorch content
🌎 Transformers provides a simple and unified way to load pretrained instances. This means you can load an AutoModel like you would load an AutoTokenizer. The only difference is selecting the correct AutoModel for the task. For text (or sequence) classification, you should load AutoModelForSequenceClassification:
Copied
See the task summary for tasks supported by an AutoModel class.
Now pass your preprocessed batch of inputs directly to the model. You just have to unpack the dictionary by adding **
:
Copied
The model outputs the final activations in the logits
attribute. Apply the softmax function to the logits
to retrieve the probabilities:
Copied
TensorFlowHide TensorFlow content
🌎 Transformers provides a simple and unified way to load pretrained instances. This means you can load an TFAutoModel like you would load an AutoTokenizer. The only difference is selecting the correct TFAutoModel for the task. For text (or sequence) classification, you should load TFAutoModelForSequenceClassification:
Copied
See the task summary for tasks supported by an AutoModel class.
Now pass your preprocessed batch of inputs directly to the model. You can pass the tensors as-is:
Copied
The model outputs the final activations in the logits
attribute. Apply the softmax function to the logits
to retrieve the probabilities:
Copied
All 🌎 Transformers models (PyTorch or TensorFlow) output the tensors before the final activation function (like softmax) because the final activation function is often fused with the loss. Model outputs are special dataclasses so their attributes are autocompleted in an IDE. The model outputs behave like a tuple or a dictionary (you can index with an integer, a slice or a string) in which case, attributes that are None are ignored.
Save a model
PytorchHide Pytorch content
Once your model is fine-tuned, you can save it with its tokenizer using PreTrainedModel.save_pretrained():
Copied
When you are ready to use the model again, reload it with PreTrainedModel.from_pretrained():
Copied
TensorFlowHide TensorFlow content
Once your model is fine-tuned, you can save it with its tokenizer using TFPreTrainedModel.save_pretrained():
Copied
When you are ready to use the model again, reload it with TFPreTrainedModel.from_pretrained():
Copied
One particularly cool 🌎 Transformers feature is the ability to save a model and reload it as either a PyTorch or TensorFlow model. The from_pt
or from_tf
parameter can convert the model from one framework to the other:
PytorchHide Pytorch contentCopied
TensorFlowHide TensorFlow contentCopied
Custom model builds
You can modify the model’s configuration class to change how a model is built. The configuration specifies a model’s attributes, such as the number of hidden layers or attention heads. You start from scratch when you initialize a model from a custom configuration class. The model attributes are randomly initialized, and you’ll need to train the model before you can use it to get meaningful results.
Start by importing AutoConfig, and then load the pretrained model you want to modify. Within AutoConfig.from_pretrained(), you can specify the attribute you want to change, such as the number of attention heads:
Copied
PytorchHide Pytorch content
Create a model from your custom configuration with AutoModel.from_config():
Copied
TensorFlowHide TensorFlow content
Create a model from your custom configuration with TFAutoModel.from_config():
Copied
Take a look at the Create a custom architecture guide for more information about building custom configurations.
Trainer - a PyTorch optimized training loop
All models are a standard torch.nn.Module
so you can use them in any typical training loop. While you can write your own training loop, 🌎 Transformers provides a Trainer class for PyTorch, which contains the basic training loop and adds additional functionality for features like distributed training, mixed precision, and more.
Depending on your task, you’ll typically pass the following parameters to Trainer:
TrainingArguments contains the model hyperparameters you can change like learning rate, batch size, and the number of epochs to train for. The default values are used if you don’t specify any training arguments:
Copied
Load a preprocessing class like a tokenizer, image processor, feature extractor, or processor:
Copied
Load a dataset:
Copied
Create a function to tokenize the dataset:
Copied
Then apply it over the entire dataset with map:
Copied
Now gather all these classes in Trainer:
Copied
When you’re ready, call train() to start training:
Copied
For tasks - like translation or summarization - that use a sequence-to-sequence model, use the Seq2SeqTrainer and Seq2SeqTrainingArguments classes instead.
You can customize the training loop behavior by subclassing the methods inside Trainer. This allows you to customize features such as the loss function, optimizer, and scheduler. Take a look at the Trainer reference for which methods can be subclassed.
The other way to customize the training loop is by using Callbacks. You can use callbacks to integrate with other libraries and inspect the training loop to report on progress or stop the training early. Callbacks do not modify anything in the training loop itself. To customize something like the loss function, you need to subclass the Trainer instead.
Train with TensorFlow
All models are a standard tf.keras.Model
so they can be trained in TensorFlow with the Keras API. 🌎 Transformers provides the prepare_tf_dataset() method to easily load your dataset as a tf.data.Dataset
so you can start training right away with Keras’ compile
and fit
methods.
Load a preprocessing class like a tokenizer, image processor, feature extractor, or processor:
Copied
Create a function to tokenize the dataset:
Copied
Apply the tokenizer over the entire dataset with map and then pass the dataset and tokenizer to prepare_tf_dataset(). You can also change the batch size and shuffle the dataset here if you’d like:
Copied
When you’re ready, you can call
compile
andfit
to start training. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to:Copied
What’s next?
Now that you’ve completed the🌎 Transformers quick tour, check out our guides and learn how to do more specific things like writing a custom model, fine-tuning a model for a task, and how to train a model with a script. If you’re interested in learning more about 🌎 Transformers core concepts, grab a cup of coffee and take a look at our Conceptual Guides!
Last updated