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

Function _bald_divergence

modAL/dropout.py:366–402  ·  view source on GitHub ↗

Calculates the bald divergence for each instance As it is explicitly formulated in: Deep Bayesian Active Learning with Image Data. (Yarin Gal, Riashat Islam, and Zoubin Ghahramani. 2017.) Args: proba: list with the predictions over th

(proba: list)

Source from the content-addressed store, hash-verified

364
365
366def _bald_divergence(proba: list) -> np.ndarray:
367 """
368 Calculates the bald divergence for each instance
369
370 As it is explicitly formulated in:
371 Deep Bayesian Active Learning with Image Data.
372 (Yarin Gal, Riashat Islam, and Zoubin Ghahramani. 2017.)
373
374 Args:
375 proba: list with the predictions over the dropout cycles
376 mask: mask to detect the padded classes (must be of same shape as elements in proba)
377 Return:
378 Returns the mean standard deviation of the dropout cycles over all classes.
379 """
380 proba_stacked = np.stack(proba, axis=len(proba[0].shape))
381
382 # entropy along dropout cycles
383 accumulated_entropy = entropy_sum(proba_stacked, axis=-1)
384 f_x = accumulated_entropy/len(proba)
385
386 # score sums along dropout cycles
387 accumulated_score = np.sum(proba_stacked, axis=-1)
388 average_score = accumulated_score/len(proba)
389 # expand dimension w/o data for entropy calculation
390 average_score = np.expand_dims(average_score, axis=-1)
391
392 # entropy over average prediction score
393 g_x = entropy_sum(average_score, axis=-1)
394
395 # entropy differences
396 diff = np.subtract(g_x, f_x)
397
398 # sum all dimensions of diff besides first dim (instances)
399 shaped = np.reshape(diff, (diff.shape[0], -1))
400
401 bald = np.sum(shaped, where=~np.isnan(shaped), axis=-1)
402 return bald
403
404
405def set_dropout_mode(model, dropout_layer_indexes: list, train_mode: bool):

Callers 1

mc_dropout_baldFunction · 0.85

Calls 1

entropy_sumFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…