mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-05-08 18:01:20 -05:00
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1617
Author: @profvjreddi
Created: 4/30/2026
Status: ✅ Merged
Merged: 4/30/2026
Merged by: @profvjreddi
Base:
dev← Head:fix/perceptron-seed📝 Commits (1)
6220345fix(tinytorch): perceptron weights deterministic across runs (#1611)📊 Changes
2 files changed (+7 additions, -2 deletions)
View changed files
📝
tinytorch/milestones/01_1958_perceptron/01_rosenblatt_forward.py(+3 -1)📝
tinytorch/src/03_layers/03_layers.py(+4 -1)📄 Description
Summary
Fixes the bug reported in #1611: every run of Milestone 1 produced the same Perceptron weights, even though the on-screen text promises "No random seed - each run will be different!".
Root cause
tinytorch/src/03_layers/03_layers.pyhad a module-leveljust under the imports (line 65).
Linear.__init__reused this single seeded RNG for every weight init viarng.standard_normal(...), so every freshly-constructedLinearproduced identical weights across processes. The milestone script01_rosenblatt_forward.pyhad its own seededrng = np.random.default_rng(7)(line 88) used for the data clusters too, contradicting the displayed message.Fix
Drop the seed in two places:
tinytorch/src/03_layers/03_layers.py- module-levelrngis now unseeded (np.random.default_rng()). Local seeded RNGs inside test/demo blocks (lines ~829, 848, 981) are intentionally left alone since those want determinism for reproducible self-tests.tinytorch/milestones/01_1958_perceptron/01_rosenblatt_forward.py- the data-generationrngis also unseeded so cluster points vary too.Verification
After re-exporting
tinytorch/core/layers.pyvianbdev.export.nb_export, ran the milestone twice and compared (excerpt ofdiff /tmp/run1.txt /tmp/run2.txt):Cluster points, predictions, and final accuracy all differ between runs — exactly what the milestone advertises. Independent
Linear(8,4)constructions within the same process also produce distinct weight matrices (np.array_equal(a, b) == False).Relates to #1611
Test plan
tito dev export 03) and confirmtinytorch/core/layers.pynow has unseededrng = np.random.default_rng()python3 tinytorch/milestones/01_1958_perceptron/01_rosenblatt_forward.py < /dev/null) and verify accuracy/cluster output differspython3 -m pytest tinytorch/tests/03_layers/ -k "linear or layers_core" -v(after exporting full package)03_layers.py(lines ~829, 848, 981) still see deterministic output thanks to their localrng = np.random.default_rng(7)— those were left intact🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.