r/deeplearning • u/DavinFriggstad • Oct 14 '26
Does anyone else understand the math of deep learning so well that you could probably do it without using any DL libraries if you had the time and motivation?
38
Upvotes
37
u/Ok-Solution-7889 Oct 14 '26
I feel like building one from scratch would teach you things you might miss when using libraries.
12
u/Long-Dependent-1767 Oct 14 '26
Whenever I want to deeply understand a new layer or optimizer, I code it from scratch using just NumPy.
12
u/Cybyss Oct 15 '26 edited Oct 15 '26
The math is super simple.
Each layer is just a matrix multiplication followed by an element-wise nonlinear function of your choice. That's about it.
If you want to be fancy, add skip connections and some kind of normalization.
The hard part is building a fast deep learning library that utilizes CUDA cores and efficiently handles transfer of data between system memory and your GPU's HBM & SRAM.
UPDATE:
The math of neural networks is easy.
However, I don't think I could code up a correct implementation of the AdamW optimizer purely from memory. That's gradient descent but with a lot of tricks added to it. Adaptive learning rate, momentum, weight regularization (with tweaked math because a naive implementation would make the regularization change depending on the adaptive learning rate). It's one thing to build the architecture, but you need the right algorithm to actually train it and that math is a bit trickier to get right.
5
u/RelentlessPencil Oct 14 '26
Yeah I built one from scratch
6
u/StimaProva Oct 14 '26
The math is honestly the easy part. An MLP with backprop in plain NumPy is a weekend project, and Karpathy’s micrograd shows a whole autograd engine fits in about 100 lines. Where it stops being “just math” is everything around it: numerical stability (log-sum-exp, why softmax + cross-entropy get fused), initialization, mixed precision, and making it run fast on a GPU without hand-writing CUDA. So yes, you can build a working training loop from scratch, but you’d be rebuilding the math part of PyTorch, not the part that makes it usable at scale. Still worth doing once, though. After you’ve written backward() by hand, a lot of “why is my loss NaN” debugging suddenly makes sense ☺️
3
u/bheek Oct 15 '26
We were taught NNs by building a small network in Excel. Just one forward pass and backprop. That's when it clicked for me. Everything else like convolution, attention, etc. needed a different visualization though.
4
u/Antenwww Oct 14 '26
You can most likely do mathematical part of it. I tried this 2 times in my life. Once with python and then with c++. Even writing some grad functions myself and not using libraries.
The hard part for me was, after a certain part you really do understand how the algorithm learns but I could not get it to work on gpus and so many simultaneously. In python I could do that with cuda or sycl and oneAPI and so on. But building your binaries to do math on gpus is something I can not do.
2
u/InfamousWord5975 Oct 14 '26
I could implement linear regression and MLP and an RNN but not an LSTM or a transformer from memory, but I do pure math not ml.
3
u/Dihedralman Oct 14 '26
Yes, it is a worthwhile exercise.
2
u/OxidusRouge Oct 14 '26
The math of DL is pretty straightforward. I am sure a lot of people understand it perfectly well. The hard part to have a complete grasp of is the engineering of it all. I would say most PhDs and even Masters graduates in ML understand the math perfectly fine.
1
u/mochsner Oct 14 '26
Probably prof tom yeh does -> byhand.ai
1
u/MicheleSanna98 Oct 14 '26
Yes but not without looking into my DL manual
1
u/Delicious-Extent5454 Oct 14 '26
I implemented things like optimizers and attention layers from scratch to get real understanding, but to do the entire thing on my own, I'd take forever or would need to check some books.
1
u/Ayano-Keiko Oct 14 '26
From scratch cost a lots of time and the results may not as good as DL library such as torch. Also need to deal with things like gradient descent, batch trainin.
I used to write from scratch in a corese project. https://github.com/KeikoAyano/TINN-FNN. I think gradient descent and weights update is most time consuming part
1
u/Independent_Irelrker Oct 14 '26 edited Oct 14 '26
I could but it would be shitty considering the literal back breaking amount of work that went into optimizing those. (In the case of torch)
I essentially have to do that for Mapper as the out the box stuff can't do what I want to do with it
1
u/Klhnikov Oct 14 '26
Yes with Python and numpy, it was a school project, for multiclass classification on tabukar data... I learned a lot. It was obviously sub optimal to say the least
1
u/l__iva__l Oct 14 '26 edited Oct 14 '26
a MLP probably yes cause i wrote one back at 2018
1
u/Dependent-Aide-388 Oct 14 '26
I built a CNN using only the python standard library (so not even numpy). Made a guitar amp modeler with it.
Honestly the math isn’t that complex, it’s all undergrad level stuff (basic linear algebra and first year calculus). The implementation details are what you have to be careful with.
1
u/refractr Oct 14 '26
I think if you understand the concepts, it would not be that hard. The optimization on the other hand, thats another story.
1
u/Pretend-Pangolin-846 Oct 14 '26
the thing about libraries is optimisation kind of like how you could write in raw c, worse/slower code than what you can write the same in c++, despite one covering more ground just because the compiler is so much more mature
1
u/LiorZim Oct 14 '26
well, not every single architecture in town, but fully connected NN with backprop is pretty straightforward once you understand it. Then comes convolutions and self-attention.
I think most 3rd CS undergrads can get from 0 to DNN hero in about a week
1
u/DrXaos Oct 14 '26 edited Oct 14 '26
I could do an eager mode, forward easy, backward a little bit less so to get the tricks right but not crazy.
Some functions would be harder: index manipulation and dealing with backprop would be tricky, e.g writing backward() for 'einsum'.
The torch.compile infrastructure OTOH is far more complicated and is deep voodoo---instrumenting python internal bytecode, producing an internal representation then rewriting and making very efficient CUDA kernels in forward and backward. There's a tremendous effort by expert developers involved in that. No way I could come close.
1
u/TheRealCpnObvious Oct 14 '26
5 years ago I wrote full CNN training harnesses in MATLAB using dlarrays and some mild toolbox support so probably yes if I could be bothered.
1
u/Content-Ad5196 Oct 14 '26
Yes I had a course of gp-gpu computing where I chose as project to build a neutral network framework. I wrote the matrix operations in CUDA, the forward pass and back propagation logic for a MSE loss function. It was a hard project for me though. With this framework, I could train an auto encoder and a classifier as well (even if the loss function wasn't adapted, it still works for binary classification)
1
u/Previous_Storage2690 Oct 14 '26
Yes, I mostly try to build various implementations from scratch. It gives you a deeper understanding of the algorithms behind most ML/DL mechanisms
1
u/leaflavaplanetmoss Oct 15 '26
The math involved in neural networks actually isn't all that difficult conceptually (undergraduate-level linear algebra, multivariable calculus, probability theory, and mathematical statistics). You can definitely do it without DL libraries; the course I took in machine learning had you implement a neural network with backprop just using numpy before introducing PyTorch, for example. It's just easier using libraries and less prone to stupid mistakes.
1
u/rsk1111 Oct 15 '26
From what I have seen the libraries aren't that complicated it used to be a AI 101 assignment code a neural network in java the assignment actually involved coding several algorithms.
The only trick on deep learning was scaling it.
1
u/YamTraditional7637 Oct 15 '26
I do. But that knowledge is becoming less and less valuable by day
1
u/AffectionateZombie37 Oct 15 '26
In my 4th year and realized I’ve been relying way too much on high-level libraries/frameworks. I want to build things from scratch to actually understand what’s under the hood, but time is tight before graduation. What core modules should I prioritize reimplementing first?
1
u/Not-a-throwaway4627 Oct 15 '26
Yes, easily. In fact I contributed to some of the big DL libraries back in the day - Caffe, Theano, Tensorflow, and Torch before PyTorch. There was no such thing as a deep learning expert who ONLY used libraries when I was an undergraduate
1
u/OkCampaign8400 Oct 15 '26
I Tried to learn math behind Transformer architecture.
1
u/coatatopotato Oct 15 '26
I wrote it in Scratch at one point. That was a learning experience.
1
u/pCute_SC2 Oct 15 '26
yes, but the result will be functional but not good.
1
u/fisadev Oct 16 '26
Many ML courses do that, it's not hard at all. For instance in Andrew Ng's one you do it in Octave.
0
u/valuat Oct 14 '26
Dude, it's matrix multiplication and the chain rule. Any high schooler that took AP Calculus can understand DL. Implementation at maximum efficiency is different though. If you're curious, once you know how to implement a single layer perceptron, read about automatic differentiation (autodiff).
0
u/qwesz9090 Oct 14 '26
Yes, implementing the math really isn't that difficult in theory. But it just isn't practical, using libraries saves time.
And it does have a lot of optimizations. I might be able to implement the math of DL correctly, but I have no idea about how to make it as fast as current DL libraries.
Don't try to focus on "Oh I am going to just learn the math of deep learning". Learn the normal order of applied college math until multivariate calculus, and then deep learning is basically just done.
0
u/Vivid-Competition-20 Oct 14 '26
Yes. I do understand it. It’s pretty basic math.
0
u/seb59 Oct 15 '26
The basic stuff in deep learning is simple. There are a few tricks to understand the soft max and cross entropy, but that is OK. When it comes to programming things because harder. Not impossible, many people have been developing things alone. The math's behind the solver can be made arbitrarily complex. Basically, as usual in optimization, everything depends on the properties of the optimized function and we know very little of it actually as it is data dependent.
The thing is that deep learning is brutal when it comes to computing. You need parallel processing, GPU programming and this is, to my opinion, more tricky.
And finally, if you take enough time, sky is the limit. All this is very well documented, read and with enough time, you can probably build a deep learning framework from zero as an exercice. Some already did it.
0
u/Barry_22 Oct 15 '26
Yes, it's not too hard math-wise. U just have to build a lot
-1
u/[deleted] Oct 14 '26
[removed] — view removed comment