
Model Optimization
- 33 installs
- 4 repo stars
- Updated January 5, 2026
- pluginagentmarketplace/custom-plugin-ai-data-scientist
model-optimization is a Claude Code skill for ai & agent building.
About
model-optimization is a Claude Code skill for ai & agent building. It helps solo builders move faster with AI-assisted development.
- model-optimization
- AI & Agent Building
- AI-coding skill
Model Optimization by the numbers
- 33 all-time installs (skills.sh)
- Ranked #8,944 of 16,546 AI & Agent Building skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-ai-data-scientist --skill model-optimizationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 33 |
|---|---|
| repo stars | ★ 4 |
| Last updated | January 5, 2026 |
| Repository | pluginagentmarketplace/custom-plugin-ai-data-scientist ↗ |
How do I helps with ai & agent building tasks.?
Helps with ai & agent building tasks.
Who is it for?
Best when you're working on ai & agent building and need structured help with model optimization.
Skip if: Teams with no ai & agent building needs, or anyone wanting a generic chat assistant without this specific workflow.
When should I use this skill?
When you need to helps with ai & agent building tasks., or when model-optimization is a claude code skill for ai & agent building.
What you get
Structured output aligned to model-optimization: model-optimization, AI & Agent Building.
Files
Model Optimization
Optimize models for better performance, efficiency, and faster inference.
Hyperparameter Tuning
Grid Search
from sklearn.model_selection import GridSearchCV
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [5, 10, 15],
'min_samples_split': [2, 5, 10]
}
grid_search = GridSearchCV(
RandomForestClassifier(),
param_grid,
cv=5,
scoring='f1_weighted',
n_jobs=-1
)
grid_search.fit(X_train, y_train)
print(f"Best params: {grid_search.best_params_}")
print(f"Best score: {grid_search.best_score_:.3f}")Bayesian Optimization
from skopt import BayesSearchCV
param_space = {
'n_estimators': (100, 500),
'max_depth': (5, 50),
'learning_rate': (0.01, 0.3, 'log-uniform')
}
bayes_search = BayesSearchCV(
xgb.XGBClassifier(),
param_space,
n_iter=50,
cv=5,
scoring='f1_weighted'
)
bayes_search.fit(X_train, y_train)Optuna
import optuna
def objective(trial):
params = {
'n_estimators': trial.suggest_int('n_estimators', 100, 1000),
'max_depth': trial.suggest_int('max_depth', 3, 10),
'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3)
}
model = xgb.XGBClassifier(**params)
score = cross_val_score(model, X_train, y_train,
cv=5, scoring='f1').mean()
return score
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100)
print(f"Best params: {study.best_params}")
print(f"Best score: {study.best_value:.3f}")Model Compression
Quantization (PyTorch)
import torch
# Post-training dynamic quantization
model_fp32 = MyModel()
model_int8 = torch.quantization.quantize_dynamic(
model_fp32,
{torch.nn.Linear},
dtype=torch.qint8
)
# 4x smaller model, 2-4x faster inference
# Quantization-aware training
model = MyModel()
model.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')
model_prepared = torch.quantization.prepare_qat(model)
# Train
for epoch in range(epochs):
train(model_prepared)
model_quantized = torch.quantization.convert(model_prepared)Pruning
import torch.nn.utils.prune as prune
# Global unstructured pruning
parameters_to_prune = [
(module, 'weight') for module in model.modules()
if isinstance(module, torch.nn.Linear)
]
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=0.2 # Remove 20% of weights
)
# Remove pruning reparametrization
for module, _ in parameters_to_prune:
prune.remove(module, 'weight')Knowledge Distillation
import torch.nn.functional as F
def distillation_loss(student_logits, teacher_logits, labels, T=3.0, alpha=0.5):
"""
Distillation loss: combination of soft targets from teacher
and hard targets from ground truth
"""
# Soft targets (knowledge from teacher)
soft_targets = F.softmax(teacher_logits / T, dim=1)
soft_prob = F.log_softmax(student_logits / T, dim=1)
soft_loss = F.kl_div(soft_prob, soft_targets, reduction='batchmean') * (T ** 2)
# Hard targets (ground truth)
hard_loss = F.cross_entropy(student_logits, labels)
# Combined loss
return alpha * soft_loss + (1 - alpha) * hard_loss
# Train student model
teacher_model.eval()
student_model.train()
for images, labels in train_loader:
with torch.no_grad():
teacher_logits = teacher_model(images)
student_logits = student_model(images)
loss = distillation_loss(student_logits, teacher_logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()AutoML
Auto-sklearn
import autosklearn.classification
automl = autosklearn.classification.AutoSklearnClassifier(
time_left_for_this_task=3600, # 1 hour
per_run_time_limit=300,
memory_limit=3072
)
automl.fit(X_train, y_train)
predictions = automl.predict(X_test)
print(automl.leaderboard())
print(automl.show_models())H2O AutoML
import h2o
from h2o.automl import H2OAutoML
h2o.init()
train = h2o.H2OFrame(pd.concat([X_train, y_train], axis=1))
test = h2o.H2OFrame(pd.concat([X_test, y_test], axis=1))
aml = H2OAutoML(max_runtime_secs=3600, max_models=20)
aml.train(x=X_train.columns.tolist(), y='target',
training_frame=train)
# Leaderboard
lb = aml.leaderboard
print(lb)
# Best model
best_model = aml.leader
predictions = best_model.predict(test)TPOT
from tpot import TPOTClassifier
tpot = TPOTClassifier(
generations=5,
population_size=50,
verbosity=2,
random_state=42,
n_jobs=-1
)
tpot.fit(X_train, y_train)
print(f"Score: {tpot.score(X_test, y_test):.3f}")
# Export best pipeline
tpot.export('best_pipeline.py')Feature Selection
from sklearn.feature_selection import (
SelectKBest, f_classif, RFE, SelectFromModel
)
# Univariate selection
selector = SelectKBest(f_classif, k=10)
X_new = selector.fit_transform(X, y)
# Recursive Feature Elimination
estimator = RandomForestClassifier()
rfe = RFE(estimator, n_features_to_select=10)
X_new = rfe.fit_transform(X, y)
# Model-based selection
selector = SelectFromModel(RandomForestClassifier(), max_features=10)
X_new = selector.fit_transform(X, y)Performance Optimization
Inference Optimization (ONNX)
import torch.onnx
# Export PyTorch to ONNX
dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(
model,
dummy_input,
"model.onnx",
opset_version=11,
input_names=['input'],
output_names=['output']
)
# Run with ONNX Runtime
import onnxruntime as ort
session = ort.InferenceSession("model.onnx")
input_name = session.get_inputs()[0].name
output = session.run(None, {input_name: input_data})TensorRT (NVIDIA GPU)
import tensorrt as trt
# Convert ONNX to TensorRT
logger = trt.Logger(trt.Logger.WARNING)
builder = trt.Builder(logger)
network = builder.create_network()
parser = trt.OnnxParser(network, logger)
with open('model.onnx', 'rb') as f:
parser.parse(f.read())
config = builder.create_builder_config()
config.max_workspace_size = 1 << 30 # 1GB
engine = builder.build_engine(network, config)
# 10x faster inference on GPULearning Rate Scheduling
from torch.optim.lr_scheduler import StepLR, CosineAnnealingLR
# Step decay
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
# Cosine annealing
scheduler = CosineAnnealingLR(optimizer, T_max=100)
# Training loop
for epoch in range(epochs):
train(model, optimizer)
scheduler.step()Early Stopping
class EarlyStopping:
def __init__(self, patience=7, min_delta=0):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss):
if self.best_loss is None:
self.best_loss = val_loss
elif val_loss > self.best_loss - self.min_delta:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_loss = val_loss
self.counter = 0
# Usage
early_stopping = EarlyStopping(patience=10)
for epoch in range(epochs):
train_loss = train(model)
val_loss = validate(model)
early_stopping(val_loss)
if early_stopping.early_stop:
print("Early stopping triggered")
breakBest Practices
1. Start simple: Baseline model first 2. Profile before optimizing: Find bottlenecks 3. Measure everything: Track metrics 4. Trade-offs: Accuracy vs speed vs size 5. Validate improvements: A/B testing 6. Automate: Use AutoML for initial exploration
# Hyperparameter Optimization Configuration
# AutoML and hyperparameter tuning settings
# Study Configuration
study:
name: "model_optimization"
direction: "maximize" # maximize, minimize
metric: "val_f1"
sampler: "tpe" # tpe, random, grid, cmaes
pruner: "hyperband" # hyperband, median, successive_halving
# Search Space Definition
search_space:
# Model Selection
model_type:
type: "categorical"
choices: ["random_forest", "xgboost", "lightgbm", "neural_network"]
# Random Forest
random_forest:
n_estimators:
type: "int"
low: 50
high: 500
step: 50
max_depth:
type: "int"
low: 3
high: 20
min_samples_split:
type: "int"
low: 2
high: 20
min_samples_leaf:
type: "int"
low: 1
high: 10
# XGBoost
xgboost:
n_estimators:
type: "int"
low: 100
high: 1000
max_depth:
type: "int"
low: 3
high: 12
learning_rate:
type: "float"
low: 0.01
high: 0.3
log: true
subsample:
type: "float"
low: 0.6
high: 1.0
colsample_bytree:
type: "float"
low: 0.6
high: 1.0
reg_alpha:
type: "float"
low: 1e-8
high: 10.0
log: true
reg_lambda:
type: "float"
low: 1e-8
high: 10.0
log: true
# LightGBM
lightgbm:
n_estimators:
type: "int"
low: 100
high: 1000
num_leaves:
type: "int"
low: 20
high: 150
max_depth:
type: "int"
low: 3
high: 12
learning_rate:
type: "float"
low: 0.01
high: 0.3
log: true
feature_fraction:
type: "float"
low: 0.6
high: 1.0
bagging_fraction:
type: "float"
low: 0.6
high: 1.0
min_child_samples:
type: "int"
low: 5
high: 100
# Neural Network
neural_network:
hidden_layers:
type: "int"
low: 1
high: 5
hidden_units:
type: "categorical"
choices: [32, 64, 128, 256, 512]
dropout_rate:
type: "float"
low: 0.0
high: 0.5
learning_rate:
type: "float"
low: 1e-5
high: 1e-2
log: true
batch_size:
type: "categorical"
choices: [16, 32, 64, 128]
optimizer:
type: "categorical"
choices: ["adam", "sgd", "rmsprop"]
activation:
type: "categorical"
choices: ["relu", "gelu", "selu"]
# Optimization Settings
optimization:
n_trials: 100
timeout_seconds: 3600
n_jobs: 4
cv_folds: 5
# Early stopping
early_stopping:
enabled: true
patience: 20
min_trials: 10
# Parallelization
parallel:
enabled: true
n_workers: 4
storage: "sqlite:///optuna_study.db"
# Model Compression
compression:
quantization:
enabled: true
method: "dynamic" # dynamic, static, qat
dtype: "int8"
pruning:
enabled: true
method: "magnitude" # magnitude, structured, unstructured
amount: 0.3
distillation:
enabled: false
teacher_model: "large_model"
temperature: 3.0
alpha: 0.5
# Export Configuration
export:
formats:
- "pickle"
- "onnx"
- "joblib"
save_trials: true
save_best_model: true
output_dir: "outputs/optimization"
Model Optimization Guide
Optimization Strategy Selection
Start
│
▼
┌─────────────────┐
│ Few hyperparams │──► Grid Search (exhaustive)
│ (<5 params) │
└────────┬────────┘
│ Many params
▼
┌─────────────────┐
│ Limited budget │──► Random Search (faster)
│ (<100 trials) │
└────────┬────────┘
│ More budget
▼
┌─────────────────┐
│ Expensive evals │──► Bayesian (Optuna/HyperOpt)
│ (DL models) │
└────────┬────────┘
│ Very expensive
▼
┌─────────────────┐
│ Unlimited │──► Population (evolutionary)
│ compute │
└─────────────────┘Search Method Comparison
| Method | Efficiency | Parallelizable | Best For |
|---|---|---|---|
| Grid Search | Low | Yes | Small spaces |
| Random Search | Medium | Yes | High-dim spaces |
| Bayesian (TPE) | High | Limited | Most cases |
| Evolutionary | Medium | Yes | Complex spaces |
| Hyperband | High | Yes | Neural networks |
Common Hyperparameter Ranges
Tree-based Models
random_forest:
n_estimators: [100, 500]
max_depth: [3, 20]
min_samples_split: [2, 20]
min_samples_leaf: [1, 10]
xgboost:
n_estimators: [100, 1000]
max_depth: [3, 12]
learning_rate: [0.01, 0.3]
subsample: [0.6, 1.0]
colsample_bytree: [0.6, 1.0]
lightgbm:
n_estimators: [100, 1000]
num_leaves: [20, 150]
max_depth: [3, 12]
learning_rate: [0.01, 0.3]Neural Networks
learning_rate: [1e-5, 1e-2] (log scale)
batch_size: [16, 32, 64, 128, 256]
dropout: [0.0, 0.5]
hidden_units: [32, 64, 128, 256, 512]
weight_decay: [1e-6, 1e-2] (log scale)Model Compression Techniques
| Technique | Size Reduction | Speed Gain | Accuracy Loss |
|---|---|---|---|
| Quantization (INT8) | 4x | 2-4x | 0-2% |
| Pruning (30%) | 1.4x | 1.2x | 0-1% |
| Knowledge Distillation | Varies | Varies | 1-3% |
| ONNX Conversion | 1x | 1.5-2x | 0% |
| TensorRT | 1x | 2-5x | <1% |
Early Stopping Best Practices
# Configuration
patience = 10 # Wait 10 epochs
min_delta = 0.001 # Minimum improvement
monitor = 'val_loss' # Metric to watch
mode = 'min' # Lower is better
restore_best = True # Load best weightsCross-Validation Strategies
| Strategy | Use When |
|---|---|
| KFold | Standard, balanced data |
| StratifiedKFold | Imbalanced classification |
| TimeSeriesSplit | Time-dependent data |
| GroupKFold | Group-based data |
| LeaveOneOut | Very small datasets |
Optimization Checklist
Pre-optimization:
□ Baseline model established
□ Evaluation metric defined
□ Cross-validation strategy chosen
□ Search space defined
During optimization:
□ Early stopping enabled
□ Pruning for failed trials
□ Results logged
□ Resource limits set
Post-optimization:
□ Best model validated
□ Hyperparameter importance analyzed
□ Overfitting checked
□ Model saved and documentedCommon Pitfalls
1. Overfitting to validation set - Use nested CV 2. Too narrow search space - Start broad 3. Ignoring early stopping - Wastes compute 4. No baseline comparison - Can't measure improvement 5. Random seed not fixed - Non-reproducible results
Tools Comparison
| Tool | Ease | Distributed | Visualization |
|---|---|---|---|
| Optuna | High | Yes | Built-in |
| Ray Tune | Medium | Excellent | TensorBoard |
| Hyperopt | Medium | Limited | Manual |
| Scikit-optimize | High | No | Manual |
| Keras Tuner | High | No | Built-in |
#!/usr/bin/env python3
"""
Hyperparameter Optimization with Optuna
Automated ML model tuning framework
"""
import optuna
from optuna.pruners import HyperbandPruner
from optuna.samplers import TPESampler
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, make_scorer
from typing import Dict, Any, Optional, Callable
import logging
import warnings
warnings.filterwarnings('ignore')
optuna.logging.set_verbosity(optuna.logging.WARNING)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class OptunaOptimizer:
"""Hyperparameter optimization using Optuna."""
def __init__(
self,
model_class: type,
search_space: Dict[str, Dict],
metric: str = 'f1',
direction: str = 'maximize',
cv: int = 5,
n_jobs: int = -1
):
self.model_class = model_class
self.search_space = search_space
self.metric = metric
self.direction = direction
self.cv = cv
self.n_jobs = n_jobs
self.study = None
self.best_model = None
def _sample_params(self, trial: optuna.Trial) -> Dict[str, Any]:
"""Sample hyperparameters from search space."""
params = {}
for param_name, config in self.search_space.items():
param_type = config['type']
if param_type == 'int':
params[param_name] = trial.suggest_int(
param_name,
config['low'],
config['high'],
step=config.get('step', 1)
)
elif param_type == 'float':
params[param_name] = trial.suggest_float(
param_name,
config['low'],
config['high'],
log=config.get('log', False)
)
elif param_type == 'categorical':
params[param_name] = trial.suggest_categorical(
param_name,
config['choices']
)
return params
def _objective(self, trial: optuna.Trial, X, y) -> float:
"""Objective function for optimization."""
params = self._sample_params(trial)
try:
model = self.model_class(**params)
# Define scoring based on metric
if self.metric == 'f1':
scorer = make_scorer(f1_score, average='weighted')
elif self.metric == 'accuracy':
scorer = 'accuracy'
elif self.metric == 'roc_auc':
scorer = 'roc_auc'
else:
scorer = self.metric
scores = cross_val_score(
model, X, y,
cv=self.cv,
scoring=scorer,
n_jobs=self.n_jobs
)
return scores.mean()
except Exception as e:
logger.warning(f"Trial failed: {e}")
return float('-inf') if self.direction == 'maximize' else float('inf')
def optimize(
self,
X,
y,
n_trials: int = 100,
timeout: Optional[int] = None,
callbacks: Optional[list] = None
) -> Dict[str, Any]:
"""Run hyperparameter optimization."""
logger.info(f"Starting optimization with {n_trials} trials...")
# Create study
self.study = optuna.create_study(
direction=self.direction,
sampler=TPESampler(seed=42),
pruner=HyperbandPruner()
)
# Run optimization
self.study.optimize(
lambda trial: self._objective(trial, X, y),
n_trials=n_trials,
timeout=timeout,
callbacks=callbacks,
show_progress_bar=True
)
# Get best parameters
best_params = self.study.best_params
best_score = self.study.best_value
logger.info(f"Best score: {best_score:.4f}")
logger.info(f"Best params: {best_params}")
# Train final model with best params
self.best_model = self.model_class(**best_params)
self.best_model.fit(X, y)
return {
'best_params': best_params,
'best_score': best_score,
'n_trials': len(self.study.trials),
'model': self.best_model
}
def get_importance(self) -> Dict[str, float]:
"""Get hyperparameter importance."""
if self.study is None:
raise ValueError("Run optimize() first")
try:
importance = optuna.importance.get_param_importances(self.study)
return dict(importance)
except Exception as e:
logger.warning(f"Could not compute importance: {e}")
return {}
def get_trials_dataframe(self):
"""Get trials as DataFrame."""
if self.study is None:
raise ValueError("Run optimize() first")
return self.study.trials_dataframe()
def create_search_space(model_type: str) -> Dict[str, Dict]:
"""Create search space for common model types."""
spaces = {
'random_forest': {
'n_estimators': {'type': 'int', 'low': 50, 'high': 300, 'step': 50},
'max_depth': {'type': 'int', 'low': 3, 'high': 15},
'min_samples_split': {'type': 'int', 'low': 2, 'high': 20},
'min_samples_leaf': {'type': 'int', 'low': 1, 'high': 10},
'max_features': {'type': 'categorical', 'choices': ['sqrt', 'log2', None]}
},
'xgboost': {
'n_estimators': {'type': 'int', 'low': 100, 'high': 500},
'max_depth': {'type': 'int', 'low': 3, 'high': 10},
'learning_rate': {'type': 'float', 'low': 0.01, 'high': 0.3, 'log': True},
'subsample': {'type': 'float', 'low': 0.6, 'high': 1.0},
'colsample_bytree': {'type': 'float', 'low': 0.6, 'high': 1.0}
},
'lightgbm': {
'n_estimators': {'type': 'int', 'low': 100, 'high': 500},
'num_leaves': {'type': 'int', 'low': 20, 'high': 100},
'max_depth': {'type': 'int', 'low': 3, 'high': 10},
'learning_rate': {'type': 'float', 'low': 0.01, 'high': 0.3, 'log': True},
'feature_fraction': {'type': 'float', 'low': 0.6, 'high': 1.0}
}
}
return spaces.get(model_type, spaces['random_forest'])
def main():
"""Demo hyperparameter optimization."""
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
print("Hyperparameter Optimization Demo")
print("=" * 50)
# Generate sample data
X, y = make_classification(
n_samples=1000,
n_features=20,
n_informative=10,
n_redundant=5,
n_classes=2,
random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Create optimizer
search_space = create_search_space('random_forest')
optimizer = OptunaOptimizer(
model_class=RandomForestClassifier,
search_space=search_space,
metric='f1',
direction='maximize',
cv=3
)
# Run optimization (fewer trials for demo)
results = optimizer.optimize(X_train, y_train, n_trials=20)
print(f"\nBest Parameters: {results['best_params']}")
print(f"Best CV Score: {results['best_score']:.4f}")
# Evaluate on test set
y_pred = results['model'].predict(X_test)
test_f1 = f1_score(y_test, y_pred, average='weighted')
print(f"Test F1 Score: {test_f1:.4f}")
# Parameter importance
importance = optimizer.get_importance()
print("\nParameter Importance:")
for param, imp in sorted(importance.items(), key=lambda x: x[1], reverse=True):
print(f" {param}: {imp:.4f}")
print("\n[SUCCESS] Optimization complete!")
if __name__ == '__main__':
main()
Related skills
FAQ
What does model-optimization do?
model-optimization is a Claude Code skill for ai & agent building.
When should I use model-optimization?
When you need to helps with ai & agent building tasks., or when model-optimization is a claude code skill for ai & agent building.
What are the main capabilities?
model-optimization; AI & Agent Building; AI-coding skill.