-
Notifications
You must be signed in to change notification settings - Fork 5
/
ensemble_ts.py
152 lines (118 loc) · 4.67 KB
/
ensemble_ts.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Jize Zhang
See : https://github.com/zhang64-llnl/Mix-n-Match-Calibration/blob/master/util_calibration.py
"""
import numpy as np
from scipy import optimize
from sklearn.isotonic import IsotonicRegression
"""
auxiliary functions for optimizing the temperature (scaling approaches) and weights of ensembles
*args include logits and labels from the calibration dataset:
"""
def mse_t(t, *args):
## find optimal temperature with MSE loss function
logit, label = args
logit = logit / t
n = np.sum(np.exp(logit), 1)
p = np.exp(logit) / n[:, None]
mse = np.mean((p - label) ** 2)
return mse
def ll_t(t, *args):
## find optimal temperature with Cross-Entropy loss function
logit, label = args
logit = logit / t
n = np.sum(np.exp(logit), 1)
p = np.clip(np.exp(logit) / n[:, None], 1e-20, 1 - 1e-20)
N = p.shape[0]
ce = -np.sum(label * np.log(p)) / N
return ce
def mse_w(w, *args):
## find optimal weight coefficients with MSE loss function
p0, p1, p2, label = args
p = w[0] * p0 + w[1] * p1 + w[2] * p2
p = p / np.sum(p, 1)[:, None]
mse = np.mean((p - label) ** 2)
return mse
def ll_w(w, *args):
## find optimal weight coefficients with Cros-Entropy loss function
p0, p1, p2, label = args
p = (w[0] * p0 + w[1] * p1 + w[2] * p2)
N = p.shape[0]
ce = -np.sum(label * np.log(p)) / N
return ce
##### Ftting Temperature Scaling
def temperature_scaling(logit, label, loss):
bnds = ((0.05, 5.0),)
if loss == 'ce':
t = optimize.minimize(ll_t, 1.0, args=(logit, label), method='L-BFGS-B', bounds=bnds, tol=1e-12,
options={'disp': False})
if loss == 'mse':
t = optimize.minimize(mse_t, 1.0, args=(logit, label), method='L-BFGS-B', bounds=bnds, tol=1e-12,
options={'disp': False})
t = t.x
return t
##### Ftting Enseble Temperature Scaling
def ensemble_scaling(logit, label, loss, t, n_class):
p1 = np.exp(logit) / np.sum(np.exp(logit), 1)[:, None]
logit = logit / t
p0 = np.exp(logit) / np.sum(np.exp(logit), 1)[:, None]
p2 = np.ones_like(p0) / n_class
bnds_w = ((0.0, 1.0), (0.0, 1.0), (0.0, 1.0),)
def my_constraint_fun(x):
return np.sum(x) - 1
constraints = {"type": "eq", "fun": my_constraint_fun, }
if loss == 'ce':
w = optimize.minimize(ll_w, (1.0, 0.0, 0.0), args=(p0, p1, p2, label), method='SLSQP', constraints=constraints,
bounds=bnds_w, tol=1e-12, options={'disp': False})
if loss == 'mse':
w = optimize.minimize(mse_w, (1.0, 0.0, 0.0), args=(p0, p1, p2, label), method='SLSQP', constraints=constraints,
bounds=bnds_w, tol=1e-12, options={'disp': False})
w = w.x
return w
"""
Calibration:
Input: uncalibrated logits, temperature (and weight)
Output: calibrated prediction probabilities
"""
##### Calibration: Temperature Scaling with MSE
def ts_calibrate(logit, label, logit_eval, loss):
t = temperature_scaling(logit, label, loss)
print("temperature = " + str(t))
logit_eval = logit_eval / t
p = np.exp(logit_eval) / np.sum(np.exp(logit_eval), 1)[:, None]
return p
##### Calibration: Ensemble Temperature Scaling
def ets_calibrate(logit, label, n_class, loss='mse'):
t = temperature_scaling(logit, label, loss='mse') # loss can change to 'ce'
#print("temperature = " + str(t))
w = ensemble_scaling(logit, label, 'mse', t, n_class)
#print("weight = " + str(w))
return t, w
"""
p1 = np.exp(logit_eval) / np.sum(np.exp(logit_eval), 1)[:, None]
logit_eval = logit_eval / t
p0 = np.exp(logit_eval) / np.sum(np.exp(logit_eval), 1)[:, None]
p2 = np.ones_like(p0) / n_class
p = w[0] * p0 + w[1] * p1 + w[2] * p2
return p
"""
##### Calibration: Isotonic Regression (Multi-class)
def mir_calibrate(logit, label, logit_eval):
p = np.exp(logit) / np.sum(np.exp(logit), 1)[:, None]
p_eval = np.exp(logit_eval) / np.sum(np.exp(logit_eval), 1)[:, None]
ir = IsotonicRegression(out_of_bounds='clip')
y_ = ir.fit_transform(p.flatten(), (label.flatten()))
yt_ = ir.predict(p_eval.flatten())
p = yt_.reshape(logit_eval.shape) + 1e-9 * p_eval
return p
def irova_calibrate(logit, label, logit_eval):
p = np.exp(logit) / np.sum(np.exp(logit), 1)[:, None]
p_eval = np.exp(logit_eval) / np.sum(np.exp(logit_eval), 1)[:, None]
for ii in range(p_eval.shape[1]):
ir = IsotonicRegression(out_of_bounds='clip')
y_ = ir.fit_transform(p[:, ii], label[:, ii])
p_eval[:, ii] = ir.predict(p_eval[:, ii]) + 1e-9 * p_eval[:, ii]
return p_eval
return p_eval