|
| 1 | +.. _examples_smac_backend: |
| 2 | + |
| 3 | +============ |
| 4 | +SMAC Backend |
| 5 | +============ |
| 6 | + |
| 7 | +Hyperactive provides wrappers for SMAC3's (Sequential Model-based Algorithm |
| 8 | +Configuration) optimization algorithms, enabling state-of-the-art Bayesian |
| 9 | +optimization with Random Forest and Gaussian Process surrogate models. |
| 10 | + |
| 11 | +.. note:: |
| 12 | + |
| 13 | + SMAC must be installed separately: |
| 14 | + |
| 15 | + .. code-block:: bash |
| 16 | +
|
| 17 | + pip install hyperactive[smac] |
| 18 | + # or |
| 19 | + pip install hyperactive[all_extras] |
| 20 | +
|
| 21 | +
|
| 22 | +Available Optimizers |
| 23 | +-------------------- |
| 24 | + |
| 25 | +SMAC provides three optimization strategies with different surrogate models: |
| 26 | + |
| 27 | +.. list-table:: |
| 28 | + :header-rows: 1 |
| 29 | + :widths: 25 35 40 |
| 30 | + |
| 31 | + * - Optimizer |
| 32 | + - Surrogate Model |
| 33 | + - Best For |
| 34 | + * - ``SmacRandomForest`` |
| 35 | + - Random Forest |
| 36 | + - Mixed parameter spaces (continuous, categorical, integer) |
| 37 | + * - ``SmacGaussianProcess`` |
| 38 | + - Gaussian Process |
| 39 | + - Continuous parameter spaces, small to moderate budgets |
| 40 | + * - ``SmacRandomSearch`` |
| 41 | + - None (random sampling) |
| 42 | + - Baseline comparison, high-dimensional spaces |
| 43 | + |
| 44 | + |
| 45 | +SmacRandomForest |
| 46 | +---------------- |
| 47 | + |
| 48 | +The flagship SMAC optimizer. Uses a Random Forest surrogate model with |
| 49 | +Expected Improvement acquisition function. Handles mixed parameter types natively. |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + from hyperactive.opt.smac import SmacRandomForest |
| 54 | +
|
| 55 | + param_space = { |
| 56 | + "C": (0.01, 100.0), # Float range |
| 57 | + "gamma": (0.0001, 1.0), # Float range |
| 58 | + "kernel": ["rbf", "linear"], # Categorical |
| 59 | + } |
| 60 | +
|
| 61 | + optimizer = SmacRandomForest( |
| 62 | + param_space=param_space, |
| 63 | + n_iter=100, |
| 64 | + n_initial_points=10, # Random points before model-based search |
| 65 | + random_state=42, |
| 66 | + experiment=objective, |
| 67 | + ) |
| 68 | + best_params = optimizer.solve() |
| 69 | +
|
| 70 | +
|
| 71 | +SmacGaussianProcess |
| 72 | +------------------- |
| 73 | + |
| 74 | +Uses a Gaussian Process surrogate model (Matern 5/2 kernel) for sample-efficient |
| 75 | +optimization. Best suited for continuous parameter spaces. |
| 76 | + |
| 77 | +.. warning:: |
| 78 | + |
| 79 | + Gaussian Processes scale O(n^3) with observations. Not recommended for |
| 80 | + budgets exceeding 100 evaluations. For mixed or categorical spaces, |
| 81 | + use ``SmacRandomForest`` instead. |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + from hyperactive.opt.smac import SmacGaussianProcess |
| 86 | +
|
| 87 | + # Continuous parameters work best with GP |
| 88 | + param_space = { |
| 89 | + "learning_rate": (0.0001, 0.1), |
| 90 | + "weight_decay": (0.0, 0.1), |
| 91 | + } |
| 92 | +
|
| 93 | + optimizer = SmacGaussianProcess( |
| 94 | + param_space=param_space, |
| 95 | + n_iter=50, # GP is sample-efficient |
| 96 | + random_state=42, |
| 97 | + experiment=objective, |
| 98 | + ) |
| 99 | + best_params = optimizer.solve() |
| 100 | +
|
| 101 | +
|
| 102 | +SmacRandomSearch |
| 103 | +---------------- |
| 104 | + |
| 105 | +Pure random search without surrogate modeling. Useful as a baseline or for |
| 106 | +high-dimensional spaces where model-based methods struggle. |
| 107 | + |
| 108 | +.. code-block:: python |
| 109 | +
|
| 110 | + from hyperactive.opt.smac import SmacRandomSearch |
| 111 | +
|
| 112 | + optimizer = SmacRandomSearch( |
| 113 | + param_space=param_space, |
| 114 | + n_iter=100, |
| 115 | + random_state=42, |
| 116 | + experiment=objective, |
| 117 | + ) |
| 118 | + best_params = optimizer.solve() |
| 119 | +
|
| 120 | +
|
| 121 | +Common Parameters |
| 122 | +----------------- |
| 123 | + |
| 124 | +All SMAC optimizers share these parameters: |
| 125 | + |
| 126 | +.. list-table:: |
| 127 | + :header-rows: 1 |
| 128 | + :widths: 25 15 60 |
| 129 | + |
| 130 | + * - Parameter |
| 131 | + - Default |
| 132 | + - Description |
| 133 | + * - ``param_space`` |
| 134 | + - Required |
| 135 | + - Search space dictionary with parameter ranges |
| 136 | + * - ``n_iter`` |
| 137 | + - 100 |
| 138 | + - Number of optimization iterations |
| 139 | + * - ``max_time`` |
| 140 | + - None |
| 141 | + - Optional time limit in seconds |
| 142 | + * - ``random_state`` |
| 143 | + - None |
| 144 | + - Random seed for reproducibility |
| 145 | + * - ``deterministic`` |
| 146 | + - True |
| 147 | + - Whether objective function is deterministic |
| 148 | + * - ``initialize`` |
| 149 | + - None |
| 150 | + - Warm start configuration (see below) |
| 151 | + |
| 152 | + |
| 153 | +Parameter Space Definition |
| 154 | +-------------------------- |
| 155 | + |
| 156 | +SMAC optimizers support three parameter types: |
| 157 | + |
| 158 | +.. code-block:: python |
| 159 | +
|
| 160 | + param_space = { |
| 161 | + # Float range: both bounds must be float |
| 162 | + "learning_rate": (0.001, 0.1), |
| 163 | +
|
| 164 | + # Integer range: both bounds must be int |
| 165 | + "n_estimators": (10, 500), |
| 166 | +
|
| 167 | + # Categorical: list of choices |
| 168 | + "kernel": ["rbf", "linear", "poly"], |
| 169 | + } |
| 170 | +
|
| 171 | +.. note:: |
| 172 | + |
| 173 | + For ambiguous tuples like ``(1, 10)``, Python type determines the parameter |
| 174 | + type. Use ``(1, 10)`` for integer range and ``(1.0, 10.0)`` for float range. |
| 175 | + |
| 176 | + |
| 177 | +Warm Starting |
| 178 | +------------- |
| 179 | + |
| 180 | +Use warm starting to seed optimization with known good configurations: |
| 181 | + |
| 182 | +.. code-block:: python |
| 183 | +
|
| 184 | + optimizer = SmacRandomForest( |
| 185 | + param_space=param_space, |
| 186 | + n_iter=100, |
| 187 | + initialize={ |
| 188 | + "warm_start": [ |
| 189 | + {"C": 1.0, "gamma": 0.1, "kernel": "rbf"}, |
| 190 | + {"C": 10.0, "gamma": 0.01, "kernel": "linear"}, |
| 191 | + ] |
| 192 | + }, |
| 193 | + experiment=objective, |
| 194 | + ) |
| 195 | +
|
| 196 | +
|
| 197 | +When to Use SMAC Backend |
| 198 | +------------------------ |
| 199 | + |
| 200 | +The SMAC backend is useful when you need: |
| 201 | + |
| 202 | +- **State-of-the-art Bayesian optimization** with proven surrogate models |
| 203 | +- **Native handling of mixed parameter spaces** (Random Forest handles categorical parameters well) |
| 204 | +- **Sample-efficient optimization** for expensive function evaluations |
| 205 | +- **Hyperparameter optimization** following AutoML best practices |
| 206 | +- **Reproducible results** in scientific experiments |
| 207 | + |
| 208 | +Choose ``SmacRandomForest`` when: |
| 209 | + |
| 210 | +- Your search space has mixed parameter types |
| 211 | +- You have 50+ evaluations budget |
| 212 | +- Parameters interact in complex ways |
| 213 | + |
| 214 | +Choose ``SmacGaussianProcess`` when: |
| 215 | + |
| 216 | +- All parameters are continuous |
| 217 | +- Budget is small (10-50 evaluations) |
| 218 | +- You need uncertainty estimates |
| 219 | + |
| 220 | +Choose ``SmacRandomSearch`` when: |
| 221 | + |
| 222 | +- You need a baseline for comparison |
| 223 | +- Search space is high-dimensional (>20 parameters) |
| 224 | +- Evaluations are cheap and parallelizable |
| 225 | + |
| 226 | + |
| 227 | +References |
| 228 | +---------- |
| 229 | + |
| 230 | +- `SMAC3 Documentation <https://automl.github.io/SMAC3/main/>`_ |
| 231 | +- Lindauer, M., et al. (2022). SMAC3: A Versatile Bayesian Optimization |
| 232 | + Package for Hyperparameter Optimization. JMLR. |
0 commit comments