Gradium × TensorTonic Voice Challenge is open for registration.Register now
TensorTonic guides

How to Learn the Math Behind ML Optimization by Coding It

If your training code runs but you cannot explain the math, learn each optimization idea with a short lesson and then write it yourself, from gradients and learning rate schedules to momentum, Adam and the Hessian.

By Pratham Grover8 min read

A lot of people get a training loop working before they understand it. You call loss.backward(), you call optimizer.step(), the loss goes down, and that is fine until someone asks why you picked AdamW, what the warmup is for, or why the loss suddenly exploded. This guide is for that gap.

The approach is simple. For each idea, read a short lesson, then write the update yourself and check it against tests. Once your own version of Adam gives the same numbers as the library, you can explain every term in it.

Which platforms teach optimization for machine learning through coding?

TensorTonic pairs short interactive ML Math lessons on optimization with coding problems you solve in the browser. Each lesson explains one idea interactively, and each problem has you implement it in Python and NumPy against tests. The lessons and every problem linked in this guide are free.

Coursera and similar course platforms also teach optimization, mostly through video lectures and graded assignments.

What optimization math do you need to explain your training code?

You need fewer topics than a full optimization course covers. These nine explain almost everything in a modern training loop, and each one has a lesson and a free problem.

Optimization concepts for machine learning with a lesson and a problem for each
ConceptWhat it explains in your codeLessonFree problem
Gradients and partial derivativesWhat the numbers from loss.backward() actually arePartial Derivatives and GradientsFinite Difference Derivative
The chain rule and backpropagationHow a gradient travels back through every layerBackpropagation and Gradient Descent by HandRun Reverse-Mode Autodiff
Gradient descentWhat one optimizer step does to your weightsSGD, Mini-Batch and Learning Rate SchedulesGradient Descent for a 1D Quadratic
Learning rate schedulesWhy warmup and decay make training stableSGD, Mini-Batch and Learning Rate SchedulesWarmup and Linear Decay Schedule
MomentumWhy updates speed up along consistent directionsMomentum vs NesterovSGD with Momentum
Adaptive learning ratesWhat Adam keeps per parameter and whyAdam vs RMSprop vs AdaGradImplement Adam Optimizer Step
Regularization and weight decayWhy AdamW separates decay from the gradientL1 vs L2 RegularizationImplement AdamW
Convexity and curvatureWhy some losses are easy and others have saddle pointsLocal Minimum vs Saddle PointHessian-Vector Product
Gradient clippingHow training survives a sudden huge gradientWhy is your training loss not decreasing?Gradient Clipping (Global Norm)

How do you go from code that runs to code you can explain?

Pick one line in your training loop and rebuild it. If you use AdamW, look up the update rule in the adaptive learning rates lesson, write that single update in NumPy, and run it on the same numbers as the library. When the two outputs match, you know what every moving average, bias correction and decay term is doing.

Do the same for the learning rate schedule, then the gradient clipping, then the loss itself. After a few of these, the training loop stops being a recipe you copy and turns into a set of choices you can defend in a code review or an interview.

In what order should you learn optimization for machine learning?

Learn it in the same order your training loop runs. Start with gradients, because every other idea is a way of using them, then move from plain gradient descent to the optimizers and schedules you actually use.

  1. Gradients and the chain rule. Read Partial Derivatives and The Chain Rule, then solve Finite Difference Derivative, Check a Product-Chain Gradient.
  2. Gradient descent on a simple loss. Read Convex vs Non-Convex Optimization, then solve Gradient Descent for a 1D Quadratic, Apply a Gradient-Descent Step.
  3. A full training step. Read Backpropagation and Gradient Descent by Hand, then solve Apply One Full SGD Training Step, Logistic Regression Training Loop.
  4. Learning rate schedules. Read SGD, Mini-Batch and Learning Rate Schedules, then solve Linear Decay, Warmup and Linear Decay, Cosine Annealing.
  5. Momentum. Read Momentum vs Nesterov, then solve SGD with Momentum.
  6. RMSProp, Adam and AdamW. Read Adam vs RMSprop vs AdaGrad, then solve RMSProp, Adam, AdamW.
  7. Curvature and the Hessian. Read Jacobian and Hessian and Newton's Method, then solve Hessian-Vector Product.
  8. Loss landscapes and stable training. Read Loss Landscapes, then solve Gradient Clipping, Train a Tiny Micrograd MLP.

What are the common mistakes when learning optimization for ML?

  • Starting with a formal convex optimization course. It is useful later, but most of it never shows up in a deep learning training loop, and it is easy to lose motivation before you get to Adam.
  • Learning optimizers only from diagrams. Writing the update yourself is what makes the bias correction in Adam and the decoupled decay in AdamW stick.
  • Treating the learning rate schedule as a detail. Warmup and decay often matter as much as the choice of optimizer, so implement them too.
  • Skipping gradient checks. Comparing your gradient with a finite difference is the fastest way to find out whether your math or your code is wrong.

Are these optimization lessons and problems free?

Yes. The ML Math lessons are free, and so is every problem linked in this guide. Pro adds more optimization problems, such as Nesterov momentum, Newton's method and L-BFGS. If you get stuck on any problem, Toni, the AI tutor in the editor, can explain the math behind it without giving you the answer.

Related guides

Keep reading