r/deeplearning • • 10d ago

I built a neural network from scratch using only NumPy.

Post image

I wanted to know what actually happens inside loss.backward().

So I built a neural network with nothing but NumPy. Every gradient derived on paper, every layer written by hand, every derivative checked numerically.

98.05% on MNIST.

Code and derivations: https://github.com/Meckmin/mnist-numpy-from-scratch

149 Upvotes

71 comments sorted by

26

u/ARDiffusion 9d ago

God why’s everyone in the comments being so negative? Congratulations, this is very impressive! Great way to learn too :)

4

u/Necessary_Guest_4412 9d ago

thank you so much

3

u/valuat 9d ago

That's the only way to learn. I implemented a MLP in pure Python when I didn't know Python; I knew the math (and C, MATLAB, R) but no Python. I left this *exercise* way more confident about my Python abilities. Well done!

12

u/MrMoussab 10d ago

Best way to learn. Check Karpathy's micrograd

2

u/Necessary_Guest_4412 10d ago

yeah i know that, thanks for advice 👊🏻

7

u/Cool_Mono 9d ago

Don’t listen to reddit internet haters. From a data scientist, very nicely done! Keep going

1

u/Necessary_Guest_4412 9d ago

thank you so much 

8

u/Confident_Luck2359 9d ago

A few years ago this was a normal part of any machine learning curriculum. You're doing it right.

2

u/Necessary_Guest_4412 9d ago

🙏🏻🙏🏻

3

u/mah-sam 7d ago

I wanted to say that badly. It's interesting how people directly go to developing llms without even learning the basics currently.

3

u/bxssl 10d ago

Seems that we had the same idea and converged on the same implementation! https://github.com/Ojansen/mathematically-correct-machine-learning

1

u/Necessary_Guest_4412 9d ago

that seems very cool. it is nice to see someone has same goals

3

u/codewithmessi 9d ago

Well done man, i am also learning NNs

3

u/Alive_Horse_2914 7d ago • Bravo

Nice work! This is exactly how I got started too! Learning the fundamentals gives such a deep understanding that has been invaluable during my career. If you want a new project to work on, might I suggest implementing an auto-grad system and training a neural net with that? That’s the way libraries like PyTorch and TensorFlow work and I doubt 90% of the people that work with them know what’s going on under the hood

1

u/Necessary_Guest_4412 7d ago

thank you for the advice. i noted it down. that will be the next project i am going to work on.

4

u/HMasterSunday 10d ago

how much was you vs the llm? if you had an llm doing it, what did you actually learn?

3

u/Necessary_Guest_4412 10d ago

llm is used only for readme

12

u/AntiquatedMLE 10d ago

So did everyone else back in 2013…

45

u/Necessary_Guest_4412 10d ago

Dude I was 8 in 2013.

37

u/Silverfrost_01 10d ago • Bravo

Taking the time to do this is worthy of praise in my opinion. There are not many people who will actually go to the lengths of understanding these things that deeply anymore.

4

u/Shreyanak_exe 10d ago

I second this. I had to write out the entire matrix multiplications on large papers. I wish I could show them, but I recently turned it into a static page: https://neural-net-scratch.vercel.app/

0

u/[deleted] 10d ago

[deleted] — view deleted comment

1

u/Necessary_Guest_4412 10d ago

what do you mean

10

u/fralbalbero 10d ago

I'm pretty sure he did it for himself to learn, not to brag...

-4

u/cave_men 9d ago

What would be the motivation to post it here, if the sole purpose was learning?

2

u/Necessary_Guest_4412 9d ago

getting feedbacks? dude why would I show off on such an anonymous platform? many users provided me with valuable feedback.

-2

u/cave_men 9d ago

Thats good to hear, but no reason to be that sensitive.

2

u/Necessary_Guest_4412 9d ago

no reason to be that negative as well

-2

u/cave_men 9d ago

how is it negative? Its a simple question

1

u/CorrectGrammarPls 7d ago

Mrr mrrr mrrr mrrr googoo gagaa bubba wubba just a simple question mrrr mrrr googoo

0

u/cave_men 7d ago

Id expect nothing less from you :D

1

u/CorrectGrammarPls 6d ago

Mmmrrr googoo gagaa expect nothing less googoo gagaa mmmuurrrrr

→ More replies

6

u/ChunkyHabeneroSalsa 10d ago

It's a common exercise for a reason. It's a good learning tool.

