-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.py
More file actions
71 lines (43 loc) · 1.91 KB
/
Train.py
File metadata and controls
71 lines (43 loc) · 1.91 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
import json
import torch
from Evaluate import evaluate
import time
def cosine(t1, t2):
cos = torch.nn.CosineSimilarity(dim=0)
c = cos(t1.detach().cpu().flatten(), t2.detach().cpu().flatten())
return c
def train(model, optimizer, main_track, lr, data_loader,
device, criterion, epoch, cosine=False, batch_size=128, n_epochs=20):
iterations_left = (n_epochs - epoch) * len(data_loader)
total_iterations = n_epochs * len(data_loader)
start_time = time.time()
cosine_track = []
for i, (images, labels) in enumerate(data_loader):
if i > 1 and cosine:
prior_grad = [p.grad.data.detach().cpu() for p in model.parameters()]
s = time.time()
images = images.to(device)
labels = labels.to(device)
output = model(images)
loss = criterion(output, labels)
optimizer.zero_grad()
loss.backward()
if optimizer.__class__.__name__ == "SVRG":
optimizer.step(i)
else:
optimizer.step()
cosine_ = "NA"
if i > 1 and cosine:
cos = torch.nn.CosineSimilarity(dim=0)
c_track = []
for g1, p1 in zip(prior_grad, model.parameters()):
c_track.append(cosine(g1, p1.grad).item())
cosine_ = round(sum(c_track) / len(c_track), 2)
cosine_track.append(cosine_)
e = time.time()
taken = round((e - s), 2)
remaining = (taken) * iterations_left
iterations_left -= 1
print(f"""OPTIMIZER: {str(optimizer).split(' ')[0]}| LR: {lr}| BATCH_SIZE: {data_loader.batch_size}| EPOCH: {epoch}/{n_epochs}| ITERATION: {i}/{len(data_loader)}| LOSS: {round(loss.item(), 3)}| COSINE: {cosine_}| REMAINING: {round(remaining, 2)} second(s)\r""", end="")
# Returns loss at the end of epoch and cosine track if used
return loss.item(), cosine_track