fl_server_ai.uncertainty ¶
Modules:
| Name | Description |
|---|---|
base | |
ensemble | |
mc_dropout | |
method | |
none | |
swag | |
Classes:
| Name | Description |
|---|---|
Ensemble | Ensemble uncertainty estimation. |
MCDropout | Monte Carlo (MC) Dropout Uncertainty Estimation |
NoneUncertainty | Empty uncertainty estimation when no specific uncertainty method is used. |
SWAG | Stochastic Weight Averaging Gaussian (SWAG) uncertainty estimation. |
UncertaintyBase | Abstract base class for uncertainty estimation. |
Functions:
| Name | Description |
|---|---|
get_uncertainty_class | Get uncertainty class associated with a given Model, Training, or UncertaintyMethod object. |
Attributes¶
__all__ module-attribute ¶
__all__ = ['get_uncertainty_class', 'Ensemble', 'MCDropout', 'NoneUncertainty', 'SWAG', 'UncertaintyBase']
Classes¶
Ensemble ¶
Bases: UncertaintyBase
flowchart TD
fl_server_ai.uncertainty.Ensemble[Ensemble]
fl_server_ai.uncertainty.base.UncertaintyBase[UncertaintyBase]
fl_server_ai.uncertainty.base.UncertaintyBase --> fl_server_ai.uncertainty.Ensemble
click fl_server_ai.uncertainty.Ensemble href "" "fl_server_ai.uncertainty.Ensemble"
click fl_server_ai.uncertainty.base.UncertaintyBase href "" "fl_server_ai.uncertainty.base.UncertaintyBase"
Ensemble uncertainty estimation.
Methods:
| Name | Description |
|---|---|
prediction | |
Source code in fl_server_ai/uncertainty/ensemble.py
Functions¶
prediction classmethod ¶
Source code in fl_server_ai/uncertainty/ensemble.py
MCDropout ¶
Bases: UncertaintyBase
flowchart TD
fl_server_ai.uncertainty.MCDropout[MCDropout]
fl_server_ai.uncertainty.base.UncertaintyBase[UncertaintyBase]
fl_server_ai.uncertainty.base.UncertaintyBase --> fl_server_ai.uncertainty.MCDropout
click fl_server_ai.uncertainty.MCDropout href "" "fl_server_ai.uncertainty.MCDropout"
click fl_server_ai.uncertainty.base.UncertaintyBase href "" "fl_server_ai.uncertainty.base.UncertaintyBase"
Monte Carlo (MC) Dropout Uncertainty Estimation
Requirements:
- model with dropout layers
- T, number of samples per input (number of monte-carlo samples/forward passes)
References:
- Paper: Understanding Measures of Uncertainty for Adversarial Example Detection https://arxiv.org/abs/1803.08533
- Code inspiration: https://github.com/lsgos/uncertainty-adversarial-paper/tree/master
Methods:
| Name | Description |
|---|---|
prediction | |
Source code in fl_server_ai/uncertainty/mc_dropout.py
Functions¶
prediction classmethod ¶
Source code in fl_server_ai/uncertainty/mc_dropout.py
NoneUncertainty ¶
Bases: UncertaintyBase
flowchart TD
fl_server_ai.uncertainty.NoneUncertainty[NoneUncertainty]
fl_server_ai.uncertainty.base.UncertaintyBase[UncertaintyBase]
fl_server_ai.uncertainty.base.UncertaintyBase --> fl_server_ai.uncertainty.NoneUncertainty
click fl_server_ai.uncertainty.NoneUncertainty href "" "fl_server_ai.uncertainty.NoneUncertainty"
click fl_server_ai.uncertainty.base.UncertaintyBase href "" "fl_server_ai.uncertainty.base.UncertaintyBase"
Empty uncertainty estimation when no specific uncertainty method is used.
This class does not calculate any uncertainty and only returns the prediction with an empty uncertainty dictionary.
Methods:
| Name | Description |
|---|---|
prediction | |
Source code in fl_server_ai/uncertainty/none.py
SWAG ¶
Bases: UncertaintyBase
flowchart TD
fl_server_ai.uncertainty.SWAG[SWAG]
fl_server_ai.uncertainty.base.UncertaintyBase[UncertaintyBase]
fl_server_ai.uncertainty.base.UncertaintyBase --> fl_server_ai.uncertainty.SWAG
click fl_server_ai.uncertainty.SWAG href "" "fl_server_ai.uncertainty.SWAG"
click fl_server_ai.uncertainty.base.UncertaintyBase href "" "fl_server_ai.uncertainty.base.UncertaintyBase"
Stochastic Weight Averaging Gaussian (SWAG) uncertainty estimation.
Methods:
| Name | Description |
|---|---|
prediction | |
Source code in fl_server_ai/uncertainty/swag.py
Functions¶
prediction classmethod ¶
Source code in fl_server_ai/uncertainty/swag.py
UncertaintyBase ¶
Bases: ABC
flowchart TD
fl_server_ai.uncertainty.UncertaintyBase[UncertaintyBase]
click fl_server_ai.uncertainty.UncertaintyBase href "" "fl_server_ai.uncertainty.UncertaintyBase"
Abstract base class for uncertainty estimation.
This class defines the interface for uncertainty estimation in federated learning.
- API Reference
fl_server_aiuncertainty
- API Reference
fl_server_aiuncertainty
Methods:
| Name | Description |
|---|---|
expected_entropy | Calculate the mean entropy of the predictive distribution across the MC samples. |
get_options | Get uncertainty options from training options. |
interpret | Interpret the different network (model) outputs and calculate the uncertainty. |
mutual_information | Calculate the BALD (Bayesian Active Learning by Disagreement) of a model; |
prediction | Make a prediction using the given input and model. |
predictive_entropy | Calculate the entropy of the mean predictive distribution across the MC samples. |
to_json | Convert the given inference and uncertainty data to a JSON string. |
Source code in fl_server_ai/uncertainty/base.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 | |
Functions¶
expected_entropy classmethod ¶
expected_entropy(predictions: Tensor) -> Tensor
Calculate the mean entropy of the predictive distribution across the MC samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | predictions of shape (n_mc x batch_size x n_classes) | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: mean entropy of the predictive distribution |
Source code in fl_server_ai/uncertainty/base.py
get_options classmethod ¶
Get uncertainty options from training options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Model | Training | The Model or Training object to retrieve options for. | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dict[str, Any]: Uncertainty options. |
Raises:
| Type | Description |
|---|---|
TypeError | If the given object is not a Model or Training. |
Source code in fl_server_ai/uncertainty/base.py
interpret classmethod ¶
Interpret the different network (model) outputs and calculate the uncertainty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | multiple network (model) outputs (N, batch_size, n_classes) | required |
Return
Tuple[torch.Tensor, Dict[str, Any]]: inference and uncertainty
Source code in fl_server_ai/uncertainty/base.py
mutual_information classmethod ¶
mutual_information(predictions: Tensor) -> Tensor
Calculate the BALD (Bayesian Active Learning by Disagreement) of a model; the difference between the mean of the entropy and the entropy of the mean of the predicted distribution on the predictions. This method is also sometimes referred to as the mutual information (MI).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | predictions of shape (n_mc x batch_size x n_classes) | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: difference between the mean of the entropy and the entropy of the mean of the predicted distribution |
Source code in fl_server_ai/uncertainty/base.py
prediction abstractmethod classmethod ¶
Make a prediction using the given input and model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | The input to make a prediction for. | required |
| Model | The model to use for making the prediction. | required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, dict[str, Any]] | Tuple[torch.Tensor, Dict[str, Any]]: The prediction and any associated uncertainty. |
Source code in fl_server_ai/uncertainty/base.py
predictive_entropy classmethod ¶
predictive_entropy(predictions: Tensor) -> Tensor
Calculate the entropy of the mean predictive distribution across the MC samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | predictions of shape (n_mc x batch_size x n_classes) | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: entropy of the mean predictive distribution |
Source code in fl_server_ai/uncertainty/base.py
to_json classmethod ¶
to_json(inference: Tensor, uncertainty: dict[str, Any] = {}, **json_kwargs) -> str
Convert the given inference and uncertainty data to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | The inference to convert. | required |
| dict[str, Any] | The uncertainty to convert. | {} |
| Additional keyword arguments to pass to | {} |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | A JSON string representation of the given inference and uncertainty data. |
Source code in fl_server_ai/uncertainty/base.py
Functions¶
get_uncertainty_class ¶
get_uncertainty_class(value: Model) -> Type[UncertaintyBase]
get_uncertainty_class(value: Training) -> Type[UncertaintyBase]
get_uncertainty_class(value: UncertaintyMethod) -> Type[UncertaintyBase]
get_uncertainty_class(value: Model | Training | UncertaintyMethod) -> Type[UncertaintyBase]
Get uncertainty class associated with a given Model, Training, or UncertaintyMethod object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Model | Training | UncertaintyMethod | The object to retrieve the uncertainty class for. | required |
Returns:
| Type | Description |
|---|---|
Type[UncertaintyBase] | Type[UncertaintyBase]: The uncertainty class associated with the given object. |
Raises:
| Type | Description |
|---|---|
ValueError | If the given object is not a Model, Training, or UncertaintyMethod, or if the uncertainty method associated with the object is unknown. |