Modern Optimizers & Normalization Mechanics
AdamW decoupled weight decay, exponential moving averages, and RMSNorm from scratch.
Reference notes. Condensed summaries of each topic, not step-by-step lessons. The Core Course is the one to work through.
1. The idea
Modern deep learning training relies on adaptive gradient algorithms like AdamW and scale-invariant normalization layers like RMSNorm to accelerate convergence and stabilize optimization.
2. The math
3. How it works
4. The code (python)
import numpy as np
class AdamWScratch:
"""AdamW optimizer with decoupled weight decay implemented in pure Python."""
def __init__(self, params, lr=1e-3, beta1=0.9, beta2=0.999, eps=1e-8, weight_decay=0.01):
self.params = params
self.lr = lr
self.beta1 = beta1
self.beta2 = beta2
self.eps = eps
self.weight_decay = weight_decay
self.m = {p: np.zeros_like(v) for p, v in params.items()}
self.v = {p: np.zeros_like(v) for p, v in params.items()}
self.t = 0
def step(self, grads):
self.t += 1
for p in self.params:
g = grads[p]
self.m[p] = self.beta1 * self.m[p] + (1 - self.beta1) * g
self.v[p] = self.beta2 * self.v[p] + (1 - self.beta2) * (g ** 2)
m_hat = self.m[p] / (1 - self.beta1 ** self.t)
v_hat = self.v[p] / (1 - self.beta2 ** self.t)
self.params[p] -= self.lr * (m_hat / (np.sqrt(v_hat) + self.eps) + self.weight_decay * self.params[p])5. Check yourself
In the catalog
Finished this chapter?
The Epoch Zero newsletter
What changed in ML architectures this week: new models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.