Intermediate

W&B Sweeps

Automate hyperparameter search using Bayesian optimization, grid search, or random search - distributed across multiple machines.

Sweep Configuration

Python - Define a sweep config
sweep_config = {
    "method": "bayes",        # bayes, grid, or random
    "metric": {
        "name": "val/accuracy",
        "goal": "maximize"
    },
    "parameters": {
        "learning_rate": {
            "distribution": "log_uniform_values",
            "min": 1e-5,
            "max": 1e-1
        },
        "batch_size": {
            "values": [16, 32, 64, 128]
        },
        "epochs": {
            "value": 50             # fixed value
        },
        "dropout": {
            "distribution": "uniform",
            "min": 0.1,
            "max": 0.5
        },
        "optimizer": {
            "values": ["adam", "sgd", "adamw"]
        },
        "hidden_size": {
            "distribution": "int_uniform",
            "min": 64,
            "max": 512
        }
    },
    "early_terminate": {
        "type": "hyperband",
        "min_iter": 5,
        "eta": 3
    }
}

Search Strategies

StrategyHow It WorksBest For
GridExhaustively tries all combinationsSmall search spaces, categorical parameters
RandomRandomly samples from distributionsLarge search spaces, initial exploration
BayesianUses a probabilistic model to select promising configsExpensive evaluations, continuous parameters

Running a Sweep

Python - Complete sweep workflow
import wandb

def train():
    """Training function called by each sweep agent."""
    run = wandb.init()
    config = wandb.config

    # Build model with sweep parameters
    model = build_model(
        hidden_size=config.hidden_size,
        dropout=config.dropout
    )

    optimizer = get_optimizer(
        config.optimizer,
        model.parameters(),
        lr=config.learning_rate
    )

    for epoch in range(config.epochs):
        train_loss = train_epoch(model, optimizer, config.batch_size)
        val_loss, val_acc = validate(model)

        wandb.log({
            "train/loss": train_loss,
            "val/loss": val_loss,
            "val/accuracy": val_acc,
        })

    wandb.finish()

# Create the sweep
sweep_id = wandb.sweep(sweep_config, project="sweep-demo")

# Launch agents (run 50 trials)
wandb.agent(sweep_id, function=train, count=50)

Distributed Sweeps

Bash - Run agents on multiple machines
# Machine 1: Create the sweep
wandb sweep sweep_config.yaml
# Output: Created sweep with ID: abc123

# Machine 1, 2, 3, ...: Start agents
wandb agent your-entity/your-project/abc123
wandb agent your-entity/your-project/abc123
wandb agent your-entity/your-project/abc123

# Each agent pulls configs from the central sweep controller
# and reports results back. The Bayesian optimizer uses all
# results to choose the next config intelligently.

YAML Configuration

YAML - sweep_config.yaml
program: train.py
method: bayes
metric:
  name: val/accuracy
  goal: maximize
parameters:
  learning_rate:
    distribution: log_uniform_values
    min: 0.00001
    max: 0.1
  batch_size:
    values: [16, 32, 64, 128]
  dropout:
    distribution: uniform
    min: 0.0
    max: 0.5
early_terminate:
  type: hyperband
  min_iter: 5
Start with random, then refine with Bayesian: Run 20-30 random trials to understand the search space, then switch to Bayesian optimization to focus on the most promising regions. Use early termination (Hyperband) to kill underperforming runs quickly.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.