|
| 1 | +"""Module for ablation-based Hyperparameter Importance analysis. |
| 2 | +
|
| 3 | +This module provides the `AblationGame` class for analyzing hyperparameter |
| 4 | +interactions through ablation-based experiments. It leverages a surrogate model |
| 5 | +to estimate the impact of individual hyperparameters by systematically combining |
| 6 | +baseline and optimized configuration values. The `AblationGame` allows for |
| 7 | +understanding which hyperparameters contribute most to performance gains and |
| 8 | +identifying potential areas for further tuning. |
| 9 | +
|
| 10 | +The module is designed to work in conjunction with the `hypershap.games.abstract` |
| 11 | +module and relies on the `hypershap.task` module for defining the |
| 12 | +hyperparameter search space and surrogate model. |
| 13 | +
|
| 14 | +Key components include: |
| 15 | +
|
| 16 | +* **`AblationGame`**: A game class that evaluates the performance of |
| 17 | + configurations generated by blending baseline and optimized hyperparameter |
| 18 | + values, providing insights into individual hyperparameter contributions. |
| 19 | +
|
| 20 | +This module facilitates a deeper understanding of hyperparameter dependencies |
| 21 | +and guides optimization efforts by revealing the impact of each hyperparameter |
| 22 | +in a systematic and interpretable manner. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from typing import TYPE_CHECKING |
| 28 | + |
1 | 29 | import numpy as np |
2 | 30 |
|
3 | 31 | from hypershap.games.abstract import AbstractHPIGame |
4 | | -from hypershap.task import AblationExplanationTask |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from hypershap.task import AblationExplanationTask |
5 | 35 |
|
6 | 36 |
|
7 | 37 | class AblationGame(AbstractHPIGame): |
| 38 | + """The ablation game generates local explanations for hyperparameter configurations. |
| 39 | +
|
| 40 | + It does so by evaluating all potential ablation paths, switching from a baseline configuration |
| 41 | + to an optimized configuration value by value. It leverages a surrogate model to estimate |
| 42 | + performance based on blended configurations. |
| 43 | + """ |
| 44 | + |
8 | 45 | def __init__( |
9 | 46 | self, |
10 | 47 | explanation_task: AblationExplanationTask, |
11 | | - n_workers: int = 1, |
12 | | - verbose: bool = False, |
13 | | - ): |
14 | | - """ |
15 | | - The ablation game generates local explanations for hyperparameter configurations by evaluating all potential |
16 | | - ablation paths switching from a baseline configuration to an optimized configuration value by value. |
| 48 | + n_workers: int | None = None, |
| 49 | + verbose: bool | None = None, |
| 50 | + ) -> None: |
| 51 | + """Initialize an AblationGame. |
| 52 | +
|
17 | 53 | Args: |
18 | | - explanation_task (AblationExplanationTask): |
| 54 | + explanation_task (AblationExplanationTask): An instance of `AblationExplanationTask` |
| 55 | + providing access to the baseline configuration, configuration of interest, |
| 56 | + and the surrogate model for evaluation. |
| 57 | + n_workers (int | None): The number of worker threads to use for parallel |
| 58 | + evaluation of coalitions. Defaults to None, which disables parallelization. |
| 59 | + Using more workers can significantly speed up the computation of Shapley values. |
| 60 | + The maximum number of workers is capped by the number of coalitions. |
| 61 | + verbose (bool | None): A boolean indicating whether to print verbose messages |
| 62 | + during computation. Defaults to None. When set to True, the method prints |
| 63 | + debugging information and progress updates. |
| 64 | +
|
19 | 65 | """ |
20 | 66 | super().__init__(explanation_task, n_workers=n_workers, verbose=verbose) |
21 | 67 |
|
22 | 68 | def evaluate_single_coalition(self, coalition: np.ndarray) -> float: |
| 69 | + """Evaluate a single coalition (combination of baseline and optimized hyperparameters). |
| 70 | +
|
| 71 | + This method blends the baseline and optimized configurations based on the provided coalition, |
| 72 | + then uses the surrogate model to estimate the performance of the blended configuration. |
| 73 | +
|
| 74 | + Args: |
| 75 | + coalition (np.ndarray): A Boolean array indicating which hyperparameters should be taken from the |
| 76 | + baseline configuration (False) and which from the configuration of interest (True). |
| 77 | +
|
| 78 | + Returns: |
| 79 | + float: The performance score of the blended configuration as estimated by the surrogate model. |
| 80 | +
|
| 81 | + """ |
23 | 82 | baseline_cfg = self._get_explanation_task().baseline_config.get_array() |
24 | 83 | cfg_of_interest = self._get_explanation_task().config_of_interest.get_array() |
25 | 84 | blend = np.where(coalition == 0, baseline_cfg, cfg_of_interest) |
26 | | - coalition_value = self._get_explanation_task().surrogate_model.evaluate(blend) |
27 | | - return coalition_value |
| 85 | + return self._get_explanation_task().surrogate_model.evaluate(blend) |
28 | 86 |
|
29 | 87 | def _get_explanation_task(self) -> AblationExplanationTask: |
| 88 | + """Retrieve the explanation task associated with this ablation game. |
| 89 | +
|
| 90 | + This method simply returns the `explanation_task` attribute, which provides |
| 91 | + access to the baseline configuration, configuration of interest, and surrogate model. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + AblationExplanationTask: The explanation task associated with this ablation game. |
| 95 | +
|
| 96 | + """ |
30 | 97 | return self.explanation_task |
0 commit comments