Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | AdamW Stationary Points and the Margin Equilibrium log(1/eps)AdamW Stationary Points and the Margin Equilibrium log(1/eps)
AdamW Stationary Points and the Margin Equilibrium log(1/eps)
MediumDeep Learning
Example:
Input:
```python
import numpy as np
g = lambda th: -np.exp(-th)
for eps in (1e-8, 1e-12, 1e-16):
th = adamw_fixed_point(g, 0.02, eps)
print(round(th, 2), round(equilibrium_margin(eps), 2), bool(abs(th - equilibrium_margin(eps)) < 1.2))
```
For g(theta) = -exp(-theta) the stationary condition reads exp(-theta)/(exp(-theta) + eps) = lam * theta. With lam = 0.02 and eps = 1e-8 the solution is 18.95, within half a unit of log(1/eps) = 18.42; at eps = 1e-12 and 1e-16 the fixed point tracks log(1/eps) = 27.63 and 36.84 the same way, so eps alone sets the scale.
A Fix for Softmax and Cross-Entropy asks where training with AdamW comes to rest after a network has memorised its data. For a scalar parameter θ with gradient g(θ), the update θ←θ−η(m^/(v^+ϵ)+λθ) is stationary when the gradient is steady (m^=g, v^=g2):
∣g(θ)∣+ϵg(θ)+λθ=0
The learning rate and both decay rates have cancelled: the stationary point depends on λ and ϵ only. Implement a solver for it and the margin consequence.
adamw_fixed_point(g_of_theta, lam, eps)
g_of_theta is a Python callable returning the gradient at a scalar theta; lam > 0 and eps >= 0.
The normalised gradient has magnitude below 1, so the solution lies in [−1/λ,1/λ] and the left-hand side above is negative at the left end and positive at the right end. Run bisection on exactly that bracket for 100 halvings, keeping the half on which the sign changes, and return the final midpoint as a Python float. Assume the sign changes once.
equilibrium_margin(eps)
Return log(1/ϵ), the margin at which a memorised example's gradient ∣g∣≈e−m balances weight decay under Adam's normalisation.
cliff_crossed(eps, dtype)
dtype is one of "bf16", "fp32", "fp64". Use the paper's precision cliffs c16=5.5, c32=16.6, c64=36.7 (the margin beyond which 1/(1+e−m) rounds to exactly 1 in that format). Return True when equilibrium_margin(eps) exceeds the cliff, i.e. when standard cross-entropy in that precision must cross the cliff before AdamW can settle.
Hints
Evaluate the sign at the left end once and compare the midpoint's sign with it; np.sign of a product also works.
A tiny ϵ makes g/(∣g∣+ϵ) behave like a sign function except in a sliver around g=0, which is where the solution has to be when λ∣θ∣<1.
For g(θ)=−e−θ the equation becomes e−θ/(e−θ+ϵ)=λθ; solve it in your head for λθ=1/2.