pythonadvanced

Weights & Biases Hyperparameter Sweeps

Run automated hyperparameter search with W&B Sweeps using Bayesian optimization.

python
import wandb
import random

sweet_config = {
    'method': 'bayes',
    'metric': {'goal': 'maximize', 'name': 'accuracy'},
    'parameters': {
        'learning_rate': {'min': 0.0001, 'max': 0.1},
        'batch_size':    {'values': [16, 32, 64, 128]},
        'n_layers':      {'values': [2, 4, 6]},
    },
}

def train_model():
    with wandb.init() as run:
        cfg = run.config
        # Simulate training
        accuracy = 0.7 + 0.1 * random.random() + 0.05 * (cfg.learning_rate < 0.01)
        wandb.log({'accuracy': accuracy, 'loss': 1 - accuracy})

sweep_id = wandb.sweep(sweet_config, project='sweep-demo')
wandb.agent(sweep_id, train_model, count=10)

Use Cases

  • hyperparameter tuning
  • AutoML
  • experiment automation

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.