liveBook · Manning

Foundational model

As explained in Hugging Face in Action

A pretrained model is a machine learning model whose parameters have already been learned from a training dataset before being adapted or used for a particular task.

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

How Hugging Face in Action covers it

How deep it goes
Explains reuse, discovery, inference, and fine-tuning, while stopping short of neural-network fundamentals and training-from-scratch methods.
Examples
Demonstrates DistilBERT sentiment classification, DETR object detection, Model Hub search, and device-aware sequence-classification loading.
What it assumes
Assumes basic Python use while teaching model selection and inference without requiring knowledge of text classification or model training.
What you can do after
Apply it via Hugging Face Transformers pipelines, Hosted Inference API, or task-specific model loaders.
How it connects
Connects pretrained models with Transformers, pipelines, transfer learning, fine-tuning, sentiment analysis, and object detection.

Also explained in

Compare how 21 books explain foundational model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.