-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogME.py
151 lines (132 loc) · 5.15 KB
/
LogME.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
### The code has been modified from https://github.com/thuml/LogME ###
import warnings
import numpy as np
#@njit
def each_evidence(y_, f, fh, v, s, vh, N, D):
"""
compute the maximum evidence for each class
"""
epsilon = 1e-5
alpha = 1.0
beta = 1.0
lam = alpha / beta
tmp = (vh @ (f @ np.ascontiguousarray(y_)))
for _ in range(11):
# should converge after at most 10 steps
# typically converge after two or three steps
gamma = (s / (s + lam)).sum()
# A = v @ np.diag(alpha + beta * s) @ v.transpose() # no need to compute A
# A_inv = v @ np.diag(1.0 / (alpha + beta * s)) @ v.transpose() # no need to compute A_inv
m = v @ (tmp * beta / (alpha + beta * s))
alpha_de = (m * m).sum()
alpha = gamma / (alpha_de + epsilon)
beta_de = ((y_ - fh @ m) ** 2).sum()
beta = (N - gamma) / (beta_de + epsilon)
new_lam = alpha / beta
if np.abs(new_lam - lam) / lam < 0.01:
break
lam = new_lam
evidence = D / 2.0 * np.log(alpha) \
+ N / 2.0 * np.log(beta) \
- N / 2.0 * np.log(2 * np.pi) \
- alpha / 2.0 * (alpha_de + epsilon) \
- beta / 2.0 * (beta_de + epsilon) \
- 0.5 * np.sum(np.log(alpha + beta * s))
ed = [0.0 for i in range(6)]
ed[0] = D / 2.0 * np.log(alpha) /N
ed[1] = N / 2.0 * np.log(beta) /N
ed[2] = - N / 2.0 * np.log(2 * np.pi) /N
ed[3] = - alpha / 2.0 * (alpha_de + epsilon) /N
ed[4] = - beta / 2.0 * (beta_de + epsilon) /N
ed[5] = - 0.5 * np.sum(np.log(alpha + beta * s)) /N
return evidence / N, alpha, beta, m, ed
# use pseudo data to compile the function
# D = 20, N = 50
f_tmp = np.random.randn(20, 50).astype(np.float64)
each_evidence(np.random.randint(0, 2, 50).astype(np.float64), f_tmp, f_tmp.transpose(), np.eye(20, dtype=np.float64), np.ones(20, dtype=np.float64), np.eye(20, dtype=np.float64), 50, 20)
#@njit
def truncated_svd(x):
u, s, vh = np.linalg.svd(x.transpose() @ x)
s = np.sqrt(s)
u_times_sigma = x @ vh.transpose()
k = np.sum((s > 1e-10) * 1) # rank of f
s = s.reshape(-1, 1)
s = s[:k]
vh = vh[:k]
u = u_times_sigma[:, :k] / s.reshape(1, -1)
return u, s, vh
truncated_svd(np.random.randn(20, 10).astype(np.float64))
class LogME(object):
def __init__(self, regression=False, mean=None, std=None, img_size=128):
"""
:param regression: whether regression
"""
self.regression = regression
self.fitted = False
self.reset()
self.std = std
self.mean = mean
self.img_size = img_size
def reset(self):
self.num_dim = 0
self.alphas = [] # alpha for each class / dimension
self.betas = [] # beta for each class / dimension
# self.ms.shape --> [C, D]
self.ms = [] # m for each class / dimension
def _fit_icml(self, f: np.ndarray, y: np.ndarray):
"""
LogME calculation proposed in the ICML 2021 paper
"LogME: Practical Assessment of Pre-trained Models for Transfer Learning"
at http://proceedings.mlr.press/v139/you21b.html
"""
fh = f
f = f.transpose()
D, N = f.shape
v, s, vh = np.linalg.svd(f @ fh, full_matrices=True)
evidences = []
eds = []
self.num_dim = y.shape[1] if self.regression else int(y.max() + 1)
for i in range(self.num_dim):
y_ = y[:, i] if self.regression else (y == i).astype(np.float64)
evidence, alpha, beta, m, ed = each_evidence(y_, f, fh, v, s, vh, N, D)
evidences.append(evidence)
self.alphas.append(alpha)
self.betas.append(beta)
self.ms.append(m)
eds.append(ed)
self.ms = np.stack(self.ms)
mean_eds = np.mean(np.array(eds), axis=0)
self.dominate_term = (np.argmax(np.abs(mean_eds)), np.max(np.abs(mean_eds)), eds)
return np.mean(evidences), self.alphas, self.betas
_fit = _fit_icml
def fit(self, f: np.ndarray, y: np.ndarray):
"""
:param f: [N, F], feature matrix from pre-trained model
:param y: target labels.
For classification, y has shape [N] with element in [0, C_t).
For regression, y has shape [N, C] with C regression-labels
:return: LogME score (how well f can fit y directly)
"""
if self.fitted:
warnings.warn('re-fitting for new data. old parameters cleared.')
self.reset()
else:
self.fitted = True
f = f.astype(np.float64)
if self.regression:
y = y.astype(np.float64)
if len(y.shape) == 1:
y = y.reshape(-1, 1)
return self._fit(f, y)
def predict(self, f: np.ndarray):
"""
:param f: [N, F], feature matrix
:return: prediction, return shape [N, X]
"""
if not self.fitted:
raise RuntimeError("not fitted, please call fit first")
f = f.astype(np.float64)
logits = f @ self.ms.T
if self.regression:
return logits
return np.argmax(logits, axis=-1)