Checkpoint ensemble exponential

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

Checkpoint ensemble exponential is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Average predictions from each epoch checkpoint with exponentially increasing weights (2^epoch), favoring later more-converged snapshots

How to use this skill

Skill file

Checkpoint Ensemble with Exponential Weights

Overview

Instead of using only the best checkpoint, collect predictions from every epoch and average with exponential weights (2^epoch). Later epochs get more weight since they're more converged, but earlier epochs contribute diversity. Costs zero extra training time — just save predictions at each epoch.

Quick Start

import numpy as np

def checkpoint_ensemble_predict(model, train_fn, test_loader, n_epochs,
                                 device='cuda'):
    """Train model and collect exponentially-weighted checkpoint predictions.
    
    Args:
        model: PyTorch model
        train_fn: function(model, epoch) that trains one epoch
        test_loader: DataLoader for test data
        n_epochs: total training epochs
    Returns:
        averaged predictions array
    """
    all_preds = []
    weights = [2 ** epoch for epoch in range(n_epochs)]
    
    for epoch in range(n_epochs):
        train_fn(model, epoch)
        
        model.eval()
        epoch_preds = []
        with torch.no_grad():
            for x_batch in test_loader:
                logits = model(x_batch.to(device))
                epoch_preds.append(torch.sigmoid(logits).cpu().numpy())
        all_preds.append(np.concatenate(epoch_preds))
    
    return np.average(all_preds, weights=weights, axis=0)

# Usage
predictions = checkpoint_ensemble_predict(model, train_one_epoch, test_loader, 5)

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.