Snapshot Ensemble with Cyclic Cosine Schedule

Quick Overview

Implement a snapshot ensemble in NumPy: train softmax regression with full-batch gradient descent under a cyclic cosine learning rate, save weights at the end of each cycle, and compare ensemble accuracy with the final snapshot. It tests stable softmax, gradient updates, and when cyclic restarts give diverse members.

Snapshot Ensemble with Cyclic Cosine Schedule

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Train a softmax regression in NumPy with full-batch gradient descent under a cyclic cosine learning rate, saving one weight snapshot at the end of each cycle. At inference, average the snapshots' softmax outputs and report validation accuracy for the ensemble and for the final snapshot alone. Implement these functions: - `cosine_cyclic_lr(n_epochs, n_cycles, lr_max)` returns an array of length `n_epochs` (formula below). Raise `ValueError` unless `n_epochs` is a positive multiple of a positive `n_cycles`. - `softmax(logits)` is row-wise and numerically stable (logits like `1000.0` must not overflow). - `train_snapshot_ensemble(X, y, n_classes, n_epochs, n_cycles, lr_max, l2=0.0)` uses zero init and the update below, appending an independent copy of `(W, b)` after every epoch where `(t + 1) % T == 0`. - `snapshot_ensemble_predict_proba(snapshots, X)` returns the arithmetic mean of each snapshot's `softmax(X @ W + b)`, shape `(n, n_classes)`. - `snapshot_ensemble_accuracy(X_train, y_train, X_val, y_val, n_classes, n_epochs, n_cycles, lr_max, l2=0.0)` returns `(ensemble_acc, final_snapshot_acc)` as Python floats. Argmax ties go to the lowest class. The schedule, the update rule and two examples: ```python # Schedule, T = n_epochs // n_cycles lr[t] = lr_max / 2 * (cos(pi * (t % T) / T) + 1) cosine_cyclic_lr(8, 2, 1.0) # -> [1.0, 0.8536, 0.5, 0.1464, 1.0, 0.8536, 0.5, 0.1464] # Epoch t update (W: d x K zeros, b: K zeros, Y = one-hot(y)) G = (softmax(X @ W + b) - Y) / n W = W - lr[t] * (X.T @ G + l2 * W) b = b - lr[t] * G.sum(axis=0) X = [[1, 0], [0, 1], [1, 1]], y = [0, 1, 1] train_snapshot_ensemble(X, y, 2, 1, 1, 1.0, 0.0) # -> one snapshot: W = [[0, 0], [-1/3, 1/3]], b = [-1/6, 1/6] ``` ```hint Large logits Softmax gives the same output when you add the same constant to every logit in a row. Use that freedom to keep the exponentials in range. ``` ```hint Storing snapshots Check whether your update writes into the existing weight arrays or creates new ones, and what that means for snapshots you appended earlier. ``` ### Constraints & Assumptions - `X` has shape `(n, d)`, `W` has shape `(d, n_classes)` and `b` has length `n_classes`. - Labels are integers in `[0, n_classes)`. - Number of snapshots equals `n_cycles`. - Training is full-batch: every epoch is one gradient step on all `n` rows. ### What a Strong Answer Covers - Builds the schedule from the formula, with the restart at each cycle boundary and the input validation. - Implements a row-wise softmax that stays finite for very large logits. - Follows the given update exactly (zero init, gradient divided by `n`, L2 on `W` only) and saves independent copies at the end of each cycle. - Averages probabilities across snapshots and computes both accuracies with the lowest-index tie rule. - Explains when cyclic restarts produce diverse members and when they do not. ### Follow-up Questions - Why might snapshot ensembling help little for softmax regression? - How does this compare to a deep ensemble for uncertainty estimation? - How would you measure whether the ensemble is better calibrated, and not only more accurate?

Overview: Implement a snapshot ensemble in NumPy: train softmax regression with full-batch gradient descent under a cyclic cosine learning rate, save weights at the end of each cycle, and compare ensemble accuracy with the final snapshot. It tests stable softmax, gradient updates, and when cyclic restarts give diverse members.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Train a softmax regression in NumPy with full-batch gradient descent under a cyclic cosine learning rate, saving one weight snapshot at the end of each cycle. At inference, average the snapshots' softmax outputs and report validation accuracy for the ensemble and for the final snapshot alone.

Implement these functions:

  • cosine_cyclic_lr(n_epochs, n_cycles, lr_max) returns an array of length n_epochs (formula below). Raise ValueError unless n_epochs is a positive multiple of a positive n_cycles .
  • softmax(logits) is row-wise and numerically stable (logits like 1000.0 must not overflow).
  • train_snapshot_ensemble(X, y, n_classes, n_epochs, n_cycles, lr_max, l2=0.0) uses zero init and the update below, appending an independent copy of (W, b) after every epoch where (t + 1) % T == 0 .
  • snapshot_ensemble_predict_proba(snapshots, X) returns the arithmetic mean of each snapshot's softmax(X @ W + b) , shape (n, n_classes) .
  • snapshot_ensemble_accuracy(X_train, y_train, X_val, y_val, n_classes, n_epochs, n_cycles, lr_max, l2=0.0) returns (ensemble_acc, final_snapshot_acc) as Python floats. Argmax ties go to the lowest class.

The schedule, the update rule and two examples:

# Schedule, T = n_epochs // n_cycles
lr[t] = lr_max / 2 * (cos(pi * (t % T) / T) + 1)
cosine_cyclic_lr(8, 2, 1.0)
# -> [1.0, 0.8536, 0.5, 0.1464, 1.0, 0.8536, 0.5, 0.1464]

# Epoch t update (W: d x K zeros, b: K zeros, Y = one-hot(y))
G = (softmax(X @ W + b) - Y) / n
W = W - lr[t] * (X.T @ G + l2 * W)
b = b - lr[t] * G.sum(axis=0)

X = [[1, 0], [0, 1], [1, 1]], y = [0, 1, 1]
train_snapshot_ensemble(X, y, 2, 1, 1, 1.0, 0.0)
# -> one snapshot: W = [[0, 0], [-1/3, 1/3]], b = [-1/6, 1/6]

Constraints & Assumptions

  • X has shape (n, d) , W has shape (d, n_classes) and b has length n_classes .
  • Labels are integers in [0, n_classes) .
  • Number of snapshots equals n_cycles .
  • Training is full-batch: every epoch is one gradient step on all n rows.

What a Strong Answer Covers Guidance

  • Builds the schedule from the formula, with the restart at each cycle boundary and the input validation.
  • Implements a row-wise softmax that stays finite for very large logits.
  • Follows the given update exactly (zero init, gradient divided by n , L2 on W only) and saves independent copies at the end of each cycle.
  • Averages probabilities across snapshots and computes both accuracies with the lowest-index tie rule.
  • Explains when cyclic restarts produce diverse members and when they do not.

Follow-up Questions Guidance

  • Why might snapshot ensembling help little for softmax regression?
  • How does this compare to a deep ensemble for uncertainty estimation?
  • How would you measure whether the ensemble is better calibrated, and not only more accurate?