parallel-grid-search
Parallelize hyperparameter grid search using joblib for efficient multi-core execution.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 7
01 前言 Front Matter
- name
- parallel-grid-search
- description
- Parallelize hyperparameter grid search using joblib for efficient multi-core execution.
03 SKILL.md 正文
Parallel Grid Search
Overview
Use joblib to parallelize expensive computations across multiple CPU cores, significantly speeding up grid search over hyperparameter combinations.
Installation
pip install joblib scikit-learn
Basic Pattern
from joblib import Parallel, delayed
import itertools
def evaluate_hyperparams(hp_combination, data, evaluation_func):
"""Evaluate a single hyperparameter combination."""
result = evaluation_func(hp_combination, data)
return {**hp_combination, **result}
# Define hyperparameter grid
param_grid = {
'min_samples': [3, 4, 5, 6, 7, 8, 9],
'epsilon': [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
'shape_weight': [0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]
}
# Generate all combinations
combinations = [
{k: v for k, v in zip(param_grid.keys(), vals)}
for vals in itertools.product(*param_grid.values())
]
# Parallel evaluation
n_jobs = -1 # Use all available cores
results = Parallel(n_jobs=n_jobs, verbose=10)(
delayed(evaluate_hyperparams)(combo, data, eval_func)
for combo in combinations
)
Advanced: Batching and Progress
from tqdm import tqdm
def parallel_grid_search_batched(param_grid, data, evaluation_func, n_jobs=-1):
"""
Perform parallel grid search with progress tracking.
Args:
param_grid: Dictionary of parameter names to lists of values
data: Dataset to evaluate on
evaluation_func: Function that takes (hyperparams_dict, data) -> results_dict
n_jobs: Number of parallel jobs (-1 = all cores)
Returns:
List of result dictionaries
"""
# Generate all combinations
combinations = [
{k: v for k, v in zip(param_grid.keys(), vals)}
for vals in itertools.product(*param_grid.values())
]
# Parallel evaluation with progress bar
results = Parallel(n_jobs=n_jobs)(
delayed(evaluation_func)(combo, data)
for combo in tqdm(combinations, desc="Grid Search", total=len(combinations))
)
return results
joblib Best Practices
Memory-Efficient Processing
# Use batch_size for memory efficiency
results = Parallel(n_jobs=-1, batch_size='auto')(
delayed(expensive_function)(item)
for item in data
)
Controlling Verbosity
# verbose=10 prints progress every 10 jobs
# verbose=0 is silent, verbose=1 prints at start/end
results = Parallel(n_jobs=-1, verbose=10)(
delayed(task)(x) for x in items
)
Backend Selection
# Default is 'loky' (good for most tasks)
# 'threading' is lighter but can have GIL issues
# 'processes' spawns new processes
results = Parallel(n_jobs=-1, backend='loky')(
delayed(task)(x) for x in items
)
Collecting Results into DataFrame
import pandas as pd
results = Parallel(n_jobs=-1)(
delayed(evaluate_hyperparams)(combo, data, eval_func)
for combo in combinations
)
# Convert to DataFrame
results_df = pd.DataFrame(results)
# Filter and sort
filtered = results_df[results_df['f1'] > 0.5].sort_values('f1', ascending=False)
Notes
- joblib handles pickling of functions and data automatically
- For very large datasets, consider passing data references rather than copies
- Use
n_jobs=-1to use all cores; use-2to leave one core free - Progress tracking requires
tqdmpackage
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.