-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
92 lines (81 loc) · 3.46 KB
/
train.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
import torch
import numpy as np
from model_property import static_layernorm_lip_loss
def train(dataloader, model, loss_fn, optimizer, device='cuda', lip_loss_coef=None):
size = len(dataloader.dataset)
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
# Compute prediction error
pred = model(X)
loss = loss_fn(pred, y)
if lip_loss_coef is not None:
loss += lip_loss_coef * static_layernorm_lip_loss(model)
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def test(dataloader, model, loss_fn, device='cuda'):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
def gradloss(model, x, batchnorm):
for i in model.parameters():
i.requires_grad = True
if batchnorm:
first_derivative = torch.autograd.functional.jacobian(model, x, create_graph=True)
sum_first = torch.sum(first_derivative * first_derivative) / x.shape[0]
else:
sum_first = torch.tensor(0, dtype=x.dtype).to(device=x.device)
for i in range(x.shape[0]):
first_derivative = torch.autograd.functional.jacobian(model, x[i].reshape((1,) + x.shape[1:]), create_graph=True)
sum_first += torch.sum(first_derivative * first_derivative)
return sum_first
def gradloss_train(dataloader, model, loss_fn, optimizer, mu=0.1, device='cuda', batchnorm=False):
size = len(dataloader.dataset)
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
# Compute prediction error
pred = model(X)
loss = loss_fn(pred, y)
grad_loss = gradloss(model, X, batchnorm)
tot_loss = loss + mu * grad_loss
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 200 == 0:
loss, current = loss.item(), batch * len(X)
print(f"loss: {loss:>7f} gradient loss: {grad_loss:>7f} total loss: {tot_loss:>7f} [{current:>5d}/{size:>5d}]")
def gradient_test(dataloader, model, loss_fn, gl_ratio=0.1, device='cuda', batchnorm=False):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss, correct, grad_loss, gl_cnt = 0, 0, 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
if np.random.uniform(0, 1) < gl_ratio:
grad_loss += float(gradloss(model, X, batchnorm))
gl_cnt += 1
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
grad_loss /= gl_cnt
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f}, Avg Gradient loss: {grad_loss:>8f} \n")