Sub topic 1
What Is Inside a Model File
A trained model is more than a file of numbers. It is a model artifact: a saved package that lets another program rebuild or use a model. The artifact may contain learned weights, the model architecture, training settings, metadata, and sometimes the state of the training process.
Prerequisites
This chapter assumes that you can read basic Python, understand classes and special methods, and know that model weights are learned numerical parameters. You should also know the basic purpose of a deep learning framework such as PyTorch.
The main parts of a model artifact
Weights are arrays of numbers stored in tensors. A tensor is a multidimensional array used by machine learning libraries. For example, a classifier might store a matrix of weights and a vector of biases for each layer.
The architecture describes how those tensors are used. It may be represented by Python code, a graph of operations, or a framework-specific structure. A weights-only file does not always contain enough information to reconstruct the architecture. You may need to create the model in code before loading its weights.
A training checkpoint is a broader artifact. It can include:
- The model's weights.
- The optimizer state, such as momentum values.
- The current training step or epoch.
- The learning rate and other configuration values.
- Validation metrics.
- A vocabulary, label mapping, or preprocessing settings.
- Version information for the code and libraries.
These parts have different purposes. Inference usually needs the architecture and weights. Resuming training also needs the optimizer state and training progress.
| Part | Purpose | Needed for inference? |
|---|---|---|
| Model weights | Store learned parameters | Yes |
| Architecture | Define computation | Usually |
| Optimizer state | Resume training | No |
| Metadata | Record labels, versions, and settings | Often |
Serialization: turning objects into bytes
Serialization is the process of converting in-memory data into bytes that can be stored or transferred. Deserialization reverses that process. A model framework chooses a serialization format that describes tensors, containers, numbers, and other values.
For example, this PyTorch checkpoint stores a dictionary with named weights and simple metadata:
import torch
from torch import nn
model = nn.Sequential(nn.Linear(4, 2), nn.ReLU())
checkpoint = {
"model_state": model.state_dict(),
"metadata": {"input_features": 4, "labels": ["cat", "dog"]},
}
torch.save(checkpoint, "classifier.pt")
loaded = torch.load("classifier.pt", map_location="cpu", weights_only=True)
model.load_state_dict(loaded["model_state"])
print(sorted(loaded))The saved file is a binary representation. Opening it in a text editor will not show the model as readable Python. The framework must interpret the bytes and recreate the stored objects.
A model file is a contract
A model artifact works only when the loader and the artifact agree about important details:
- Tensor names and shapes must match the model.
- Tensor data types must be supported.
- The architecture must use compatible operations.
- Preprocessing must match the process used during training.
- Library and format versions may matter.
A file can load successfully and still produce incorrect predictions if the input normalization or label order is wrong. Treat preprocessing rules and metadata as part of the model, even when they are stored separately.
Exercise
Why might a weights-only file need model code before it can be used?
Show answer
The file contains parameter values but may not describe the layers and operations that use those values. The code creates that architecture, and the loader fills it with the saved weights.