At least it's not doing backprop by hand on a piece of paper. That was annoying

5

u/curious_corn 10d ago

Why did you waste your time writing this shitty response?

2

u/epelzer 10d ago

Even until more recently you did this in any study that included some subject related to ML as an exercise.

1

u/Individual-Cut-9018 7d ago

Studying math? So overrated... People used to do that stuff 20,000 years ago...

1

u/curiouslyjake 10d ago

That's a wild exaggeration.

3

u/Not-a-throwaway4627 9d ago

Congrats on doing a standard homework assignment for cognitive science students circa 2012

The double irony here is that this actually sets you apart from most ML and data science bullshit artists. Which is most people in those fields.
You’re on track to be good at this, but in the world of people who know what’s up, this is 101 stuff. Keep up the good work

1

u/Necessary_Guest_4412 9d ago

thanks for the feedback

6

u/natsu0529 8d ago

A lot of japanse do that for learning in univercity!
All that pain doing backprop by hand, and then you just use PyTorch anyway lol.
Good for learning though.

3

u/Player_1_LTD 8d ago

Learning first is king. After that spam the easier tool or AI as much as you want. (Some vibe coders do not even know how to step though a problem debugging in an IDE.)

4

u/natsu0529 8d ago • Bravo

Yesss, true.
You can't really build on tech without the fundamentals, right?

3

u/Player_1_LTD 8d ago

1000% and they dont even know what the fundamentals even are!!

2

u/natsu0529 8d ago

who is json

1

u/Player_1_LTD 8d ago

The og Red Power Ranger!? XD

2

u/natsu0529 8d ago

nah, I meant White Chocolate

2

u/Player_1_LTD 8d ago

How about milk chocolate!? 🍫

2

u/AgitatedTumbleweed65 9d ago

Cool, but whenever I see type hints in python I assume it's written by an llm and this is star framing heh.

But if not, then good job 👍

2

u/Necessary_Guest_4412 9d ago

actually I didnt know that I supposed to used the typr hints. but when I asked something about a method to gemini, I saw these type hints, learned what these are and why they are being used. Then I used it all through my project.

5

u/Local-Issue-1740 9d ago

i dont wanna see ur code

0

u/AgitatedTumbleweed65 9d ago edited 9d ago

🤣 honestly I don't see type hints that often in most of the libraries or rather sparse use of them.

But Claude loves them and is really cosisten , which is super helpful. I just think that it is a big hallmark of llm code, especially with personal projects (people are lazy lol)

Edit: most of the smaller libraries + typos

1

u/Local-Issue-1740 8d ago

No u are right i am also to Lazy most of the time xD . I dont know what i was smoking when i wrote that comment

3

u/Intrepid-Self-3578 9d ago

Huh? Dude no they are python coding standards. 

1

u/AgitatedTumbleweed65 9d ago

They are more like good practice

2

u/Emotional_County_688 8d ago

Mi ídolo eres tú, yo estoy estudiando Ciencia de Datos y ni cerca estoy de hacer algo así.

0

u/Necessary_Guest_4412 8d ago

Continúa trabajando!

3

u/Hackerjurassicpark 9d ago

One of the standard DS interview questions in the 2010s was to derive gradient descent and implement it in code. Fun times

3

u/ats678 8d ago

This is a very good way to understand what’s going on under the hood! The next level is to do the same in CUDA to understand how to do it on a GPU :)

1

u/Individual-Cut-9018 7d ago

Incredible work! These are the first steps to big AI

0

u/Necessary_Guest_4412 7d ago

🙏🏻🙏🏻

1

u/Skynse 4d ago

Congrats on the hello world of deep learning

0

u/Intrepid-Self-3578 9d ago

It is great but why not do it in pen in paper it doesn’t make a difference in code or paper? 

Any specific reason for code?

0

u/Necessary_Guest_4412 9d ago

First, I derived all the derivatived and the matrix multiplications, because I wanted to see how one matrix' shape matches with another. 

Then, I implemented the all process with python.

0

u/dritmike 10d ago

So you think that little bump was validation inaccuracy then it realized?

-2

u/Classic-Pubs 10d ago

But why

2

u/I_am_BrokenCog 9d ago

When you're in your fifties, and you wonder why some of your peers "made it", but you didn't ... reflect on the "but why" choices they made which you didn't.

-2

u/Juff567 10d ago

Is this considered hard?

2

u/Necessary_Guest_4412 10d ago

who said its hard?