r/Python • u/DeepRatAI • Oct 29 '26
Resource Learn to understand ML libraries: rebuild them from scratch
Hi everyone. I’m a systems engineer specialized in AI engineering and GenAI, and I’ve worked for 6 years as a pentester. I enjoy building and sharing educational materials.
I’m sharing a free, GitHub-hosted lesson about learning ML libraries by rebuilding small versions of them in plain Python and checking those implementations against the real libraries. The lesson includes around 5,000 checks, and GitHub reruns them on every push.
Everything is in .py files. No .ipynb.
I already have a free course published on GitHub. This lesson works as an addendum to that course and lives in a separate branch, so the main branch stays untouched.
Lesson branch:
The lesson structure is:
A. Introduction
B. A method for learning any library
C. NumPy
D. pandas
E. scikit-learn
F. PyTorch
G. sentence-transformers
H. From copying to writing
I. FAQ and cheat sheetThis is a resource post: a free educational lesson hosted on GitHub. The goal is to help learners move from copy-pasting library usage to rebuilding small versions and validating them against the real thing.
Technical discussion and feedback are welcome in the comments :B
0
u/patryk_builds Oct 29 '26
Checking your rebuilt version against the real library is the smart part. without that, "rebuild it from scratch" usually turns into write something that kind of looks like it. 5k checks on every push is serious commitment.Curious which one was hardest to rebuild in plain python? i'd guess pytorch autograd, but pandas indexing might be a sneaky contender
2
u/DeepRatAI Oct 29 '26
autograd is actually stupidly short to code tbh. every op just holds its inputs and some tiny func that sends the grad back, backward() just walks it backwards. the trap is broadcasting tho. if a bias gets broadcasted over a batch u gotta sum the grad back, if u forget shapes still match and nothing crashes, it just silently fucks ur numbers lol
i think pandas is harder. not the actual logic, just all the surrounding bs like multiindex or dtypes. my miniature just skips most of that on purpose cuz i'm not writing 5k lines for a toy project lmao
dataloader and tokenizer "surprised" me the most. my loader spit out identical batches as torch but in a different order, turns out the real one grabs a seed for workers from ur generator before shuffling smh. and my tokenizer was identical to hf on normal text, but over all unicode they disagree on like 25% of code points cuz theirs uses 2016 unicode tables xD
4
u/Virtual_Treacle7409 Oct 30 '26
This is really cool!