-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
359 lines (294 loc) · 12.6 KB
/
utils.py
File metadata and controls
359 lines (294 loc) · 12.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
from pathlib import Path
import matplotlib.pyplot as plt
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
def plot_aoc_ratio(ud, model, save_path: str = "artifacts"):
"""
this plot let us to see the ratio of the amount of change in the parameters given the weights.
this ratio is calculated by multiplying the learning rate with the layer's gradient's standard deviation and dividing it by the layer's parameters' standard deviation
so this ratio will be higher if gradients are vary too much from the mean (so they're large) and the parameters are too small in comparison
a usual value for this ratio is 1e-3, so if the ratio is too high, it means that the updates are too large with respect to the weights
meaning a faster learning, if it's rather lower than 1e-3 that typically means that the updates are too less to make an impact on the weights
so, consider that we lower the learning rate, so this would result in a slower learning process, meaning ratios of the most layers will probably be less than 1e-3
so the learning will slow down and the model will learn the patterns in the data more slowly.
"""
# visualize the ratio of the amount of change vs the weights
# and this is the ratio of the amount of change
path = Path(save_path)
parameters = model.parameters()
plt.figure(figsize=(20, 4))
legends = []
for i, (param, p) in enumerate(parameters.items()):
if p.ndim == 2:
plt.plot([ud[j][i] for j in range(len(ud))])
legends.append(param)
plt.plot(
[0, len(ud)], [-3, -3], "k"
) # these ratios should be ~1e-3, indicate on the plot (the initial data fed to log10 in the training phase, so -3 means 10^-3)
# put the legend outside of the plot
plt.legend(legends, bbox_to_anchor=(1.05, 1), loc="upper left")
plt.title("update to data ratio")
plt.savefig(path / "amount of change.png", bbox_inches="tight")
def plot_grad2data_ratio(model, save_path: str = "artifacts"):
"""
this plot let us to see the ratio of the magnitude of the gradients to the magnitude of the data
so if the ratio is too big, it means that the gradients are too large with respect to the data so the updates will be too large
if the ratio is too small, we then expect the step update to be too small to make an impact. While this could still work, we might need to increase the learning rate since
it'll effect the network's overall learning speed.
try to train the layers for just 1 epoch and then 1000 full epochs
in the initial epochs, the ratio of the last layer will be too large compared to the others but it'll decrease (or other's will increase)
as the training goes on. Of course, this plot is not that informative since what we need is the actual amount of update compared to the layer input.
"""
# this is the ratio of the gradient of a specific layer to its input.
# so if the ratio is too high, it means that the gradients are too high with regard to the input so the update will be larger
# and we actually want constant but smaller updates throughout the network so we don't miss any local minimas etc.
path = Path(save_path)
parameters = model.parameters()
plt.figure(figsize=(20, 4))
legends = []
print("-" * 20)
print("Grad to data ratio")
for i, (param, p) in enumerate(parameters.items()):
t = p.grad
if p.ndim == 2:
print(
"weight %10s | mean %+f | std %e | grad:data ratio %e"
% (tuple(p.shape), t.mean(), t.std(), t.std() / p.std())
)
hy, hx = torch.histogram(t, density=True)
plt.plot(hx[:-1].detach(), hy.detach())
legends.append(f"{param} ({tuple(p.shape)})")
plt.legend(legends)
plt.title("weights gradient to data ratio")
plt.savefig(path / "grad2data.png", bbox_inches="tight")
print("-" * 20)
def plot_layer_grads(model, save_path: str = "artifacts"):
"""
this plot let us to see the distribution of the gradients of the layers
"""
path = Path(save_path)
# visualize histograms
plt.figure(figsize=(20, 4))
legends = []
layers = model._layers
print("-" * 20)
print("Layers: grads distribution")
for i, layer in enumerate(layers):
layer_name = layer.__class__.__name__
t = layer.out.grad
assert t is not None, f"Grads for {layer_name} is None!"
print(
"layer %d (%10s): mean %+.8f, std %.8f"
% (i, layer_name, t.mean().item(), t.std().item())
)
hy, hx = torch.histogram(t, density=True)
plt.plot(hx[:-1].detach(), hy.detach())
legends.append(f"layer {i} ({layer_name})")
plt.legend(legends)
plt.title("Layers: grads distribution")
plt.savefig(path / "grads.png")
print("-" * 20)
return
def plot_layer_outputs(model, save_path: str = "artifacts"):
path = Path(save_path)
# visualize histograms
layer_name = None
plt.figure(figsize=(20, 4))
legends = []
layers = model._layers
print("-" * 20)
print("Layers: output distribution")
for i, layer in enumerate(layers):
t = layer.out
layer_name = layer.__class__.__name__
print(
"layer %d (%10s): mean %+.2f, std %.2f, saturated: %.2f%%"
% (
i,
layer_name,
t.mean().item(),
t.std().item(),
(torch.abs(t) > 0.97).float().mean().item() * 100,
)
)
hy, hx = torch.histogram(t, density=True)
plt.plot(hx[:-1].detach(), hy.detach())
legends.append(f"layer {i} ({layer_name})")
plt.legend(legends)
plt.title("Layers: output distribution")
plt.savefig(path / "outputs.png")
print("-" * 20)
return
def save_loss_figures(
train_losses: torch.Tensor, valid_losses: torch.Tensor, save_path: str = "artifacts"
):
plt.figure(figsize=(10, 5))
plt.plot(train_losses.tolist(), label="train loss")
plt.plot(valid_losses.tolist(), label="valid loss")
plt.legend()
path = Path(save_path)
plt.savefig(path / "losses.png")
return
def get_baseline_score(vocabulary_size):
"""
Calculate the baseline loss for the model.
This function computes a baseline loss assuming uniform probability
distribution over the vocabulary.
Args:
model: The GPT model instance
Returns:
float: The baseline loss
"""
baseline_loss = (-torch.log(torch.tensor(1 / vocabulary_size))).item()
print("BASELINE LOSS:", baseline_loss)
return baseline_loss
def load_artifact(save_path, name):
artifact = torch.load(f"{save_path}/{name}.pt")
return artifact
def save_artifacts(save_path, **kwargs):
os.makedirs(save_path, exist_ok=True)
for key, value in kwargs.items():
torch.save(value, f"{save_path}/{key}.pt")
def calc_debug_stats(
learning_rate, parameters
) -> tuple[list[float], list[float], list[float]]:
ratio = [
(learning_rate * p.grad.std() / p.data.std()).log10().item()
for p in parameters.values()
]
means = [p.mean().item() for p in parameters.values()]
stds = [p.std().item() for p in parameters.values()]
return ratio, means, stds
def train_step(
model: torch.nn.Module,
x: torch.Tensor,
y: torch.Tensor,
optimizer: torch.optim.Optimizer,
device: str,
) -> float:
"""optimizes the model weights using the given optimizer object for a batch"""
x, y = x.to(device), y.to(device)
# get the logits and the loss
_, loss = model(x, y)
# optimize the model parameters and log the gradients if needed
# backward pass
loss.backward()
optimizer.step()
optimizer.zero_grad(set_to_none=True) # cast the grads to None
return loss.item()
def evaluate(model, loader, device, progress_bar=True):
valid_losses = torch.zeros(len(loader))
bar = (
tqdm(enumerate(loader), total=len(loader))
if progress_bar
else enumerate(loader)
)
for i, (x, y) in bar:
with torch.no_grad():
x, y = x.to(device), y.to(device)
# same as above, don't calculate the gradients this time
# and just calculate the loss
_, loss = model(x, y) # _: logits
# statistics and logging, again.
valid_losses[i] = loss
if isinstance(bar, tqdm):
desc_text = (
f"({(i+1)}/{len(loader)}): loss {valid_losses.sum()/((i+1)):.4f}"
)
bar.set_description(desc_text)
return valid_losses.mean().item()
def train_loop(
model,
train_loader: DataLoader,
test_loader: DataLoader,
learning_rate: float,
lrsche: bool,
device: str,
max_steps: int | None = None,
epochs: int | None = None,
optimizer: torch.optim.Optimizer | None = None,
):
"""
Trains the given model using the provided data loaders.
model: the model to be trained, current available ones are MLP, hMLP, and GPT.
train_loader (torch DataLoader): the training loader that loads the training batches for the training
test_loader (torch DataLoader) : the testing loader that loads the data batches for the testing phase
epochs (int): number of epochs (iterations) to train the model
learning_rate (float): the learning rate that'll be used to scale the gradients in the optimization phase
lrsche (bool): whether apply a learning rate decay or not
"""
if (max_steps is None) and (epochs is None):
raise ValueError("Both `max_steps` and `epochs` are none!")
assert not (
max_steps and epochs
), "You cannot pass both `max_steps` and `epochs` at the same time!"
# allow the model parameters to calculate gradients
model.to(device)
if optimizer is None:
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
train_batch_size: int = (
train_loader.batch_size if isinstance(train_loader.batch_size, int) else 1
)
num_training_samples = len(train_loader.dataset)
# training loop
if epochs:
# loss vectors
train_losses: torch.Tensor = torch.zeros(epochs)
valid_losses: torch.Tensor = torch.zeros(epochs)
for epoch in range(epochs):
# set up the tqdm bar
bar = tqdm(enumerate(train_loader), total=len(train_loader))
# decay the learning rate if it's provided
if lrsche and epochs > 1:
n_epochs = round(epochs * 0.33)
if (epoch + 1) % n_epochs == 0:
learning_rate = learning_rate / 10
print("========")
print("TRAINING (epoch:%d/%d)" % (epoch + 1, epochs))
model.train()
for i, (x, y) in bar:
loss = train_step(model, x, y, optimizer, device)
# statistics and logging
train_losses[epoch] += loss
desc_text = f"({epoch*train_batch_size + i*train_batch_size}/{num_training_samples}) (lr={learning_rate:.4f}): loss {train_losses[epoch]/(i+1):.4f}"
bar.set_description(desc_text)
train_losses[epoch] /= len(train_loader)
model.eval()
valid_loss = evaluate(model, test_loader, device)
valid_losses[epoch] = valid_loss
elif max_steps is not None:
# if `max_steps` is given instead of `epochs`,
# just train the model for the given max_steps,
# we will assume the dataloader has InfiniteRandomSampler as it's sampler
# but it can be used with default dataloader as well
bar = tqdm(total=max_steps)
# loss vectors
train_losses = torch.zeros(max_steps)
valid_losses = torch.zeros(max_steps)
batch_size = train_loader.batch_size
batch_size = batch_size if isinstance(batch_size, int) else 1
total = 0
frac = round(max_steps * 0.33)
valid_loss = 0
model.train()
for ix, (x, y) in enumerate(train_loader):
if ix == max_steps:
break
loss = train_step(model, x, y, optimizer, device)
bar.update(1)
total += batch_size
train_losses[ix] = loss
desc_text = f"(lr={learning_rate:.4f}): loss {train_losses.sum()/(ix+1):.4f} val_loss {valid_loss:.4f}"
bar.set_description(desc_text)
if frac > 1 and ((ix + 1) % frac == 0):
model.eval()
valid_loss = evaluate(model, test_loader, device, progress_bar=False)
model.train()
model.to("cpu")
bar.close()
else:
train_losses, valid_losses = torch.tensor([]), torch.tensor([])
# return the losses.
return train_losses, valid_losses