Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Core / Securing models and the ML supply chain

Securing models and the ML supply chain cheat sheet

Set up and baseline (lessons 0 and 1)

TaskCommand or code
Check the labpython3 labcheck.py
Install the pinned versionspip install -r requirements.txt
Load the fixed split of the digitsX_train, X_test, y_train, y_test = digits()
Train the course model (a logistic regression that settles: the same printed numbers on every processor)model = train(X_train, y_train)
The same kind of model, not yet trainedlogistic() or logistic(0.01)
One recall for each classrecalls(model, X_test, y_test)
Keep a measured valuerecord("digits.accuracy", 0.9533)
Show the results filepython3 common.py

Poison the data and detect it (lesson 2)

TaskCommand or code
Flip a share of the labels of one classflip_labels(y, 8, 3, 0.2)
Add copies with a wrong labelinject(X, y, 8, 3, 65)
Accuracy gate and class gategates(model, X_val, y_val, baseline)
Compare with a fixed baseline, not with last weekacc >= baseline - ACC_DROP
Run the whole table (five seeds for each row)python3 poison.py

Backdoors (lesson 3)

TaskCommand or code
Stamp a corner patch on imagesstamp(X, 6, 6)
Plant a backdoorplant(X, y, 20)
Success rate of a triggersuccess_rate(model, X_test, y_test)
Scan for a triggerscan(model, X_test) gives (row, col, class, share)
Flag a modelshare > SCAN_LIMIT (0.60)
Remove the poisoned images, then retrainremove(X, y, row, col), then train(Xc, yc)
Tune a downloaded network (a little tuning may leave a backdoor)fine_tune(net, X, y, 40)

Evasion and hardening (lesson 4)

TaskCommand or code
Gradient attack on a linear modelfgsm(model, X, y, 2)
Black-box search with a query budgetsearch(model.predict_proba, X, y, 300)
Train on noisy copiesnoisy_copies(X, y, sigma=6)
Input checkinput_ok(X, limit)
The limit of the checknp.percentile(roughness(X_train), 99)

Privacy leaks and defences (lessons 5 and 6)

TaskCommand or code
Membership attack with a shadow modelattack_accuracy(lambda: RandomForestClassifier(random_state=0))
Mean confidence on members and non-membersmean_confidence(make)
Rebuild a class from scoresinvert(model, 3)
Show an 8 by 8 imageshow(x)
Show labels onlyLabelsOnly(model)
Train on noisy imagesNoisyTraining(model, 3)
Count with epsilon privacytrue_count + rng.laplace(0, 1 / epsilon)
Clip what one user adds to a countclipped_count(users, wanted, cap=1)

Model theft (lesson 7)

TaskCommand or code
Copy a model from its labelscopy_from(victim, 225)
Fidelity of a copyfidelity(copy, victim)
Profile the clients of a logprofile(make_log())
Flag by rate and by near-copiesflagged(stats, per_minute=10, near_share=0.3)
Make and count a watermarkwatermark(), hits(model, marks)
Copy of the file with half the treeshalf_the_trees(model)

Model files (lessons 8 and 9)

TaskCommand or code
Read a pickle without loading itpython3 -m pickletools file.pkl
Export a model to JSON or arraysexport_json(model, "models/target.json"), export_npz(...)
Save and load weights as safetensors (make coef_ contiguous first)save_file({"coef": np.ascontiguousarray(model.coef_), ...}, path), load_file(path)
Read the header of a safetensors filestruct.unpack("<Q", raw[:8]), then json.loads(raw[8:8 + size])
Hash a filesha256_of("models/target.json")
Load only what is proven (.json, .npz or .safetensors)load_model(path, expected_sha256)
Refuse a small .npz that unpacks to a huge arraythe loader adds the unpacked sizes: sum(i.file_size for i in zipfile.ZipFile(path).infolist())
Load a pickle that you made yourselfconvert_pickle("candidate/model.pkl", "candidate/model.json")

Release gates and the scorecard (lessons 10 and 11)

TaskCommand or code
Build a folder that can be releasedpython3 ship.py ship
Run the seven gatespython3 gates.py ship
Train again from the recorded data and comparereproduce_gate(model, X, y)
Make two damaged folders to test the gatespython3 makebad.py
Stop a deployment when a gate failspython3 gates.py ship && ./deploy.sh
Grade a folder on five areaspython3 scorecard.py ship
Grade a weaker model (the same ship.py, fewer images)python3 weak.py weak, then python3 scorecard.py weak
Read the exit codeecho "exit code $?"

The scenario test (lesson 12)

TaskCommand or code
Make the folder to gradepython3 colleague.py
Grade, convert, fix, grade againpython3 scorecard.py candidate, convert_pickle(...), remove(...), python3 scorecard.py candidate
Write the decision notedecision.md, first line HOLD or SHIP

Safety habits