-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_logme.py
236 lines (214 loc) · 8.54 KB
/
calculate_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import numpy as np
import pandas as pd
import warnings
from tqdm import tqdm
import numpy as np
from numba import njit
@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) \
- 0.5 * np.sum(np.log(alpha + beta * s)) \
- beta / 2.0 * (beta_de + epsilon) \
- alpha / 2.0 * (alpha_de + epsilon) \
- N / 2.0 * np.log(2 * np.pi)
return evidence / N, alpha, beta, m
# 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):
"""
:param regression: whether regression
"""
self.regression = regression
self.fitted = False
self.reset()
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 = []
self.num_dim = y.shape[1] if self.regression else int(y.max() + 1)
for i in tqdm(range(self.num_dim)):
y_ = y[:, i] if self.regression else (y == i).astype(np.float64)
evidence, alpha, beta, m = 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)
self.ms = np.stack(self.ms)
return np.mean(evidences)
def _fit_fixed_point(self, f: np.ndarray, y: np.ndarray):
"""
LogME calculation proposed in the arxiv 2021 paper
"Ranking and Tuning Pre-trained Models: A New Paradigm of Exploiting Model Hubs"
at https://arxiv.org/abs/2110.10545
"""
N, D = f.shape # k = min(N, D)
if N > D: # direct SVD may be expensive
u, s, vh = truncated_svd(f)
else:
u, s, vh = np.linalg.svd(f, full_matrices=False)
# u.shape = N x k
# s.shape = k
# vh.shape = k x D
s = s.reshape(-1, 1)
sigma = (s ** 2)
evidences = []
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)
y_ = y_.reshape(-1, 1)
x = u.T @ y_ # x has shape [k, 1], but actually x should have shape [N, 1]
x2 = x ** 2
res_x2 = (y_ ** 2).sum() - x2.sum() # if k < N, we compute sum of xi for 0 singular values directly
alpha, beta = 1.0, 1.0
for _ in range(11):
t = alpha / beta
gamma = (sigma / (sigma + t)).sum()
m2 = (sigma * x2 / ((t + sigma) ** 2)).sum()
res2 = (x2 / ((1 + sigma / t) ** 2)).sum() + res_x2
alpha = gamma / (m2 + 1e-5)
beta = (N - gamma) / (res2 + 1e-5)
t_ = alpha / beta
evidence = D / 2.0 * np.log(alpha) \
+ N / 2.0 * np.log(beta) \
- 0.5 * np.sum(np.log(alpha + beta * sigma)) \
- beta / 2.0 * res2 \
- alpha / 2.0 * m2 \
- N / 2.0 * np.log(2 * np.pi)
evidence /= N
if abs(t_ - t) / t <= 1e-3: # abs(t_ - t) <= 1e-5 or abs(1 / t_ - 1 / t) <= 1e-5:
break
evidence = D / 2.0 * np.log(alpha) \
+ N / 2.0 * np.log(beta) \
- 0.5 * np.sum(np.log(alpha + beta * sigma)) \
- beta / 2.0 * res2 \
- alpha / 2.0 * m2 \
- N / 2.0 * np.log(2 * np.pi)
evidence /= N
m = 1.0 / (t + sigma) * s * x
m = (vh.T @ m).reshape(-1)
evidences.append(evidence)
self.alphas.append(alpha)
self.betas.append(beta)
self.ms.append(m)
self.ms = np.stack(self.ms)
return np.mean(evidences)
_fit = _fit_fixed_point
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)
classes = {
"yes":0,
"no":1,
"up":2,
"down":3,
"left":4,
"right":5,
"on":6,
"off":7,
"stop":8,
"go":9,
"_unknown_":10,
"_silence_":11}
import pickle
target_path= "/home/virginiakm1988/s3prl/s3prl/result/logme/KS_ground_truth.txt"
target_df = pd.read_table(target_path, sep=" ",skiprows=0,header=None, engine='python')
labels = np.array([classes[item] for item in target_df[1]])
batch_size = 40
layer_num = 0
for layer_num in range(12):
path = f"/groups/virginiakm1988/KS_hubert_ranking_layer_{layer_num}.pkl"
with open(path, "rb") as fp: # Unpickling
feature_lst = pickle.load(fp)
features = np.array([feature.cpu().numpy().flatten() for feature in feature_lst[0]])
for i in range(1,batch_size):
features = np.concatenate((features, np.array([feature.cpu().numpy().flatten() for feature in feature_lst[i]])))
logme = LogME(regression=False)
score = logme._fit_fixed_point(features,labels[:32*batch_size])
print(layer_num,score)
#break
#print(np.array(feature_lst).shape)
#features_lst = []
#for feature in feature_lst:
# print(len(feature), len(feature[0]),np.array(feature).shape)
# # 32 , 49 ,
#print(np.array(features_lst).shape)