MCPcopy Create free account
hub / github.com/modAL-python/modAL / mc_dropout_bald

Function mc_dropout_bald

modAL/dropout.py:19–68  ·  view source on GitHub ↗

Mc-Dropout bald query strategy. Returns the indexes of the instances with the largest BALD (Bayesian Active Learning by Disagreement) score calculated through the dropout cycles and the corresponding bald score. Based on the work of: Deep Bayesian Act

(classifier: BaseEstimator, X: modALinput, n_instances: int = 1,
                    random_tie_break: bool = False, dropout_layer_indexes: list = [],
                    num_cycles: int = 50, sample_per_forward_pass: int = 1000,
                    logits_adaptor: Callable[[
                        torch.tensor, modALinput], torch.tensor] = default_logits_adaptor,
                    **mc_dropout_kwargs,)

Source from the content-addressed store, hash-verified

17
18
19def mc_dropout_bald(classifier: BaseEstimator, X: modALinput, n_instances: int = 1,
20 random_tie_break: bool = False, dropout_layer_indexes: list = [],
21 num_cycles: int = 50, sample_per_forward_pass: int = 1000,
22 logits_adaptor: Callable[[
23 torch.tensor, modALinput], torch.tensor] = default_logits_adaptor,
24 **mc_dropout_kwargs,) -> np.ndarray:
25 """
26 Mc-Dropout bald query strategy. Returns the indexes of the instances with the largest BALD
27 (Bayesian Active Learning by Disagreement) score calculated through the dropout cycles
28 and the corresponding bald score.
29
30 Based on the work of:
31 Deep Bayesian Active Learning with Image Data.
32 (Yarin Gal, Riashat Islam, and Zoubin Ghahramani. 2017.)
33 Dropout as a Bayesian Approximation: Representing Model Uncer- tainty in Deep Learning.
34 (Yarin Gal and Zoubin Ghahramani. 2016.)
35 Bayesian Active Learning for Classification and Preference Learning.
36 (NeilHoulsby,FerencHusza ́r,ZoubinGhahramani,andMa ́te ́Lengyel. 2011.)
37
38 Args:
39 classifier: The classifier for which the labels are to be queried.
40 X: The pool of samples to query from.
41 n_instances: Number of samples to be queried.
42 random_tie_break: If True, shuffles utility scores to randomize the order. This
43 can be used to break the tie when the highest utility score is not unique.
44 dropout_layer_indexes: Indexes of the dropout layers which should be activated
45 Choose indices from : list(torch_model.modules())
46 num_cycles: Number of forward passes with activated dropout
47 sample_per_forward_pass: max. sample number for each forward pass.
48 The allocated RAM does mainly depend on this.
49 Small number --> small RAM allocation
50 logits_adaptor: Callable which can be used to adapt the output of a forward pass
51 to the required vector format for the vectorised metric functions
52 **uncertainty_measure_kwargs: Keyword arguments to be passed for the uncertainty
53 measure function.
54
55 Returns:
56 The indices of the instances from X chosen to be labelled;
57 The mc-dropout metric of the chosen instances;
58 """
59 predictions = get_predictions(
60 classifier, X, dropout_layer_indexes, num_cycles, sample_per_forward_pass, logits_adaptor)
61 # calculate BALD (Bayesian active learning divergence))
62
63 bald_scores = _bald_divergence(predictions)
64
65 if not random_tie_break:
66 return multi_argmax(bald_scores, n_instances=n_instances)
67
68 return shuffled_argmax(bald_scores, n_instances=n_instances)
69
70
71def mc_dropout_mean_st(classifier: BaseEstimator, X: modALinput, n_instances: int = 1,

Callers

nothing calls this directly

Calls 4

multi_argmaxFunction · 0.90
shuffled_argmaxFunction · 0.90
get_predictionsFunction · 0.85
_bald_divergenceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…