Snapshot cosine ensemble

From wenmin-wu/ds-skills · View interactive version

Snapshot cosine ensemble is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Cyclic cosine annealing LR that produces M diverse snapshots from a single training run for free ensembling

How to use this skill

Skill file

Overview

Snapshot ensembling runs cosine LR cycles during a single training pass. Each cycle converges to a different minimum, giving M diverse models for the cost of training one. Combined with SWA or simple prediction averaging, this provides cheap ensemble diversity without M training runs.

Quick Start

import numpy as np
from keras.callbacks import LearningRateScheduler, ModelCheckpoint

class SnapshotCallbackBuilder:
    def __init__(self, nb_epochs, nb_snapshots, init_lr=0.1):
        self.T = nb_epochs
        self.M = nb_snapshots
        self.alpha_zero = init_lr

    def _cosine_anneal(self, epoch):
        cos_inner = np.pi * (epoch % (self.T // self.M)) / (self.T // self.M)
        return float(self.alpha_zero / 2 * (np.cos(cos_inner) + 1))

    def get_callbacks(self, model_prefix='snap'):
        return [
            ModelCheckpoint(f'{model_prefix}_best.h5', monitor='val_loss',
                            save_best_only=True),
            LearningRateScheduler(self._cosine_anneal),
        ]

# Usage: 50 epochs, 5 snapshots, init_lr=1e-3
builder = SnapshotCallbackBuilder(nb_epochs=50, nb_snapshots=5, init_lr=1e-3)
model.fit(..., epochs=50, callbacks=builder.get_callbacks('run1'))

Workflow

  1. Choose total epochs T and number of snapshots M (cycle length = T/M)
  2. LR follows cosine schedule within each cycle, restarting to init_lr at cycle start
  3. Save model weights at the end of each cycle (when LR hits minimum)
  4. At inference, average predictions from all M snapshots
  5. Optionally combine with SWA by averaging weights of the final few snapshots

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.