How to beginner · 3 min read

How to configure wandb sweep

Quick answer
Use wandb.sweep() with a YAML or dictionary config specifying parameters, method, and metric. Then run wandb.agent() to execute the sweep with your training function. This automates hyperparameter search with wandb.

PREREQUISITES

  • Python 3.8+
  • pip install wandb
  • wandb account and API key
  • Basic Python scripting knowledge

Setup

Install wandb via pip and login to your account to get your API key. Set the API key as an environment variable or login interactively.

bash
pip install wandb
wandb login
output
wandb: Currently logged in as: your-username (use `wandb login --relogin` to force relogin)

Step by step

Create a sweep configuration dictionary or YAML defining the sweep method (e.g., grid, random, bayes), parameters to tune, and the metric to optimize. Initialize the sweep with wandb.sweep(). Define a training function that uses wandb.init() to track runs. Finally, start the sweep agent with wandb.agent() to run experiments automatically.

python
import wandb

# Define sweep configuration
sweep_config = {
    'method': 'random',  # grid, random, bayes
    'metric': {
        'name': 'accuracy',
        'goal': 'maximize'
    },
    'parameters': {
        'learning_rate': {
            'values': [0.01, 0.001, 0.0001]
        },
        'batch_size': {
            'values': [16, 32, 64]
        }
    }
}

# Initialize sweep
sweep_id = wandb.sweep(sweep_config, project='my-project')

# Define training function

def train():
    run = wandb.init()
    lr = run.config.learning_rate
    bs = run.config.batch_size
    # Dummy training loop
    accuracy = 0.8 + lr * 0.1  # Simulated metric
    wandb.log({'accuracy': accuracy})

# Run sweep agent
wandb.agent(sweep_id, function=train, count=5)
output
wandb: Creating sweep with ID: abc123
wandb: Agent started, running 5 runs
# Runs log metrics and parameters to wandb dashboard

Common variations

  • Use method: 'grid' for exhaustive search over parameter combinations.
  • Use method: 'bayes' for Bayesian optimization (requires wandb pro).
  • Run wandb.agent() asynchronously or on multiple machines for parallel sweeps.
  • Configure early stopping or custom metrics in your training function.

Troubleshooting

  • If wandb.sweep() fails, verify your API key and project name.
  • If runs do not start, check that wandb.agent() is called and network access is available.
  • Ensure your training function calls wandb.init() to initialize each run.

Key Takeaways

  • Define sweep config with method, parameters, and metric for optimization.
  • Use wandb.sweep() to create a sweep and wandb.agent() to run it.
  • Your training function must call wandb.init() to track each run.
  • Choose sweep method based on search strategy and project needs.
  • Troubleshoot by verifying API key, project, and network connectivity.
Verified 2026-04
Verify ↗