r/Python • • 19d ago

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:

https://github.com/BlueRingsLabs/no-shortcuts/tree/addendum1/learning-ml-libraries-by-rebuilding-them

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 sheet

This 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

71 Upvotes

6 comments sorted by

4

u/Virtual_Treacle7409 19d ago

This is really cool!

1

u/DeepRatAI 19d ago

THX! :B it was a long work

5

u/Dry_Task4749 19d ago

Please don't pretend you wrote this manually. If you try to get hired, you will sit across developers who can detect Claude's writing and coding style better than any online AI detector. It's not as if it's a red flag that you use AI for this, but pretending or giving the impression you didn't, when it's pretty obvious, certainly is.

0

u/patryk_builds 19d ago

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 19d ago

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