Tag: Optim

  • DL0069 RMSProp Adaptive Learning Rates

    How does RMSProp adapt the learning rate per parameter?

    Whiteboard question card with three panels: a training config showing RMSprop with one shared learning rate of 0.001, a gradient log table where w_dense gets a gradient near 2.0 every step while w_rare gets 0.3 only on steps 3 and 7, and two empty boxes asking for each parameter's next step size.

    The Problem: A recommender team trains with RMSProp at one global learning rate of 0.001. The dense output weight w_dense sees a gradient near 2.0 on every step, while a rarely seen item’s embedding row w_rare gets a gradient of 0.3 only every few steps. With one shared lr, how does each parameter end up with its own step size?

    Answer

    RMSProp keeps a per-parameter exponential moving average of squared gradients and divides each gradient component by the square root of that average before stepping, so the effective learning rate shrinks for parameters with historically large gradients and grows for those with small or sparse ones. The running estimate v_t acts as a per-coordinate normalizer: two parameters share one global learning rate yet move by very different amounts. On ill-conditioned surfaces, where curvature differs wildly across directions, this damps oscillation along steep directions while sustaining progress along shallow ones, something a single global learning rate cannot do. Tieleman and Hinton introduced the method in their 2012 Coursera lecture series to cope with non-stationary objectives such as mini-batch and recurrent training; Adam is essentially RMSProp plus a first-moment momentum term and bias correction.

    (1) Per-Coordinate Normalization: each parameter steps by \alpha\, g_t / (\sqrt{v_t} + \epsilon); a parameter whose gradients run 10x larger builds a 10x larger \sqrt{v_t}, so its effective learning rate shrinks 10x and the two coordinates end up moving by comparable amounts instead of one dwarfing the other.
    (2) Memory With a Window: the decay \rho (0.9 in the original lecture; PyTorch’s RMSprop defaults to 0.99) makes v_t an average over recent history, so the normalization adapts as the landscape changes instead of stalling like AdaGrad’s monotonically growing accumulator.
    (3) No Bias Correction: v_t starts at zero and is never corrected, so early steps are oversized; Adam fixes this with \hat{v}_t = v_t / (1 - \rho^t), one of the two additions (the other is momentum) that turn RMSProp into Adam.

    Contour plot of the ravine f(x,y) = 0.05x^2 + 2y^2 with two 42-step trajectories from (-10, 4): the red SGD path zigzags across the steep y direction with decaying overshoots while creeping along x to about -1.5, and the blue RMSProp path settles into the valley without oscillating and travels along it to the minimum

    Figure 1: On the ravine f(x,y) = 0.05x^2 + 2y^2, SGD spends its budget oscillating across the steep y direction and after 42 steps is still at x \approx -1.5; RMSProp normalizes both coordinates, never overshoots, and reaches x \approx -0.3 in the same 42 steps.

    The same normalization explains RMSProp’s strength on sparse features. For an embedding row that receives a gradient only occasionally, v_t decays toward zero between updates, so when a gradient finally arrives its effective learning rate \alpha / \sqrt{v_t} is several times larger than a densely-updated parameter’s, and the rare signal is not drowned out by a global rate tuned for frequent gradients. The flip side appears at the start of every run: with \rho = 0.99 the first estimate is v_1 = 0.01\, g_1^2, so the first step has magnitude 10\,\alpha whatever the gradient scale, a warmup-like quirk that Adam’s bias correction removes. In practice RMSProp remains a solid choice for RNNs and other non-stationary objectives, while vision recipes still often prefer well-tuned SGD with momentum for final generalization.

    Two stacked panels over 60 training steps: top shows sqrt of v_t rising from 0.2 toward its asymptote of 2 for a parameter with large steady gradient while staying near 0.3 for a sparse-gradient parameter; bottom shows the effective learning rate alpha over sqrt(v_t) falling from 5 to 0.74 for the first parameter while the second stays near 2.9 in a sawtooth pattern

    Figure 2: Two parameters over 60 steps with \rho = 0.99: the one with a large steady gradient sees its effective learning rate \alpha / \sqrt{v_t} fall 6.7x by step 60 (heading for \alpha/2 as \sqrt{v_t} \to |g|), while the sparse-gradient parameter still enjoys a roughly 4x larger rate.

    Mathematical Formulation:
    v_t = \rho\, v_{t-1} + (1 - \rho)\, g_t^2
    \theta_{t+1} = \theta_t - \frac{\alpha\, g_t}{\sqrt{v_t} + \epsilon}

    Where:

    • g_t = \nabla f_t(\theta_t) is the minibatch gradient at step t; the squaring in the accumulator is element-wise.
    • v_t is the per-parameter running average of squared gradients, with the same shape as \theta, initialized to zero.
    • \rho is the decay (0.9 in Hinton’s lecture, 0.99 by default in PyTorch), \alpha the global learning rate, and \epsilon \approx 10^{-8} a numerical floor guarding the division.
    • All operations are element-wise, so each parameter gets its own effective rate \alpha / (\sqrt{v_t} + \epsilon) under one shared \alpha.
    AspectSGD + MomentumRMSPropAdam
    State per ParameterVelocity m (first moment)Squared-gradient average v (second moment)Both m and v
    NormalizationNone; one global rate for all coordinatesGradient divided by sqrt(v) per coordinateBias-corrected m divided by sqrt(v-hat)
    Early StepsStable from step oneOversized; v underestimated with no correctionBias correction keeps steps near alpha scale
    Typical StrengthFinal test accuracy on tuned vision recipesRNNs and non-stationary objectivesDefault for transformers and general use

    Login to view more content
  • ML0096 Maximum Likelihood Estimation

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0088 SVM Scaling

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0083 Weight Decay

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0082 XGBoost Improves Gradient Boosting

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0072 Bayesian Optimization

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • DL0048 Adam Optimizer

    Can you explain how the Adam optimizer works?

    Whiteboard question card with two panels. The left panel shows a code-review diff that replaces SGD with lr 0.1 by Adam with lr 0.001, betas 0.9 and 0.999, and eps 1e-8. The right panel shows a 12-step gradient log in which weight A has small, steady gradients and weight B has large gradients that flip sign every step.

    The Problem: In a code review, a team swaps plain SGD for Adam on their text classifier with a one-line config change: lr = 0.001, betas = (0.9, 0.999), eps = 1e-8. Their gradient logs show weight A (a rare-word embedding) getting small but consistent gradients, while weight B gets large gradients that flip sign every step, and both share a single learning rate.

    Answer

    Adam (Adaptive Moment Estimation) combines momentum and RMSprop: it keeps an exponentially decaying average of the gradient (first moment, the direction) and of the squared gradient (second moment, the scale), then divides the former by the square root of the latter. The result is a per-parameter adaptive learning rate: large steps for parameters with small, consistent gradients, small steps for noisy or steep ones, plus bias correction that fixes the zero-initialization of both averages in early steps.

    (1) First Moment (Momentum): m_t = \beta_1 m_{t-1} + (1-\beta_1) g_t smooths the gradient direction over time.
    (2) Second Moment (RMSprop): v_t = \beta_2 v_{t-1} + (1-\beta_2) g_t^2 tracks the gradient magnitude for per-parameter scaling.
    (3) Bias Correction + Update: \hat{m}_t = m_t/(1-\beta_1^t), \hat{v}_t = v_t/(1-\beta_2^t) remove the zero-init bias before the normalized step.

    Mathematical Formulation:
    \theta_t = \theta_{t-1} - \alpha\, \frac{\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}

    Where:

    • \theta_t are the model parameters at step t, and g_t is the gradient of the loss at that step.
    • \alpha is the learning rate (default 0.001); \epsilon \approx 10^{-8} prevents division by zero.
    • \beta_1 = 0.9 and \beta_2 = 0.999 control the decay of the first and second moment averages (paper defaults).
    Contour plot of a quadratic bowl with the Adam optimizer path moving from a start point in the corner along adaptive steps that converge smoothly to the minimum at the origin.

    Figure 1: Adam on a quadratic bowl: adaptive per-parameter steps converge smoothly to the minimum without the zig-zag of plain SGD.

    Intuition for the Division: \hat{m}_t / \sqrt{\hat{v}_t} is roughly a signal-to-noise ratio: parameters whose gradients are large but inconsistent (high v) get small updates, while parameters with small but persistent gradients get amplified. This is what makes Adam robust to ill-scaled features and sparse gradients.


    Login to view more content
  • ML0057 K-means

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • DL0018 NaN Values

    This is an IntvML Pro interview question. Upgrade to Pro to unlock the full prompt, diagrams, and solution.

  • ML0047 Parameters

    What are the differences between parameters and hyperparameters?

    Two dashed panels from one image-classifier project: a config.yaml file listing learning rate, batch size, epochs, number of layers and dropout, and a model_checkpoint.pt file listing weight and bias tensors with sample values. Below them, a question asks how the two kinds of numbers differ.

    The Problem: Two files sit side by side in the same image-classifier project. The first is a short config.yaml with a handful of settings. The second is a model checkpoint holding over a million numeric values. Both contain numbers that shape the final model, so what actually distinguishes them?

    Answer

    Parameters are the values a model learns from its training data, while hyperparameters are settings defined by the user that guide the training process and model architecture. Parameters represent the model’s internal knowledge and decision rules; hyperparameters govern how the model learns, influencing its architecture, training dynamics, and ultimately its ability to generalize to unseen data: choosing them well is crucial for building an effective model.

    (1) Parameters: Internal variables learned from data (e.g., weights and biases); adjusted during training by the optimization algorithm; capture the model’s learned patterns and information.
    (2) Hyperparameters: External configurations set before training (e.g., learning rate, batch size, number of layers); remain fixed during training and are not updated by the learning process; influence how the model learns and its overall structure.
    (3) Who Updates Whom: The optimizer updates parameters by minimizing the loss; the practitioner (or a search procedure) updates hyperparameters by watching validation performance.

    Diagram contrasting parameters updated inside the training loop with hyperparameters set outside before training begins

    Figure 1: The two knobs of a model: parameters (weights, biases) live inside the training loop and are updated every step by the optimizer; hyperparameters (learning rate, batch size, depth, …) are set before training and shape how that loop behaves.

    Mathematical Formulation:
    \theta_{t+1} = \theta_t - \eta \, \nabla_\theta \mathcal{L}(\theta_t)
    \lambda^* = \arg\min_{\lambda} \; \mathcal{L}_{\text{val}}\big(\hat{\theta}(\lambda)\big)

    Where:

    • \theta denotes the parameters (weights and biases), updated every training step t by gradient descent.
    • \eta is the learning rate, itself a hyperparameter, set before training and never learned by the optimizer.
    • \lambda stands for hyperparameters generally; \hat{\theta}(\lambda) is the fully trained model under that setting, and the outer minimization over validation loss is hyperparameter search.

    Login to view more content