r/learnmachinelearning • u/Alt_account_6788 • 4d ago
learning to make models from scratch, anything else to try and learn?
As the title says, I've been learning ML by making the code for the models myself, no modules or existing code.
I have successfully created my own modules storing the code for multiple feature linear and logistic regression. I have also managed to get forward propagation working for neural nets but not back propagation.
What should I learn from here?
34
Upvotes
7
u/STEM-Mentor 4d ago •
I have a different opinion here. Learning to code the math logic from scratch forces you to understand the core algorithms better. However, I personally feel that's not the best use of your time, at least at this stage.
I think it will be more optimal for you to follow some of these steps.
* For a given type of ML model (e.g. xgboost, random forest, neural network) learn how to use well established Python packages for common tasks (e.g. train/test split, data cleaning, missing value handling, building the model, run it on test data, draw ROC or PR curves etc etc). These tasks you should not attempt to code from scratch. Your own code for such standard tasks may be buggy, you may not take care of all edge cases, may spend unreasonable amount of time to debug your code. So you should first learn how to effectively use existing tools.
* Once you have mastered the standard ML modeling pipelines, you may try to code parts of it, purely for improving your understanding of the algorithms (e.g. when it fails, how to make it run faster etc.). Majority of AI/ML jobs in industry need you to build, evaluate, debug models. Hence learn that first and learn it very well. First spend time learning to fit most of the common types of models (regression, classification, ensemble models etc.)
* As an example, Andrej Karpathy, the renowned scientist built basic, no-frills version of LLM and showed it to his audience. This shows us what goes on inside a LLM. But for our day-to-day work, we do use off-the-shelf LLMs like ChatGPT and often that's what our day job demands.
6
u/d0pe-asaurus 4d ago
Get the backprop working for neural nets and take it out for a spin with a 784x120x10 fully connected network for MNIST
2
u/YousifAbdulhussein 4d ago
If you’re doing things from scratch, check out imparteducation.com since it has free courses on how to build stuff from scratch with code and everything
1
u/Massive_Distance_616 3d ago
On backprop specifically, since that's the part that's actually broken, not just unlearned: the usual place people get stuck is the chain rule through the activation function. Forward prop only needs the activation's output, but backprop needs its derivative at each layer, multiplied into the gradient coming from the layer after it. If you're using sigmoid or tanh, that derivative is easy to get backwards or forget entirely, and the model will train but converge to nonsense or not move at all.
A good way to isolate it: implement a single hidden layer first with a tiny toy dataset (even 4 points you can compute by hand), and check your gradients numerically. Perturb one weight by a small epsilon, see how much the loss changes, and compare that to what your backprop code computed for that weight's gradient. If they don't match to a few decimal places, the bug is in that specific layer's gradient calculation, and you can narrow it down layer by layer rather than guessing.
Once backprop works, the natural next steps from scratch are usually: a proper train/test split with gradient descent variants (momentum, then Adam), then convolutional layers if you want to go toward vision, or just a deeper MLP with regularization (L2, dropout) since those are cheap to add once backprop itself is solid.
1
u/Every_Principle_3637 3d ago
I got stuck on the sigmoid derivative too, turned out I was missing the gradient scaling from the next layer. try logging the shape of each gradient step to catch mismatches.
1
u/SweetYalla 2d ago
I did focus on backpropagation next, then try building a small neural network and training it on a real dataset. Implementing gradient descent and optimizers from scratch would also be great practice.
1
u/wasl-app 2d ago
hey , for me i may have a different approach than the code it self , i would start with the ml math it self , linear algebra mostly , it is a completely different approach to code i understand , but it will make see ml in another prospective