Heuristics
Heuristics take a set of predictions and output an uncertainty value. They are agnostic to the method used for predicting, so they work with MC sampling and Ensembles.
Example
Using BALD, we can compute the uncertainty of many predictions.
import numpy as np
from baal.active.heuristics import BALD
# output from ModelWrapper.predict_on_dataset with shape [1000, num_classes, 20]
predictions: np.ndarray = ...
# To get the full uncertainty score
uncertainty = BALD().compute_score(predictions)
# To get ranks
most_uncertain = BALD()(predictions)
# If you wish to mix BALD and Uniform sampling,
# you can modify the `shuffle_prop` parameter.
BALD(shuffle_prop=0.1)
# When working with Sequence or Segmentation models, you can specify how to aggregate
# values using the "reduction" parameter.
BALD(reduction="mean")
API
baal.active.heuristics.AbstractHeuristic
Abstract class that defines a Heuristic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shuffle_prop |
float
|
shuffle proportion. |
DEPRECATED
|
reverse |
bool
|
True if the most uncertain sample has the highest value. |
False
|
reduction |
Union[str, Callable]
|
Reduction used after computing the score. |
'none'
|
Source code in baal/active/heuristics/heuristics.py
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 |
|
__call__(predictions)
Rank the predictions according to their uncertainties.
Only return the scores and not the associated uncertainties.
compute_score(predictions)
Compute the score according to the heuristic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
Array of predictions |
required |
Returns:
Type | Description |
---|---|
Array of scores. |
get_ranks(predictions)
Rank the predictions according to their uncertainties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
[batch_size, C, ..., Iterations] |
required |
Returns:
Type | Description |
---|---|
Ranked index according to the uncertainty (highest to lowes). |
|
Scores for all predictions. |
Source code in baal/active/heuristics/heuristics.py
get_uncertainties(predictions)
Get the uncertainties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
Array of predictions |
required |
Returns:
Type | Description |
---|---|
Array of uncertainties |
Source code in baal/active/heuristics/heuristics.py
get_uncertainties_generator(predictions)
Compute the score according to the heuristic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Iterable
|
Generator of predictions |
required |
Returns:
Type | Description |
---|---|
Array of scores. |
Source code in baal/active/heuristics/heuristics.py
reorder_indices(scores)
Order indices given their uncertainty score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scores |
ndarray / List[ndarray]
|
Array of uncertainties or list of arrays. |
required |
Returns:
Type | Description |
---|---|
ordered index according to the uncertainty (highest to lowes). |
Source code in baal/active/heuristics/heuristics.py
baal.active.heuristics.BALD
Bases: AbstractHeuristic
Sort by the highest acquisition function value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shuffle_prop |
float
|
Amount of noise to put in the ranking. Helps with selection bias (default: 0.0). |
DEPRECATED
|
reduction |
Union[str, callable]
|
function that aggregates the results (default: 'none`). |
'none'
|
References
https://arxiv.org/abs/1703.02910
Source code in baal/active/heuristics/heuristics.py
compute_score(predictions)
Compute the score according to the heuristic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
ndarray
|
Array of predictions |
required |
Returns:
Type | Description |
---|---|
Array of scores. |
Source code in baal/active/heuristics/heuristics.py
baal.active.heuristics.Random
Bases: Precomputed
Random heuristic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shuffle_prop |
float
|
UNUSED |
DEPRECATED
|
reduction |
Union[str, callable]
|
UNUSED. |
'none'
|
seed |
Optional[int]
|
If provided, will seed the random generator. |
None
|
Source code in baal/active/heuristics/heuristics.py
baal.active.heuristics.Entropy
Bases: AbstractHeuristic
Sort by the highest entropy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shuffle_prop |
float
|
Amount of noise to put in the ranking. Helps with selection bias (default: 0.0). |
DEPRECATED
|
reduction |
Union[str, callable]
|
function that aggregates the results (default: |
'none'
|