A pretrained model has learned parameters from a large dataset before being adapted or used for a particular task. Reusing one avoids training a model from scratch and lets developers build applications quickly. section 1.1 section 3.2.1
Run sentiment analysis
from transformers import pipeline classifier = pipeline( "text-classification", model="distilbert-base-uncased-finetuned-sst-2-english", revision="af0f99b" ) result = classifier("The film was witty and enjoyable.") print(result) The pipeline loads a task-specific pretrained model and returns a sentiment label with its confidence. It hides the mechanics of text classification and model training behind a high-level API. section 1.1
from transformers import pipeline
classifier = pipeline('text-classification',
model = 'distilbert-base-uncased-finetuned-sst-2-english',
revision = 'af0f99b')
Use the listing to see the minimal Transformers code for loading a versioned pretrained model through a pipeline. section 1.1
Pretrained models cover text, image, and audio tasks. A model repository usually documents its task and architecture, while a model widget or Hosted Inference API can provide browser-based testing before local integration. section 1.2 section 4.0
A pretrained model can be used in several ways:
- Select a checkpoint by task, architecture, language, or performance metrics. section 1.4.2
- Run inference directly, such as sentiment analysis or object detection. section 1.1 section 1.2
- Fine-tune it on a smaller domain-specific dataset instead of training from scratch. section 6.1
Listing 6.4 Loading a pretrained model for sequence classification
from transformers import AutoModelForSequenceClassification
import torch
model = AutoModelForSequenceClassification.from_pretrained( #1
model_checkpoint, num_labels = 2)
if torch.backends.mps.is_available(): #2
device = torch.device("mps")
else:
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
model.to(device) #3
Use the listing to inspect a sequence-classification loader and move the model to MPS, CUDA, or CPU depending on the available device. section 6.1.4




















