-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject2_util.py
63 lines (45 loc) · 2.12 KB
/
project2_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
import abc
import warnings
import numpy as np
import torch
def ece(predicted_probabilities: np.ndarray, labels: np.ndarray, n_bins: int = 30) -> float:
"""
Computes the Expected Calibration Error (ECE).
:n_bins: Number of bins for histogram binning
:return: ECE score as a float
"""
num_samples, num_classes = predicted_probabilities.shape
predictions = np.argmax(predicted_probabilities, axis=1)
prediction_confidences = predicted_probabilities[range(num_samples), predictions]
# Use uniform bins on the range of probabilities, i.e. closed interval [0.,1.]
bin_upper_edges = np.histogram_bin_edges([], bins=n_bins, range=(0., 1.))
bin_upper_edges = bin_upper_edges[1:]
probs_as_bin_num = np.digitize(prediction_confidences, bin_upper_edges)
sums_per_bin = np.bincount(probs_as_bin_num, minlength=n_bins, weights=prediction_confidences)
sums_per_bin = sums_per_bin.astype(np.float32)
total_per_bin = np.bincount(probs_as_bin_num, minlength=n_bins) \
+ np.finfo(sums_per_bin.dtype).eps
avg_prob_per_bin = sums_per_bin / total_per_bin
onehot_labels = np.eye(num_classes)[labels]
accuracies = onehot_labels[range(num_samples), predictions]
accuracies_per_bin = np.bincount(probs_as_bin_num, weights=accuracies, minlength=n_bins) / total_per_bin
prob_of_being_in_a_bin = total_per_bin / float(num_samples)
ece_ret = np.abs(accuracies_per_bin - avg_prob_per_bin) * prob_of_being_in_a_bin
ece_ret = np.sum(ece_ret)
return float(ece_ret)
class ParameterDistribution(torch.nn.Module, metaclass=abc.ABCMeta):
"""
Abstract class that models a distribution over model parameters,
usable for Bayes by Backprop.
"""
def __init__(self):
super().__init__()
@abc.abstractmethod
def log_likelihood(self, values: torch.Tensor) -> torch.Tensor:
pass
@abc.abstractmethod
def sample(self) -> torch.Tensor:
pass
def forward(self, values: torch.Tensor) -> torch.Tensor:
warnings.warn('ParameterDistribution should not be called! Use its explicit methods!')
return self.log_likelihood(values)