optuna-hyperparameter-tuner
Optuna integration skill for automated hyperparameter optimization with advanced search strategies, pruning, multi-objective optimization, and visualization capabilities.
What this skill does
# Optuna Hyperparameter Tuner
Optimize hyperparameters using Optuna with advanced search strategies, pruning, and visualization.
## Overview
This skill provides comprehensive capabilities for hyperparameter optimization using Optuna, the state-of-the-art hyperparameter optimization framework. It supports various samplers, pruners, multi-objective optimization, and integration with popular ML frameworks.
## Capabilities
### Search Strategies
- Tree-structured Parzen Estimator (TPE) - default, efficient
- CMA-ES - for continuous parameters
- Grid search - exhaustive
- Random search - baseline
- NSGAII - multi-objective optimization
- QMC (Quasi-Monte Carlo) - low-discrepancy sampling
### Pruning Strategies
- Median pruning - early stop underperformers
- Hyperband (ASHA) - aggressive resource allocation
- Percentile pruning - threshold-based
- Successive Halving - efficient resource use
- Wilcoxon pruning - statistical comparison
### Multi-Objective Optimization
- Pareto front optimization
- Multiple objective functions
- Constraint handling
- Trade-off visualization
### Study Management
- Study persistence (SQLite, PostgreSQL, MySQL)
- Study resumption
- Parallel/distributed optimization
- Trial importance analysis
- Parameter relationship analysis
### Visualization
- Optimization history
- Parameter importance
- Parallel coordinate plots
- Slice plots
- Contour plots
## Prerequisites
### Installation
```bash
pip install optuna>=3.0.0
```
### Optional Dependencies
```bash
# Database backends
pip install optuna[mysql] # MySQL support
pip install optuna[postgresql] # PostgreSQL support
# Visualization
pip install optuna-dashboard # Web dashboard
pip install plotly # Interactive plots
# Framework integrations
pip install optuna-integration[sklearn]
pip install optuna-integration[pytorch]
pip install optuna-integration[tensorflow]
```
## Usage Patterns
### Basic Optimization
```python
import optuna
def objective(trial):
# Suggest hyperparameters
learning_rate = trial.suggest_float('learning_rate', 1e-5, 1e-1, log=True)
n_estimators = trial.suggest_int('n_estimators', 50, 500)
max_depth = trial.suggest_int('max_depth', 3, 15)
subsample = trial.suggest_float('subsample', 0.5, 1.0)
# Train model
model = XGBClassifier(
learning_rate=learning_rate,
n_estimators=n_estimators,
max_depth=max_depth,
subsample=subsample,
random_state=42
)
# Cross-validation
score = cross_val_score(model, X_train, y_train, cv=5, scoring='accuracy').mean()
return score
# Create study
study = optuna.create_study(
direction='maximize',
study_name='xgboost-tuning',
storage='sqlite:///optuna.db',
load_if_exists=True
)
# Optimize
study.optimize(objective, n_trials=100, timeout=3600)
# Best results
print(f"Best trial: {study.best_trial.number}")
print(f"Best value: {study.best_value:.4f}")
print(f"Best params: {study.best_params}")
```
### With Pruning
```python
import optuna
from optuna.pruners import MedianPruner
def objective_with_pruning(trial):
# Suggest hyperparameters
learning_rate = trial.suggest_float('learning_rate', 1e-5, 1e-1, log=True)
n_epochs = trial.suggest_int('n_epochs', 10, 100)
# Create model
model = create_model(learning_rate)
# Training loop with pruning
for epoch in range(n_epochs):
train_loss = train_one_epoch(model)
val_accuracy = evaluate(model)
# Report intermediate value
trial.report(val_accuracy, epoch)
# Prune if unpromising
if trial.should_prune():
raise optuna.TrialPruned()
return val_accuracy
# Create study with pruner
study = optuna.create_study(
direction='maximize',
pruner=MedianPruner(n_startup_trials=5, n_warmup_steps=10)
)
study.optimize(objective_with_pruning, n_trials=100)
```
### Multi-Objective Optimization
```python
import optuna
def multi_objective(trial):
# Hyperparameters
learning_rate = trial.suggest_float('learning_rate', 1e-5, 1e-1, log=True)
model_size = trial.suggest_categorical('model_size', ['small', 'medium', 'large'])
# Train model
model = create_model(learning_rate, model_size)
train(model)
# Multiple objectives
accuracy = evaluate_accuracy(model)
inference_time = measure_inference_time(model)
return accuracy, inference_time # maximize accuracy, minimize time
# Create multi-objective study
study = optuna.create_study(
directions=['maximize', 'minimize'],
study_name='pareto-optimization'
)
study.optimize(multi_objective, n_trials=100)
# Get Pareto front
pareto_front = study.best_trials
for trial in pareto_front:
print(f"Accuracy: {trial.values[0]:.4f}, Time: {trial.values[1]:.4f}")
```
### Scikit-learn Integration
```python
import optuna
from optuna.integration import OptunaSearchCV
# Define parameter distributions
param_distributions = {
'n_estimators': optuna.distributions.IntDistribution(50, 500),
'max_depth': optuna.distributions.IntDistribution(3, 15),
'learning_rate': optuna.distributions.FloatDistribution(1e-5, 1e-1, log=True),
'subsample': optuna.distributions.FloatDistribution(0.5, 1.0)
}
# Create search
search = OptunaSearchCV(
XGBClassifier(random_state=42),
param_distributions,
n_trials=100,
cv=5,
scoring='accuracy',
study=study, # Optional: use existing study
n_jobs=-1
)
# Fit
search.fit(X_train, y_train)
# Results
print(f"Best score: {search.best_score_:.4f}")
print(f"Best params: {search.best_params_}")
```
### PyTorch Integration
```python
import optuna
from optuna.integration import PyTorchLightningPruningCallback
def objective(trial):
# Hyperparameters
lr = trial.suggest_float('lr', 1e-5, 1e-1, log=True)
hidden_size = trial.suggest_int('hidden_size', 32, 256)
dropout = trial.suggest_float('dropout', 0.1, 0.5)
# Create model
model = LightningModel(
hidden_size=hidden_size,
dropout=dropout,
lr=lr
)
# Create trainer with pruning callback
trainer = pl.Trainer(
max_epochs=100,
callbacks=[
PyTorchLightningPruningCallback(trial, monitor='val_accuracy')
]
)
trainer.fit(model, train_loader, val_loader)
return trainer.callback_metrics['val_accuracy'].item()
```
### Distributed Optimization
```python
import optuna
# Create shared study with database storage
study = optuna.create_study(
study_name='distributed-study',
storage='postgresql://user:pass@host:5432/optuna',
direction='maximize',
load_if_exists=True
)
# Run on multiple workers (each worker runs this)
study.optimize(objective, n_trials=25) # Each worker does 25 trials
# Results are automatically aggregated
print(f"Total trials: {len(study.trials)}")
```
## Integration with Babysitter SDK
### Task Definition Example
```javascript
const hyperparameterTuningTask = defineTask({
name: 'optuna-hyperparameter-tuning',
description: 'Optimize hyperparameters using Optuna',
inputs: {
studyName: { type: 'string', required: true },
direction: { type: 'string', default: 'maximize' },
nTrials: { type: 'number', default: 100 },
timeout: { type: 'number' },
parameterSpace: { type: 'object', required: true },
objectiveScript: { type: 'string', required: true },
sampler: { type: 'string', default: 'tpe' },
pruner: { type: 'string', default: 'median' }
},
outputs: {
bestValue: { type: 'number' },
bestParams: { type: 'object' },
nTrialsCompleted: { type: 'number' },
studyPath: { type: 'string' }
},
async run(inputs, taskCtx) {
return {
kind: 'skill',
title: `Optimize: ${inputs.studyName}`,
skill: {
name: 'optuna-hyperparameter-tuner',
context: {
operation: 'optimize',
studyName: inputs.studyName,
direction: inputs.direction,
nTrials: inputs.nTrials,
tiRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.