Model Hyperparameter Tuning

Model Hyperparameter Tuning

Guides an AI agent to tune model hyperparameters with grid, random and Bayesian search and tools like Optuna and Hyperopt, with Python examples and a notebook template.

Skill files3 files

scripts/

templates/

SKILL.md315 lines
1
---
2
name: Model Hyperparameter Tuning
3
description: Optimize hyperparameters using grid search, random search, Bayesian optimization, and automated ML frameworks like Optuna and Hyperopt
4
---
5
6
# Model Hyperparameter Tuning
7
8
## Overview
9
10
Hyperparameter tuning is the process of systematically searching for the best combination of model configuration parameters to maximize performance on validation data.
11
12
## When to Use
13
14
- When optimizing model performance beyond baseline configurations
15
- When comparing different parameter combinations systematically
16
- When fine-tuning complex models with many hyperparameters
17
- When seeking the best trade-off between bias, variance, and training time
18
- When improving model generalization on validation and test data
19
- When exploring parameter spaces for neural networks, tree models, or ensemble methods
20
21
## Tuning Methods
22
23
- **Grid Search**: Exhaustive search over parameter grid
24
- **Random Search**: Random sampling from parameter space
25
- **Bayesian Optimization**: Probabilistic model-based search
26
- **Hyperband**: Multi-fidelity optimization
27
- **Evolutionary Algorithms**: Genetic algorithm based search
28
- **Population-based Training**: Distributed parameter optimization
29
30
## Hyperparameters by Model Type
31
32
- **Tree Models**: max_depth, min_samples_split, learning_rate
33
- **Neural Networks**: learning_rate, batch_size, num_layers, dropout
34
- **SVM**: C, kernel, gamma
35
- **Ensemble**: n_estimators, max_features, min_samples_leaf
36
37
## Python Implementation
38
39
```python
40
import numpy as np
41
import pandas as pd
42
import matplotlib.pyplot as plt
43
from sklearn.datasets import make_classification
44
from sklearn.model_selection import train_test_split, cross_val_score
45
from sklearn.preprocessing import StandardScaler
46
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
47
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
48
import optuna
49
from optuna.samplers import TPESampler
50
import torch
51
import torch.nn as nn
52
from torch.optim import Adam
53
import time
54
55
# Create dataset
56
X, y = make_classification(n_samples=2000, n_features=50, n_informative=30,
57
n_redundant=10, random_state=42)
58
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
59
60
scaler = StandardScaler()
61
X_train_scaled = scaler.fit_transform(X_train)
62
X_test_scaled = scaler.transform(X_test)
63
64
print("Dataset shapes:", X_train_scaled.shape, X_test_scaled.shape)
65
66
# 1. Grid Search
67
print("\n=== 1. Grid Search ===")
68
start = time.time()
69
70
param_grid = {
71
'n_estimators': [50, 100, 200],
72
'max_depth': [5, 10, 15],
73
'min_samples_split': [2, 5, 10],
74
'min_samples_leaf': [1, 2, 4]
75
}
76
77
grid_search = GridSearchCV(
78
RandomForestClassifier(random_state=42),
79
param_grid,
80
cv=5,
81
scoring='accuracy',
82
n_jobs=-1,
83
verbose=0
84
)
85
86
grid_search.fit(X_train_scaled, y_train)
87
grid_time = time.time() - start
88
89
print(f"Best parameters: {grid_search.best_params_}")
90
print(f"Best CV score: {grid_search.best_score_:.4f}")
91
print(f"Test score: {grid_search.score(X_test_scaled, y_test):.4f}")
92
print(f"Time taken: {grid_time:.2f}s")
93
94
# 2. Random Search
95
print("\n=== 2. Random Search ===")
96
start = time.time()
97
98
param_dist = {
99
'n_estimators': np.arange(50, 300, 10),
100
'max_depth': np.arange(5, 30, 1),
101
'min_samples_split': np.arange(2, 20, 1),
102
'min_samples_leaf': np.arange(1, 10, 1),
103
'max_features': ['sqrt', 'log2']
104
}
105
106
random_search = RandomizedSearchCV(
107
RandomForestClassifier(random_state=42),
108
param_dist,
109
n_iter=20,
110
cv=5,
111
scoring='accuracy',
112
n_jobs=-1,
113
random_state=42,
114
verbose=0
115
)
116
117
random_search.fit(X_train_scaled, y_train)
118
random_time = time.time() - start
119
120
print(f"Best parameters: {random_search.best_params_}")
121
print(f"Best CV score: {random_search.best_score_:.4f}")
122
print(f"Test score: {random_search.score(X_test_scaled, y_test):.4f}")
123
print(f"Time taken: {random_time:.2f}s")
124
125
# 3. Bayesian Optimization with Optuna
126
print("\n=== 3. Bayesian Optimization (Optuna) ===")
127
128
def objective(trial):
129
params = {
130
'n_estimators': trial.suggest_int('n_estimators', 50, 300),
131
'max_depth': trial.suggest_int('max_depth', 5, 30),
132
'min_samples_split': trial.suggest_int('min_samples_split', 2, 20),
133
'min_samples_leaf': trial.suggest_int('min_samples_leaf', 1, 10),
134
'max_features': trial.suggest_categorical('max_features', ['sqrt', 'log2'])
135
}
136
137
model = RandomForestClassifier(**params, random_state=42)
138
scores = cross_val_score(model, X_train_scaled, y_train, cv=5, scoring='accuracy')
139
return scores.mean()
140
141
start = time.time()
142
sampler = TPESampler(seed=42)
143
study = optuna.create_study(sampler=sampler, direction='maximize')
144
study.optimize(objective, n_trials=20, show_progress_bar=False)
145
optuna_time = time.time() - start
146
147
best_trial = study.best_trial
148
print(f"Best parameters: {best_trial.params}")
149
print(f"Best CV score: {best_trial.value:.4f}")
150
151
# Train final model with best params
152
best_model = RandomForestClassifier(**best_trial.params, random_state=42)
153
best_model.fit(X_train_scaled, y_train)
154
print(f"Test score: {best_model.score(X_test_scaled, y_test):.4f}")
155
print(f"Time taken: {optuna_time:.2f}s")
156
157
# 4. Gradient Boosting hyperparameter tuning
158
print("\n=== 4. Gradient Boosting Tuning ===")
159
160
gb_param_grid = {
161
'learning_rate': [0.01, 0.05, 0.1, 0.2],
162
'n_estimators': [100, 200, 300],
163
'max_depth': [3, 5, 7, 9],
164
'min_samples_split': [2, 5, 10],
165
'subsample': [0.8, 0.9, 1.0]
166
}
167
168
gb_search = GridSearchCV(
169
GradientBoostingClassifier(random_state=42),
170
gb_param_grid,
171
cv=5,
172
scoring='accuracy',
173
n_jobs=-1,
174
verbose=0
175
)
176
177
gb_search.fit(X_train_scaled, y_train)
178
179
print(f"Best parameters: {gb_search.best_params_}")
180
print(f"Best CV score: {gb_search.best_score_:.4f}")
181
print(f"Test score: {gb_search.score(X_test_scaled, y_test):.4f}")
182
183
# 5. Learning rate tuning for neural networks
184
print("\n=== 5. Learning Rate Tuning for Neural Networks ===")
185
186
class SimpleNN(nn.Module):
187
def __init__(self):
188
super().__init__()
189
self.fc1 = nn.Linear(50, 128)
190
self.fc2 = nn.Linear(128, 64)
191
self.fc3 = nn.Linear(64, 1)
192
self.relu = nn.ReLU()
193
self.dropout = nn.Dropout(0.3)
194
195
def forward(self, x):
196
x = self.relu(self.fc1(x))
197
x = self.dropout(x)
198
x = self.relu(self.fc2(x))
199
x = self.dropout(x)
200
x = torch.sigmoid(self.fc3(x))
201
return x
202
203
learning_rates = [0.0001, 0.001, 0.01, 0.1]
204
lr_results = {}
205
206
device = torch.device('cpu')
207
208
for lr in learning_rates:
209
model = SimpleNN().to(device)
210
optimizer = Adam(model.parameters(), lr=lr)
211
criterion = nn.BCELoss()
212
213
X_train_tensor = torch.FloatTensor(X_train_scaled)
214
y_train_tensor = torch.FloatTensor(y_train).unsqueeze(1)
215
216
best_loss = float('inf')
217
patience = 10
218
patience_counter = 0
219
220
for epoch in range(100):
221
output = model(X_train_tensor)
222
loss = criterion(output, y_train_tensor)
223
224
optimizer.zero_grad()
225
loss.backward()
226
optimizer.step()
227
228
if loss.item() < best_loss:
229
best_loss = loss.item()
230
patience_counter = 0
231
else:
232
patience_counter += 1
233
234
if patience_counter >= patience:
235
break
236
237
lr_results[lr] = best_loss
238
print(f"Learning Rate {lr}: Best Loss = {best_loss:.6f}")
239
240
# 6. Comparison visualization
241
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
242
243
# Search method comparison
244
methods = ['Grid Search', 'Random Search', 'Bayesian Opt']
245
times = [grid_time, random_time, optuna_time]
246
scores = [grid_search.best_score_, random_search.best_score_, study.best_value]
247
248
x = np.arange(len(methods))
249
axes[0, 0].bar(x, times, color='steelblue', alpha=0.7)
250
axes[0, 0].set_ylabel('Time (seconds)')
251
axes[0, 0].set_title('Tuning Method Comparison - Time')
252
axes[0, 0].set_xticks(x)
253
axes[0, 0].set_xticklabels(methods)
254
255
axes[0, 1].bar(x, scores, color='coral', alpha=0.7)
256
axes[0, 1].set_ylabel('CV Accuracy')
257
axes[0, 1].set_title('Tuning Method Comparison - Accuracy')
258
axes[0, 1].set_xticks(x)
259
axes[0, 1].set_xticklabels(methods)
260
axes[0, 1].set_ylim([0.8, 1.0])
261
262
# Hyperparameter importance from Optuna
263
importance_dict = {}
264
for param_name in study.best_trial.params.keys():
265
trial_values = []
266
for trial in study.trials:
267
if param_name in trial.params:
268
trial_values.append(trial.value)
269
if trial_values:
270
importance_dict[param_name] = np.std(trial_values)
271
272
axes[1, 0].barh(list(importance_dict.keys()), list(importance_dict.values()),
273
color='lightgreen', edgecolor='black')
274
axes[1, 0].set_xlabel('Importance (Std Dev)')
275
axes[1, 0].set_title('Hyperparameter Importance')
276
277
# Learning rate tuning for NN
278
axes[1, 1].plot(list(lr_results.keys()), list(lr_results.values()), marker='o',
279
linewidth=2, markersize=8, color='purple')
280
axes[1, 1].set_xlabel('Learning Rate')
281
axes[1, 1].set_ylabel('Best Training Loss')
282
axes[1, 1].set_title('Learning Rate Impact on Neural Network')
283
axes[1, 1].set_xscale('log')
284
axes[1, 1].grid(True, alpha=0.3)
285
286
plt.tight_layout()
287
plt.savefig('hyperparameter_tuning.png', dpi=100, bbox_inches='tight')
288
print("\nVisualization saved as 'hyperparameter_tuning.png'")
289
290
print("\nHyperparameter tuning completed!")
291
```
292
293
## Tuning Strategy by Model
294
295
- **Tree Models**: Focus on depth, min_samples, max_features
296
- **Boosting**: Learning_rate, n_estimators, subsample
297
- **Neural Networks**: Learning rate, batch size, regularization
298
- **SVM**: C and kernel type are most important
299
300
## Best Practices
301
302
- Scale search space logarithmically for continuous parameters
303
- Use cross-validation for robust estimates
304
- Start with random search for initial exploration
305
- Use Bayesian optimization for final refinement
306
- Monitor for diminishing returns
307
308
## Deliverables
309
310
- Optimal hyperparameters found
311
- Performance metrics for top configurations
312
- Tuning efficiency analysis
313
- Visualization of parameter impact
314
- Tuning report and recommendations
315

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…