-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.py
346 lines (290 loc) · 10.6 KB
/
regression.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import numpy as np
import torch
from os import walk
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from utils import save_pickle
TENSOR_NAMES = ["A.p", "b.p", "C.p"]
STATISTICS = ["QuadraticVariation", "Sobolev", "L2", "Spectral", "Maximum"]
COLORS = ["b", "r", "g", "m"]
STATS_NAME = {
"SqrtSobolev": r"$||A||_{H^1}$",
"SqrtSSQ": r"$||A||_F$",
"Maximum": r"$||A||_{F, \infty}$",
}
def compute_statistics(tensor_list, statistics, factor=0.0):
""" For a given parameter at a fixed depth, compute the quantities
specified in 'STATISTICS'
:param tensor_list: tensor of size (L, shape)
:return: dictionary containing all the names and the values of the statistics
"""
dico = {}
for stats in statistics:
if stats == "Sobolev" or stats == "SqrtSobolev":
res = 0.0
temp = tensor_list[0]
for param in tensor_list[1:]:
res += torch.norm(param - temp, p="fro").data.numpy() ** 2
temp = param
dico[stats] = np.sqrt(len(tensor_list) * res)
elif stats == "Holder" or stats == "Increment":
L = len(tensor_list)
temp = 0.0
maxi = 0.0
for k in range(1, L):
temp = torch.norm(
tensor_list[k] - tensor_list[k - 1], p="fro"
).data.numpy()
if temp > maxi:
maxi = temp
dico[stats] = np.power(L, factor) * maxi
elif stats == "QuadraticVariation":
res = 0.0
temp = tensor_list[0]
for param in tensor_list[1:]:
res += torch.norm(param - temp, p="fro").data.numpy() ** 2
temp = param
dico[stats] = res
elif stats in ["L2", "SSQ", "SqrtSSQ", "Root SSQ"]:
res = 0.0
for param in tensor_list:
res += torch.norm(param, p="fro").data.numpy() ** 2
if stats == "L2":
dico[stats] = np.sqrt(res / len(tensor_list))
if stats == "SSQ" or "SqrtSSQ" or "Root SSQ":
dico[stats] = np.sqrt(res)
elif stats == "Spectral":
dico[stats] = np.max(
[torch.norm(param, p=2).data.numpy() for param in tensor_list]
)
elif stats == "Maximum":
dico[stats] = np.max(
[torch.norm(param, p="fro").data.numpy() for param in tensor_list]
)
elif stats == "cumsum":
dico[stats] = torch.norm(
sum([param for param in tensor_list]), p="fro"
).data.numpy()
elif stats == "detrended-cumsum":
L = len(tensor_list)
W = [0.0 for k in range(L + 1)]
for k in range(L):
W[k + 1] = W[k] + tensor_list[k]
if L < 10:
dico[stats] = max(
[
torch.norm(W[k] - k / L * W[L]).data.numpy()
for k in range(1, L + 1)
]
)
else:
temp = 0.0
maxi = 0.0
window = max(3, int(0.10 * L))
for k in range(1, L):
if k < window:
temp = W[k] - sum(W[0 : 2 * k]) / (2 * k)
elif window <= k < L - window:
temp = W[k] - sum(W[k - window : k + window]) / (2 * window)
else:
temp = W[k] - sum(W[2 * k - L : L]) / (2 * (L - k))
temp = torch.norm(temp, p="fro").data.numpy()
if temp > maxi:
maxi = temp
dico[stats] = maxi
else:
raise NotImplementedError(f"This statistic: {stats} is not implemented")
return dico
def get_folders(experiment_path):
_, folders, _ = next(walk(experiment_path))
res = []
for f in folders:
_, sub_folders, _ = next(walk(experiment_path + f))
for g in sub_folders:
res.append(experiment_path + f + "/" + g + "/")
return res
def run_regression(path, tensor_names, statistics, factor=0.0):
_, folders, _ = next(walk(path))
values = {stat: {name: [] for name in tensor_names} for stat in statistics}
depths = []
for f in folders:
if f != "regression":
depth = f[6:]
depth = int(depth)
depths.append(depth)
_, _, files = next(walk(path + f + "/weights"))
for name in tensor_names:
if name in files:
with open(path + f + "/weights/" + name, "rb") as file:
t = torch.load(file)
temp = compute_statistics(t, statistics, factor)
for stat in statistics:
values[stat][name].append(temp[stat])
idx = np.argsort(depths)
for name in tensor_names:
for stat in statistics:
values[stat][name] = np.array(values[stat][name])[idx]
return np.sort(depths), values
def shared_delta(path):
_, folders, _ = next(walk(path))
delta = []
depths = []
for f in folders:
if f != "regression":
depth = f[6:]
depth = int(depth)
depths.append(depth)
with open(path + f + "/weights/C.p", "rb") as file:
t = torch.load(file)
delta.append(t.detach().numpy()[0])
idx = np.argsort(depths)
delta = np.array(delta)[idx]
return delta
def A_or_b_times_C(path, label, delta_type, names):
_, folders, _ = next(walk(path))
values = {name: [] for name in names}
depths = []
for f in folders:
if f != "regression":
depth = f[-4:]
depth = int(depth[1:] if depth[0] == "_" else depth)
depths.append(depth)
with open(path + f + "/weights/" + label + ".p", "rb") as file:
ab = torch.load(file)
with open(path + f + "/weights/C.p", "rb") as file:
c = torch.load(file)
if delta_type == "multi":
t = [ab[i] * torch.abs(c[i]) for i in range(len(ab))]
if delta_type == "shared":
t = [ab[i] * torch.abs(c[0]) for i in range(len(ab))]
if delta_type != "none":
temp = compute_statistics(t, names)
for name in names:
values[name].append(temp[name])
idx = np.argsort(depths)
for name in names:
values[name] = np.array(values[name])[idx]
return values
def regression_delta_times_AorB(path, statistics, tensor="A", factor=0.0):
_, folders, _ = next(walk(path))
values = {stat: [] for stat in statistics}
depths = []
for f in folders:
if f != "regression":
depth = f[6:]
depth = int(depth)
depths.append(depth)
with open(path + f + "/weights/" + tensor + ".p", "rb") as file:
AorB = torch.load(file)
with open(path + f + "/weights/C.p", "rb") as file:
delta = torch.load(file)
t = [torch.norm(d) * aorb for aorb, d in zip(AorB, delta)]
temp = compute_statistics(t, statistics, factor)
for stat in statistics:
values[stat].append(temp[stat])
idx = np.argsort(depths)
for stat in statistics:
values[stat] = np.array(values[stat])[idx]
return np.sort(depths), values
def plot_final_regression(
depths,
values,
tensor_names,
statistics,
stats_name,
colors,
path,
save=True,
reg=True,
loc="upper right",
fontsize="small",
filename="",
):
if save:
save_pickle(path + "depths", depths)
for name in tensor_names:
for i, stat in enumerate(statistics):
y = values[stat][name]
if name == "A.p" and stat == "detrended-cumsum" and filename == "ass2":
idx = np.argmin(depths < 200)
lr = LinearRegression().fit(
np.log(depths[idx:]).reshape(-1, 1), np.log(y[idx:])
)
else:
lr = LinearRegression().fit(np.log(depths).reshape(-1, 1), np.log(y))
slope = lr.coef_
intercept = lr.intercept_
plt.loglog(
depths,
y,
c=colors[i],
lw=1,
label=stats_name[stat]
+ (" : slope = %.1f" % np.round(slope, 1) if reg else ""),
)
regy = np.exp(intercept + slope * np.log(depths))
if reg:
plt.loglog(depths, regy, "k--", ls="--", lw=0.5)
if save:
save_pickle(path + stat + "_" + name, y)
plt.xlabel("Depth")
plt.legend(loc=loc, fontsize=fontsize)
if save:
plt.savefig(
path + f"{filename}_{name}ng"
) # genius # elon-musk # QI=200 # newton
else:
plt.show()
plt.clf()
def make_regression(net, path):
colors = ["b", "r", "g", "m"]
tensor_names = ["A.p", "b.p"] + (
["C.p"] if net.delta_type in ["matrix", "multi"] else []
)
statistics = ["SqrtSobolev", "SqrtSSQ", "Maximum"]
stats_name = {
"SqrtSobolev": r"$||A||_{H^1}$",
"SqrtSSQ": r"$||A||_F$",
"Maximum": r"$||A||_{F, \infty}$",
}
depths, values = run_regression(path, tensor_names, statistics)
plot_final_regression(
depths,
values,
tensor_names,
statistics,
stats_name,
colors,
path + "regression/",
True,
)
if net.delta_type == "shared":
_, folders, _ = next(walk(path))
deltas = []
depths = []
for f in folders:
if f != "regression":
depth = f[6:]
depth = int(depth)
depths.append(depth)
deltas.append(torch.load(path + f + "/weights/C.p").detach().numpy()[0])
idx = np.argsort(depths)
depths = np.sort(depths)
deltas = np.abs(np.array(deltas)[idx])
lr = LinearRegression().fit(np.log(depths).reshape(-1, 1), np.log(deltas))
slope = lr.coef_
intercept = lr.intercept_
plt.loglog(
depths,
deltas,
c="b",
label="Shared $\\delta^{(L)}$" + ": slope = %.1f" % np.round(slope, 1),
lw=1,
)
regy = np.exp(intercept + slope * np.log(depths))
plt.loglog(depths, regy, "k--", lw=0.5)
plt.plot()
plt.xlabel("Depth $L$")
plt.legend(loc="lower left", fontsize="small")
plt.savefig(path + "/regression_shared_delta.png")
plt.clf()