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

Saving and Loading Custom Models

Use save_pretrained() and from_pretrained() to persist and reload models and tokenizers locally.

Saving and Loading Custom Models

What you'll learn: How to persist your models and tokenizers to disk so you can reuse them without re-downloading or re-creating them every time.

Why This Matters

Imagine downloading a massive model file every single time you run your program—that's wasteful and slow! Once you've loaded a pre-trained model (or fine-tuned one), you want to save it locally. Later, you can reload it instantly without hitting the internet.

The Core Concept

Hugging Face Transformers provides two complementary methods:

  • save_pretrained(path) — Saves your model and tokenizer to a local directory
  • from_pretrained(path) — Loads them back from that directory

Think of it like bookmarking your progress in a video game. You save your state to disk, turn off the console, and when you come back, you load from exactly where you left off.

How It Works

from transformers import AutoTokenizer, AutoModel

# Load from Hub initially
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")

# Save to local directory
tokenizer.save_pretrained("./my-saved-model")
model.save_pretrained("./my-saved-model")

# Later, reload from local directory (no internet needed!)
tokenizer = AutoTokenizer.from_pretrained("./my-saved-model")
model = AutoModel.from_pretrained("./my-saved-model")

The saved directory contains configuration files, model weights, and tokenizer vocabulary—everything needed to reconstruct your exact setup.

When to Use This

  • After downloading a large model once
  • After fine-tuning (which we haven't covered yet, but you'll save your custom weights)
  • When deploying to environments without internet access
  • For version control and reproducibility

Key Takeaway: Use save_pretrained() to persist models and tokenizers locally, then from_pretrained() to reload them instantly without re-downloading—essential for efficient development and production deployment.