Optimizer
Objective
A class that encapsulates the logic for evaluating a model's performance using a
specified explainer, postprocessor, and metric within a given modality. The Objective
class is designed to be callable and can be used within an optimization framework
like Optuna to evaluate different configurations of explainers and postprocessors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
explainer |
Explainer
|
The explainer used to generate attributions for the model. |
required |
postprocessor |
PostProcessor
|
The postprocessor applied to the attributions generated by the explainer. |
required |
metric |
Metric
|
The metric used to evaluate the effectiveness of the postprocessed attributions. |
required |
modality |
Modality
|
The data modality (e.g., image, text) the model and explainer are operating on. |
required |
inputs |
Optional[TensorOrTupleOfTensors]
|
The input data to the model. Defaults to None. |
None
|
targets |
Optional[Tensor]
|
The target labels for the input data. Defaults to None. |
None
|
Source code in pnpxai/evaluator/optimizer/objectives.py
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 |
|
__call__(trial)
Executes the objective function within an optimization trial. This method is responsible for selecting the explainer and postprocessor based on the trial suggestions, performing the explanation and postprocessing, and evaluating the results using the specified metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trial |
Trial
|
The trial object from an optimization framework like Optuna. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The evaluation result of the metric after applying the explainer and |
float
|
postprocessor to the model's predictions. Returns |
|
float
|
results contain non-countable values like |
Source code in pnpxai/evaluator/optimizer/objectives.py
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 |
|
set_data(inputs, targets)
Sets both the input data and target labels for the objective.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input data to be used by the explainer. |
required |
targets |
Tensor
|
The target labels corresponding to the input data. |
required |
Returns:
Name | Type | Description |
---|---|---|
Objective |
The updated Objective instance. |
Source code in pnpxai/evaluator/optimizer/objectives.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
set_inputs(inputs)
Sets the input data for the objective.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input data to be used by the explainer. |
required |
Returns:
Name | Type | Description |
---|---|---|
Objective |
The updated Objective instance. |
Source code in pnpxai/evaluator/optimizer/objectives.py
55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
set_targets(targets)
Sets the target labels for the objective.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
targets |
Tensor
|
The target labels corresponding to the input data. |
required |
Returns:
Name | Type | Description |
---|---|---|
Objective |
The updated Objective instance. |
Source code in pnpxai/evaluator/optimizer/objectives.py
69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
suggest(trial, obj, modality, key=None, force_params=None)
A utility function that suggests parameters for a given object based on an optimization trial. The function recursively tunes the parameters of the object according to the modality (or modalities) provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trial |
Trial
|
The trial object from an optimization framework like Optuna, used to suggest parameters for tuning. |
required |
obj |
Any
|
The object whose parameters are being tuned. This object must implement
|
required |
modality |
Union[Modality, Tuple[Modality]]
|
The modality (e.g., image, text) or tuple of modalities the object is operating on. If multiple modalities are provided, the function handles multi-modal tuning. |
required |
key |
Optional[str]
|
An optional key to uniquely identify the set of parameters being tuned, useful for differentiating parameters in multi-modal scenarios. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Any |
The object with its parameters set according to the trial suggestions. |
Notes
- The function uses
map_suggest_method
to map the tuning method based on the method type provided in the tunables. - It supports multi-modal tuning, where different modalities may require different parameters to be tuned.
- For utility functions (
UtilFunction
), the function further tunes parameters based on the selected function from the modality.
Example
Assuming trial
is an instance of optuna.trial.Trial
, and explainer: Explainer
is an object
with tunable parameters, you can tune it as follows:
tuned_explainer = suggest(trial, explainer, modality)
Source code in pnpxai/evaluator/optimizer/suggestor.py
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 |
|
OptimizationOutput
dataclass
A dataclass used to store the results of an optimization process, including the chosen explainer, postprocessor, and the Optuna study object that was used during the optimization.
Attributes:
Name | Type | Description |
---|---|---|
explainer |
Explainer
|
The explainer object selected during the optimization process. This object is responsible for generating explanations or attributions for a model. |
postprocessor |
PostProcessor
|
The postprocessor object selected during the optimization process. This object is used to process the attributions generated by the explainer, often involving normalization or other transformations. |
study |
Study
|
The Optuna study object that was used to conduct the optimization. This object contains information about the optimization trials and results. |
Source code in pnpxai/evaluator/optimizer/types.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|