-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthetic_experiment_imbalanced.py
274 lines (236 loc) · 10.7 KB
/
synthetic_experiment_imbalanced.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import torch
import torch.nn as nn
import argparse
import numpy as np
import torch.nn.functional as F
import torch.optim as optim
import torch.optim.lr_scheduler as lrs
from tqdm import tqdm
import scipy.linalg as scilin
import utils
import math
from sympy import Symbol, solve, S
NUM_CLASS_SAMPLES = [200, 100, 50, 50]
NUM_SAMPLES = sum(NUM_CLASS_SAMPLES)
class SyntheticModel(nn.Module):
def __init__(self, hidden, num_classes, depth_linear, NUM_SAMPLES, weight_decay, feature_decay):
super().__init__()
self.weight_decay = weight_decay
self.feature_decay = feature_decay
self.depth_linear = depth_linear
self.H = nn.Parameter(torch.normal(0, 0.1, size=(
NUM_SAMPLES, hidden)), requires_grad=True)
fc = []
for i in range(depth_linear-1):
fc += [nn.Linear(hidden, hidden, bias=False)]
fc += [nn.Linear(hidden, num_classes, bias=False)]
self.fc = nn.Sequential(*fc)
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean= 0, std=0.1)
def forward(self, Y):
features_list = [self.H.detach().clone()]
out = self.fc[0](self.H)
if len(self.fc) > 1:
features_list.append(out.detach().clone())
for i in range(1, len(self.fc)-1):
out = self.fc[i](out)
features_list.append(out.detach().clone())
out = self.fc[len(self.fc)-1](out)
loss_mse = torch.norm(out - Y, p='fro') ** 2 / (2*NUM_SAMPLES)
criterion = nn.CrossEntropyLoss()
Y_label = torch.argmax(Y, dim=1)
loss_CE = criterion(out, Y_label)
loss_w = self.weight_decay / 2 * \
sum([torch.sum(self.fc[i].weight**2)
for i in range(self.depth_linear)])
loss_h = self.feature_decay / 2 * torch.sum(self.H ** 2)
loss = loss_mse + loss_w + loss_h
features_list.reverse()
return out, loss, features_list
def compute_s(weight_decay, feature_decay_rate, depth, device):
c = 1
s_array = torch.zeros(len(NUM_CLASS_SAMPLES), device=device)
x = compute_x(NUM_CLASS_SAMPLES, depth, weight_decay, feature_decay_rate, device=device)
s_array = pow(NUM_SAMPLES*feature_decay_rate *
pow(x, depth) / c, 1/(2*depth))
print('S', s_array)
return s_array
def compute_x(NUM_CLASS_SAMPLES: list, M: float, lambda_weight: float, lambda_feature: float, device):
N = sum(NUM_CLASS_SAMPLES)
b = M * N * math.pow(N * math.pow(lambda_weight, M) * lambda_feature, 1/M)
x = Symbol('x', real=True)
result = torch.empty(len(NUM_CLASS_SAMPLES), device=device)
for i in range(len(NUM_CLASS_SAMPLES)):
result[i] = torch.from_numpy(np.array(max(solve(b / NUM_CLASS_SAMPLES[i] - (M * x**(M-1))/((x ** M + 1)**2), x, rational=False)), dtype=np.float32)).to(device)
if result[i] < 0:
result[i] = 0
return result
def compute_accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def compute_info(model, Y, targets):
mu_G_list = [0] * (len(model.fc))
pairs = [(i, 0) for i in range(len(NUM_CLASS_SAMPLES))]
mu_c_dict_list = [dict(pairs) for i in range(len(model.fc))]
outputs, loss, features_list = model(Y)
for i in range(len(model.fc)):
for y in range(len(NUM_CLASS_SAMPLES)):
indexes = (targets == y).nonzero(as_tuple=True)[0]
if indexes.nelement() == 0:
mu_c_dict_list[i][y] = 0
else:
mu_c_dict_list[i][y] = features_list[i][indexes, :].sum(dim=0)
mu_G_list[i] += mu_c_dict_list[i][y]
mu_c_dict_list[i][y] = mu_c_dict_list[i][y] / \
NUM_CLASS_SAMPLES[y]
mu_G_list[i] = mu_G_list[i] / sum(NUM_CLASS_SAMPLES)
prec1 = compute_accuracy(outputs.data, targets.data, topk=(1,))
return mu_G_list, mu_c_dict_list, prec1[0].cpu().numpy(), features_list, loss
def compute_Sigma_W(H, mu_c_dict, targets):
Sigma_W = 0
for y in range(len(NUM_CLASS_SAMPLES)):
indexes = (targets == y).nonzero(as_tuple=True)[0]
if indexes.nelement() == 0:
Sigma_W += 0
else:
Sigma_W += ((H[indexes, :] - mu_c_dict[y]).unsqueeze(2)
@ (H[indexes, :] - mu_c_dict[y]).unsqueeze(1)).sum(0)
Sigma_W /= NUM_SAMPLES
return Sigma_W.cpu().numpy()
def compute_Sigma_B(mu_c_dict, mu_G):
Sigma_B = 0
K = len(mu_c_dict)
for i in range(K):
Sigma_B += (mu_c_dict[i] -
mu_G).unsqueeze(1) @ (mu_c_dict[i] - mu_G).unsqueeze(0)
Sigma_B /= K
return Sigma_B.cpu().numpy()
def compute_NC2_W(W, s_array, depth):
K = W.shape[0]
WWT = torch.mm(W, W.T)
WWT /= torch.norm(WWT, p='fro')
diag_array = torch.pow(s_array, 2* depth)
sub = torch.diag_embed(diag_array)
sub /= torch.norm(sub, p='fro')
ETF = 1 / pow(K - 1, 0.5) * (torch.eye(K) - 1 / K * torch.ones((K, K))).cuda()
metric = torch.norm(WWT - sub, p='fro')
return metric.detach().cpu().numpy().item()
def compute_NC2_H(mu_c_dict, s_array, feature_decay_rate, depth):
K = len(mu_c_dict)
H = torch.empty(mu_c_dict[0].shape[0], K, device=s_array.device)
for i in range(K):
H[:, i] = mu_c_dict[i]
HTH = torch.mm(H.T, H)
HTH /= torch.norm(HTH, p='fro')
s_array_square_M = pow(s_array, 2*depth)
diag_array = torch.div(s_array_square_M,
pow((s_array_square_M + NUM_SAMPLES * feature_decay_rate), 2))
sub = torch.diag_embed(diag_array)
sub /= torch.norm(sub, p='fro')
ETF = 1 / pow(K - 1, 0.5) * (torch.eye(K) - 1 / K * torch.ones((K, K))).cuda()
metric = torch.norm(HTH - sub, p='fro')
return metric.detach().cpu().numpy().item()
def compute_NC3_WH(W, mu_c_dict, s_array, feature_decay_rate, depth):
K = len(mu_c_dict)
H = torch.empty(mu_c_dict[0].shape[0], K, device=W.device)
for i in range(K):
H[:, i] = mu_c_dict[i]
s_array_square_M = pow(s_array, 2*depth)
diag_array = torch.div(s_array_square_M,
s_array_square_M + NUM_SAMPLES * feature_decay_rate)
WH = torch.mm(W, H.cuda())
WH /= torch.norm(WH, p='fro')
sub = torch.diag_embed(diag_array)
sub_np = sub.cpu().detach().numpy()
sub /= torch.norm(sub, p='fro')
ETF = 1 / pow(K - 1, 0.5) * (torch.eye(K) - 1 / K * torch.ones((K, K))).cuda()
res = torch.norm(WH - sub, p='fro')
return res.detach().cpu().numpy().item()
def compute_NC3_W_sub_H(W, mu_c_dict, s_array, feature_decay_rate, depth):
K = len(mu_c_dict)
H = torch.empty(mu_c_dict[0].shape[0], K, device=W.device)
diag_array = torch.zeros(len(NUM_CLASS_SAMPLES), device=W.device)
mu_G = torch.zeros(1, mu_c_dict[0].shape[0], device=W.device)
for i in range(K):
H[:, i] = mu_c_dict[i]
diag_array[i] = s_array[i]**(2*depth) + NUM_SAMPLES*feature_decay_rate
W /= torch.norm(W, p='fro')
sub = torch.diag_embed(diag_array) @ H.T
sub /= torch.norm(sub, p='fro')
H1 = H / torch.norm(H, p = 'fro')
res = torch.norm(W - sub, p='fro')
return res.detach().cpu().numpy().item()
def main(args):
utils.set_seed(args.seed)
name = "imbalance-MSE-nobias" \
+ "-depth_linear_" + str(args.depth_linear) \
+ "-" + "lr_" + str(args.lr) + "-" + "dim_" + str(args.hidden) \
+ "-" + "epochs_" + str(args.num_iteration) + "-seed_" + str(args.seed)
S_ARRAY = compute_s(args.weight_decay, args.feature_decay,
args.depth_linear, device="cuda")
model = SyntheticModel(hidden=args.hidden, num_classes=args.num_classes,
depth_linear=args.depth_linear, NUM_SAMPLES=NUM_SAMPLES,
weight_decay=args.weight_decay, feature_decay=args.feature_decay).to("cuda")
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9)
targets = []
for idx in range(len(NUM_CLASS_SAMPLES)):
targets = [*targets, *[idx]*NUM_CLASS_SAMPLES[idx]]
targets = torch.tensor(targets).to("cuda")
# Randomy shuffle
targets = targets[torch.randperm(targets.shape[0])]
Y = F.one_hot(targets).to("cuda").to(torch.float)
pbar = tqdm(range(args.num_iteration))
for iteration in pbar:
model.train()
optimizer.zero_grad()
_, loss, features = model(Y)
loss.backward()
pbar.set_description("Loss is {:.6f}".format(loss))
optimizer.step()
with torch.no_grad():
model.eval()
W_list = []
W_temp = model.fc[-1].weight.clone()
W_list.append(W_temp)
for j in list(reversed(range(0, len(model.fc)-1))):
if isinstance(model.fc[j], nn.Linear):
W_temp = W_temp @ model.fc[j].weight
W_list.append(W_temp)
mu_G_train_dict, mu_c_dict_train_dict, train_acc1, features_list, loss = compute_info(
model, Y, targets)
Sigma_W = compute_Sigma_W(
features_list[-1], mu_c_dict_train_dict[-1], targets)
Sigma_B = compute_Sigma_B(
mu_c_dict_train_dict[-1], mu_G_train_dict[-1])
NC1_collapse_metric = np.trace(
Sigma_W @ scilin.pinv(Sigma_B)) / len(mu_c_dict_train_dict[-1])
print(NC1_collapse_metric)
NC2_W = compute_NC2_W(W_list[-1], S_ARRAY, args.depth_linear) # W_M...W_1
NC2_H = compute_NC2_H(
mu_c_dict_train_dict[-1], S_ARRAY, args.feature_decay, args.depth_linear) # H only
NC3_WH = compute_NC3_WH(
W_list[0], mu_c_dict_train_dict[0], S_ARRAY, args.feature_decay, args.depth_linear)
NC3_W_sub_H = compute_NC3_W_sub_H(
W_list[-1], mu_c_dict_train_dict[-1], S_ARRAY, args.feature_decay, args.depth_linear)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--num_classes', type=int, default=4)
parser.add_argument('--hidden', type=int, default=64)
parser.add_argument('--weight_decay', type=float, default=5e-4)
parser.add_argument('--feature_decay', type=float, default=5e-4)
parser.add_argument('--depth_linear', type=int, default=6)
parser.add_argument('--num_iteration', type=int, default=30000)
parser.add_argument('--lr', type=float, default=0.1)
parser.add_argument('--seed', type=str, default=2)
args = parser.parse_args()
main(args)