1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from typing import Type
import numpy as np
import pytest
import optuna
from optuna import samplers
from optuna.exceptions import ExperimentalWarning
from optuna.importance import BaseImportanceEvaluator
from optuna.importance import FanovaImportanceEvaluator
from optuna.importance import get_param_importances
from optuna.importance import MeanDecreaseImpurityImportanceEvaluator
from optuna.samplers import RandomSampler
from optuna.study import create_study
from optuna.testing.objectives import pruned_objective
from optuna.testing.storages import STORAGE_MODES
from optuna.testing.storages import StorageSupplier
from optuna.trial import Trial
evaluators: list[Type[BaseImportanceEvaluator]] = [
MeanDecreaseImpurityImportanceEvaluator,
FanovaImportanceEvaluator,
]
parametrize_evaluator = pytest.mark.parametrize("evaluator_init_func", evaluators)
@parametrize_evaluator
@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
def test_get_param_importance_target_is_none_and_study_is_multi_obj(
storage_mode: str,
evaluator_init_func: Callable[[], BaseImportanceEvaluator],
) -> None:
def objective(trial: Trial) -> tuple[float, float]:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 0, 3, step=1)
x4 = trial.suggest_int("x4", -3, 3)
x5 = trial.suggest_int("x5", 1, 5, log=True)
x6 = trial.suggest_categorical("x6", [1.0, 1.1, 1.2])
if trial.number % 2 == 0:
# Conditional parameters are ignored unless `params` is specified and is not `None`.
x7 = trial.suggest_float("x7", 0.1, 3)
value = x1**4 + x2 + x3 - x4**2 - x5 + x6
if trial.number % 2 == 0:
value += x7
return value, 0.0
with StorageSupplier(storage_mode) as storage:
study = create_study(directions=["minimize", "minimize"], storage=storage)
study.optimize(objective, n_trials=3)
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func())
@parametrize_evaluator
@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
@pytest.mark.parametrize("normalize", [True, False])
def test_get_param_importances(
storage_mode: str, evaluator_init_func: Callable[[], BaseImportanceEvaluator], normalize: bool
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 0, 3, step=1)
x4 = trial.suggest_int("x4", -3, 3)
x5 = trial.suggest_int("x5", 1, 5, log=True)
x6 = trial.suggest_categorical("x6", [1.0, 1.1, 1.2])
if trial.number % 2 == 0:
# Conditional parameters are ignored unless `params` is specified and is not `None`.
x7 = trial.suggest_float("x7", 0.1, 3)
value = x1**4 + x2 + x3 - x4**2 - x5 + x6
if trial.number % 2 == 0:
value += x7
return value
with StorageSupplier(storage_mode) as storage:
study = create_study(storage=storage, sampler=samplers.RandomSampler())
study.optimize(objective, n_trials=3)
param_importance = get_param_importances(
study, evaluator=evaluator_init_func(), normalize=normalize
)
assert isinstance(param_importance, dict)
assert len(param_importance) == 6
assert all(
param_name in param_importance for param_name in ["x1", "x2", "x3", "x4", "x5", "x6"]
)
prev_importance = float("inf")
for param_name, importance in param_importance.items():
assert isinstance(param_name, str)
assert isinstance(importance, float)
assert importance <= prev_importance
prev_importance = importance
# Sanity check for param importances
assert all(0 <= x < float("inf") for x in param_importance.values())
if normalize:
assert np.isclose(sum(param_importance.values()), 1.0)
@parametrize_evaluator
@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
@pytest.mark.parametrize("params", [[], ["x1"], ["x1", "x3"], ["x1", "x4"]])
@pytest.mark.parametrize("normalize", [True, False])
def test_get_param_importances_with_params(
storage_mode: str,
params: list[str],
evaluator_init_func: Callable[[], BaseImportanceEvaluator],
normalize: bool,
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 0, 3, step=1)
if trial.number % 2 == 0:
x4 = trial.suggest_float("x4", 0.1, 3)
value = x1**4 + x2 + x3
if trial.number % 2 == 0:
value += x4
return value
with StorageSupplier(storage_mode) as storage:
study = create_study(storage=storage)
study.optimize(objective, n_trials=10)
param_importance = get_param_importances(
study, evaluator=evaluator_init_func(), params=params, normalize=normalize
)
assert isinstance(param_importance, dict)
assert len(param_importance) == len(params)
assert all(param in param_importance for param in params)
for param_name, importance in param_importance.items():
assert isinstance(param_name, str)
assert isinstance(importance, float)
# Sanity check for param importances
assert all(0 <= x < float("inf") for x in param_importance.values())
if normalize:
assert len(param_importance) == 0 or np.isclose(sum(param_importance.values()), 1.0)
def test_get_param_importances_unnormalized_experimental() -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
return x1**2
study = create_study()
study.optimize(objective, n_trials=4)
with pytest.warns(ExperimentalWarning):
get_param_importances(study, normalize=False)
@parametrize_evaluator
@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
@pytest.mark.parametrize("normalize", [True, False])
def test_get_param_importances_with_target(
storage_mode: str, evaluator_init_func: Callable[[], BaseImportanceEvaluator], normalize: bool
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 0, 3, step=1)
if trial.number % 2 == 0:
x4 = trial.suggest_float("x4", 0.1, 3)
value = x1**4 + x2 + x3
if trial.number % 2 == 0:
value += x4
return value
with StorageSupplier(storage_mode) as storage:
study = create_study(storage=storage)
study.optimize(objective, n_trials=3)
param_importance = get_param_importances(
study,
evaluator=evaluator_init_func(),
target=lambda t: t.params["x1"] + t.params["x2"],
normalize=normalize,
)
assert isinstance(param_importance, dict)
assert len(param_importance) == 3
assert all(param_name in param_importance for param_name in ["x1", "x2", "x3"])
prev_importance = float("inf")
for param_name, importance in param_importance.items():
assert isinstance(param_name, str)
assert isinstance(importance, float)
assert importance <= prev_importance
prev_importance = importance
# Sanity check for param importances
assert all(0 <= x < float("inf") for x in param_importance.values())
if normalize:
assert np.isclose(sum(param_importance.values()), 1.0)
@parametrize_evaluator
def test_get_param_importances_invalid_empty_study(
evaluator_init_func: Callable[[], BaseImportanceEvaluator]
) -> None:
study = create_study()
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func())
study.optimize(pruned_objective, n_trials=3)
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func())
@parametrize_evaluator
def test_get_param_importances_invalid_single_trial(
evaluator_init_func: Callable[[], BaseImportanceEvaluator]
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
return x1**2
study = create_study()
study.optimize(objective, n_trials=1)
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func())
@parametrize_evaluator
def test_get_param_importances_invalid_no_completed_trials_params(
evaluator_init_func: Callable[[], BaseImportanceEvaluator]
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
if trial.number % 2 == 0:
_ = trial.suggest_float("x2", 0.1, 3, log=True)
raise optuna.TrialPruned
return x1**2
study = create_study()
study.optimize(objective, n_trials=3)
# None of the trials with `x2` are completed.
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func(), params=["x2"])
# None of the trials with `x2` are completed. Adding "x1" should not matter.
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func(), params=["x1", "x2"])
# None of the trials contain `x3`.
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func(), params=["x3"])
@parametrize_evaluator
def test_get_param_importances_invalid_dynamic_search_space_params(
evaluator_init_func: Callable[[], BaseImportanceEvaluator]
) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, trial.number + 0.1)
return x1**2
study = create_study()
study.optimize(objective, n_trials=3)
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator_init_func(), params=["x1"])
@parametrize_evaluator
def test_get_param_importances_empty_search_space(
evaluator_init_func: Callable[[], BaseImportanceEvaluator]
) -> None:
def objective(trial: Trial) -> float:
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 1, 1)
return 4 * x**2 + 4 * y**2
study = create_study()
study.optimize(objective, n_trials=3)
param_importance = get_param_importances(study, evaluator=evaluator_init_func())
assert len(param_importance) == 2
assert all([param in param_importance for param in ["x", "y"]])
assert param_importance["x"] > 0.0
assert param_importance["y"] == 0.0
@parametrize_evaluator
def test_importance_evaluator_seed(evaluator_init_func: Any) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 2, 4, log=True)
return x1 + x2 * x3
study = create_study(sampler=RandomSampler(seed=0))
study.optimize(objective, n_trials=3)
evaluator = evaluator_init_func(seed=2)
param_importance = evaluator.evaluate(study)
evaluator = evaluator_init_func(seed=2)
param_importance_same_seed = evaluator.evaluate(study)
assert param_importance == param_importance_same_seed
evaluator = evaluator_init_func(seed=3)
param_importance_different_seed = evaluator.evaluate(study)
assert param_importance != param_importance_different_seed
@parametrize_evaluator
def test_importance_evaluator_with_target(evaluator_init_func: Any) -> None:
def objective(trial: Trial) -> float:
x1 = trial.suggest_float("x1", 0.1, 3)
x2 = trial.suggest_float("x2", 0.1, 3, log=True)
x3 = trial.suggest_float("x3", 2, 4, log=True)
return x1 + x2 * x3
# Assumes that `seed` can be fixed to reproduce identical results.
study = create_study(sampler=RandomSampler(seed=0))
study.optimize(objective, n_trials=3)
evaluator = evaluator_init_func(seed=0)
param_importance = evaluator.evaluate(study)
param_importance_with_target = evaluator.evaluate(
study,
target=lambda t: t.params["x1"] + t.params["x2"],
)
assert param_importance != param_importance_with_target
|