Course contentsShow
← AI Engineering
Lesson 54 of 1,8862. Working with Pre-trained ModelsFree lesson

Loading Pre-trained Model Weights

Use from_pretrained() to download and cache model weights from Hugging Face Hub or local paths.

Loading Pre-trained Model Weights

What you'll learn: How to download and use pre-trained model weights from the Hugging Face Hub or your local machine using a single method.

The Core Idea

In previous lessons, you learned about AutoClasses like AutoModel and AutoTokenizer. Now you'll see how to actually load the weights of a pre-trained model into memory. The magic happens with the from_pretrained() method—a universal loading function that handles downloading, caching, and initializing models for you.

Think of from_pretrained() as a smart librarian. When you ask for a book (model), it first checks if you already have a copy at home (local cache). If not, it fetches it from the library (Hugging Face Hub) and keeps a copy for next time.

How It Works

from transformers import AutoModel

# Download from Hugging Face Hub (or use cached version)
model = AutoModel.from_pretrained("bert-base-uncased")

The first time you run this, Transformers downloads the model weights (often hundreds of megabytes) and stores them locally. Every subsequent call uses the cached version—no re-downloading needed.

You can also load from a local path if you've saved weights yourself:

# Load from a local directory
model = AutoModel.from_pretrained("/path/to/saved/model")

This same pattern works across all AutoClasses—AutoTokenizer, AutoModelForSequenceClassification, etc. The method name stays consistent, making your code predictable.

Why This Matters

Without from_pretrained(), you'd need to manually download files, manage paths, and load weights into the correct model architecture. This method abstracts all that complexity, letting you go from model name to working code in one line.

Key Takeaway: from_pretrained() is your universal loader—it downloads models from Hugging Face Hub (or loads from local paths), caches them automatically, and initializes them ready for use.