pythonadvanced
Weights & Biases Hyperparameter Sweeps
Run automated hyperparameter search with W&B Sweeps using Bayesian optimization.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonintermediate
Optuna Hyperparameter Optimization
Automate hyperparameter search with Optuna using Bayesian optimization and pruning.
Best for: AutoML
#optuna#hyperparameter
typescriptadvanced
Semantic Caching Layer for LLM Calls
Cache LLM responses by semantic similarity of prompts to reduce API costs and improve latency.
Best for: Reducing LLM API costs for repeated queries
#caching#embeddings
typescriptintermediate
Batch Embeddings Processing
Generate embeddings for large document sets in batches with rate limiting and progress tracking.
Best for: Indexing large document collections for search
#embeddings#batch-processing
typescriptadvanced
AI Model Router for Cost Optimization
Route prompts to different LLM models based on complexity to optimize cost and response quality.
Best for: Reducing AI API costs for production apps
#routing#optimization