Set-up · 20 minutes
Set up your lab for Securing models and the ML supply chain
- Create the lab folder and a Python environment with pinned versions
- Check that the digits model trains to the accuracy that the lessons expect
- Prove with a check script that the lab is ready
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.
What you need
- The courses "How AI works" and "AI security fundamentals". This course attacks and then defends models that you train yourself, and it does not teach again how to train, test or measure one. Lessons 2 and 3 of "AI security fundamentals" poison a digits model and measure evasion and a privacy gap. This course goes further on every one of those.
- Python 3.12 or newer. The pinned libraries need it.
- Python at the level of "Python for security work": functions, files, dictionaries, and
tryandexcept. The lessons also use classes (lessons 6 and 8) and a few short forms such aslambdaandf(*pair). Each one is explained where it first appears. - A terminal and a text editor. No GPU, no account, no paid service, and no model server: every model of this course is a scikit-learn model that trains in seconds on a laptop.
- About 500 MB of free disk space. After the install, every lesson runs offline.
Everything runs on your own computer, in one folder: ~/krlab/securing-models. The only models that you attack are the ones that you train in this folder. Every attack is followed by a defence that you measure.
Set it up
1. Make the lab folder
mkdir -p (make directory, with its parents) creates the folder. cd (change directory) moves you into it. pwd (print working directory) shows where you are.
mkdir -p ~/krlab/securing-models
cd ~/krlab/securing-models
pwd/home/you/krlab/securing-models2. Make or reuse a virtual environment
A virtual environment is a private folder of Python packages. If you finished "How AI works", its environment has the right versions: switch it on with source ~/krlab/how-ai-works/.venv/bin/activate. If you did not, make a new one here with python3 -m venv .venv, then switch it on with source .venv/bin/activate. Your prompt starts with (.venv) when it works. Run the source line at the start of every lesson.
3. Pin the versions
A pinned version is an exact one, written with ==. A model is numbers, and the numbers can change between versions of a library. Everyone who installs the same versions gets the same numbers as the lessons. Save the five lines below as requirements.txt. pip install -r requirements.txt installs the list (-r means "read the list from a file"). safetensors is a library for a file format that holds numbers and no code (lessons 8 and 9). If your environment came from "How AI works", the command installs only that one, and the others stay as they are.
requirements.txt
scikit-learn==1.9.1
numpy==2.5.5
scipy==1.18.1
joblib==1.6.0
safetensors==0.8.0pip install -r requirements.txtCheck your lab
labcheck.py checks the Python version, the environment, the folder, the five libraries (the first two parts of each version must match) and one model. The model check trains the digits classifier that the course uses and expects a test accuracy that rounds to 0.95. The classifier uses a solver that trains until the weights settle (lesson 1 says why), so the numbers that this course prints are the same on every processor (the last digits of the weights may differ). New words in the script: sys.prefix is the folder of the Python that runs, and it differs from sys.base_prefix only inside a virtual environment. __import__(name) loads a library whose name is in a variable. Path.home() is your home folder.
labcheck.py
"""labcheck.py: prove that the lab of "Securing models and the ML supply chain" works."""
import sys
from pathlib import Path
PINNED = {"sklearn": "1.9", "numpy": "2.5", "scipy": "1.18", "joblib": "1.6", "safetensors": "0.8"}
results = []
def check(name, ok, detail):
"""Print one line for one requirement and remember the result."""
results.append(ok)
print(("ok " if ok else "FAIL ") + name.ljust(12) + detail)
v = sys.version_info
check("python", v >= (3, 12), f"{v.major}.{v.minor}.{v.micro}")
check("venv", sys.prefix != sys.base_prefix, sys.prefix)
folder = Path.home() / "krlab" / "securing-models"
check("folder", folder.is_dir(), str(folder))
for module, want in PINNED.items():
try:
lib = __import__(module)
have = ".".join(lib.__version__.split(".")[:2])
check(module, have == want, f"{lib.__version__} (the lessons use {want}.x)")
except ImportError:
check(module, False, "not installed")
try:
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
model = LogisticRegression(solver="newton-cholesky", tol=1e-10, max_iter=100).fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
check("model", round(accuracy, 2) == 0.95, f"digits model, test accuracy {accuracy:.4f}")
except Exception as error:
check("model", False, str(error))
print(f"lab ready: {sum(results)} of {len(results)}")
sys.exit(0 if all(results) else 1)python3 labcheck.pyok python 3.14.4
ok venv /home/you/krlab/securing-models/.venv
ok folder /home/you/krlab/securing-models
ok sklearn 1.9.1 (the lessons use 1.9.x)
ok numpy 2.5.5 (the lessons use 2.5.x)
ok scipy 1.18.1 (the lessons use 1.18.x)
ok joblib 1.6.0 (the lessons use 1.6.x)
ok safetensors 0.8.0 (the lessons use 0.8.x)
ok model digits model, test accuracy 0.9533
lab ready: 9 of 9Your Python version and your folder can differ. Every other line must be the same. Type the last line of your own output.
If it fails
Read the first line that starts with FAIL. Fix that one, then run python3 labcheck.py again.
| The script says | What it means | What to do |
|---|---|---|
FAIL python | Python is older than 3.12. | Install Python 3.12 or newer, then make the environment again with that python3. |
FAIL venv | You did not switch the environment on. | Run source .venv/bin/activate (or the environment of "How AI works"). |
FAIL folder | The folder is not under your home folder. | Run step 1 again. The script looks for ~/krlab/securing-models. |
FAIL sklearn 1.8.0 (the lessons use 1.9.x) | A different version is installed. | Run pip install -r requirements.txt again. |
FAIL model | The accuracy is not 0.95. | Check the version lines first. A different version of scikit-learn can give a different number. |
If pip cannot find a pinned version for your system, the lab cannot give the numbers of the lessons: the page checks values that were measured with the pinned versions. You may run the lessons with other versions at your own risk, and the numbers can differ. Then record what you have: pip freeze > my-versions.txt.