Saving and Loading a Model: Keep What You Learned
Closing the notebook should not erase the model
Your Pune rent model has learned a line. A 750-square-foot flat gets an estimate of ₹25,500 in our tiny teaching dataset. Tomorrow, another program should use that same fitted model without rerunning every training cell.
Saving a model writes its learned state into a file. Loading restores that state so you can call predict. The saved file is called an artifact: a concrete output of training that another process can use.
Tip:Loading a fitted model is not the same as fitting it again. The goal is to preserve the exact learned state, not create a similar model from scratch.
What has to travel with the weights?
A coefficient alone is incomplete. A weight of 30 might mean ₹30 per square foot or ₹30 per hundred square feet. A scaler might have changed the input before the weight was used. A production prediction needs the whole interpretation.
Key Points
- Fitted estimator state, including learned parameters.
- Fitted preprocessing, such as means, scales, imputers, and encoders.
- Feature names, units, order, and expected input types.
- A model version and the code or package environment that can read it.
- The target meaning, evaluation evidence, and training-data reference.
1. Learned state
Fitted preprocessing + fitted estimator
Preserve the transformations that produced the model inputs.
2. Input contract
area_sqft: finite number, square feet
Names, order, units, and invalid-value rules define what the model receives.
3. Environment
Python + tested package versions
An artifact needs compatible code to load and run it.
4. Release evidence
Version + checksum + evaluation record
Keep a known prediction and the previous working release.
Save, reload, and compare one prediction
joblib is a Python library commonly used to persist scikit-learn estimators. This standalone cell fits a toy line, saves it to a temporary directory, loads it back, and checks the prediction.
1from pathlib import Path
2from tempfile import TemporaryDirectory
3import joblib
4import numpy as np
5from sklearn.linear_model import LinearRegression
6
7X = [[500], [700], [900], [1100]]
8y = [18000, 24000, 30000, 36000]
9model = LinearRegression().fit(X, y)
10before = model.predict([[750]])
11
12with TemporaryDirectory() as directory:
13 artifact = Path(directory) / "rent-model.joblib"
14 joblib.dump(model, artifact)
15 restored = joblib.load(artifact)
16 after = restored.predict([[750]])
17 print("File saved:", artifact.exists())
18 print(f"Reloaded rent: ₹{after[0]:,.0f}")
19 print("Predictions match:", bool(np.allclose(before, after)))File saved: True Reloaded rent: ₹25,500 Predictions match: True
TemporaryDirectory cleans up when the cell finishes. For a real release, choose a persistent artifact location. Colab’s local disk can disappear when its runtime ends, so download the artifact or save it to persistent storage.
Record a small manifest
A manifest is a description stored alongside the artifact. It lets another person discover which features are expected, which target the model predicts, and which release they are looking at.
1import json
2import platform
3import sklearn
4
5manifest = {
6 "model_version": "rent-v1",
7 "features": [{"name": "area_sqft", "unit": "square feet"}],
8 "target": "monthly rent in INR",
9 "python_version": platform.python_version(),
10 "sklearn_version": sklearn.__version__,
11 "data_reference": "teaching-example-v1",
12}
13restored_manifest = json.loads(json.dumps(manifest))
14print("Model:", restored_manifest["model_version"])
15print("Feature:", restored_manifest["features"][0]["name"])
16print("Target:", restored_manifest["target"])
17print("Environment recorded:", all(key in restored_manifest for key in ["python_version", "sklearn_version"]))Model: rent-v1 Feature: area_sqft Target: monthly rent in INR Environment recorded: True
These example fields are a starting point. In a real project, also record the training code version, data snapshot, validation metric, and feature schema version. The manifest describes the release; it does not enforce the input contract by itself.
An environment is part of reproducibility
A Python artifact is not a universal document format. Deserialising scikit-learn objects across different package versions can fail or behave differently. Record and reproduce the training environment rather than assuming a future installation will work.
Key Points
- Pin the dependency versions used by the release.
- Test loading in the serving environment before publication.
- Keep a known input and expected prediction for a load-time smoke test.
- If migrating environments, retrain or validate the migration as a new release.
Check the bytes you intended to release
A checksum is a short fingerprint computed from file bytes. If the bytes change, the checksum almost certainly changes. It helps detect an accidental replacement or incomplete transfer.
1import hashlib
2
3released_bytes = b"rent-model-v1"
4copied_bytes = b"rent-model-v1"
5changed_bytes = b"rent-model-v2"
6
7def checksum(data):
8 return hashlib.sha256(data).hexdigest()
9
10print("Copy matches:", checksum(released_bytes) == checksum(copied_bytes))
11print("Changed file matches:", checksum(released_bytes) == checksum(changed_bytes))Copy matches: True Changed file matches: False
This cell uses short byte strings to show the calculation. A real artifact checksum reads the file bytes. A checksum confirms equality against a trusted reference; it does not establish who created the file if the file and checksum come from the same untrusted source.
Only load artifacts from a trusted release path
joblib and Python pickle-style formats can execute code while loading. Do not build a feature that loads arbitrary user-uploaded model files. The serving process should load the artifact selected by your release process.
Other formats can support different portability or trust requirements, but switching formats needs compatibility checks. For this chapter, keep the artifact within a trusted pipeline and test its environment explicitly.
Version the artifact instead of overwriting the only copy
If yesterday’s model is replaced at the same path without a version record, you cannot explain which model produced yesterday’s estimate or return to it after a failed release. Keep immutable versioned artifacts and a separate pointer to the active version.
Key Points
- Preserve the previous working artifact.
- Return or log a model version with predictions.
- Keep the version’s manifest and evaluation evidence together.
- Test that saved preprocessing and estimator belong to the same release.
Where people get confused
Key Points
- A saved estimator is not necessarily a complete prediction pipeline.
- Loading does not improve a model or refresh it with new data.
- A matching checksum does not prove a file is from a trusted publisher.
- A perfect save-load test proves persistence, not generalisation quality.
Quick check
What does saving a fitted model preserve?
It preserves its learned state so another process can restore it and predict without fitting again. Any required preprocessing and input meaning must also be preserved.
Why record dependency versions?
The code that loads a Python model depends on its environment. Different package versions may fail to load it or change behaviour.
Why keep a known input and expected prediction?
They provide a smoke test for whether the loaded release behaves as intended. They do not replace model evaluation.
Why should joblib artifacts come from a trusted release path?
Their pickle-style deserialisation can execute code. Loading an arbitrary model file is not merely reading passive data.
What to carry into the next step
Key Points
- Preserve learned state, input meaning, versions, and a reproducible environment.
- Compare predictions before and after loading.
- Keep old releases available for explanation and rollback.
- Next, a Pipeline keeps preprocessing and the estimator in one fitted object.
Theia
Can you explain this concept?
Practise Saving and Loading a Model: Keep What You Learned with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.