-
Notifications
You must be signed in to change notification settings - Fork 7
/
train.py
167 lines (128 loc) · 5.21 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
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
import torch
import torch.nn.utils.prune as prune
import models.aux_funs as maf
# train step
def train_step(conf, model, opt, train_loader, verbosity = 1):
model.train()
acc = 0
tot_loss = 0.0
tot_steps = 0
for batch_idx, (x, y) in enumerate(train_loader):
# get batch data
x, y = x.to(conf.device), y.to(conf.device)
opt.zero_grad()
logits = model(x)
loss = conf.loss(logits, y)
if hasattr(conf,"weight_reg"):
loss += conf.weight_reg(model)
loss.backward()
opt.step()
# for classification tasks we want to evaluate the accuracy
if conf.eval_acc:
acc += (logits.max(1)[1] == y).sum().item()
tot_loss += loss.item()
tot_steps += y.shape[0]
# print the current accuracy and loss
if verbosity > 0:
print(50*"-")
print('Train Accuracy:', acc/tot_steps)
print('Train Loss:', tot_loss)
return {'loss':tot_loss, 'acc':acc/tot_steps}
# validation step
def validation_step(conf, model, opt, validation_loader, verbosity = 1):
acc = 0.0
loss = 0.0
tot_steps = 0
# -------------------------------------------------------------------------
# evaluate on validation set
if not validation_loader is None:
for batch_idx, (x, y) in enumerate(validation_loader):
# get batch data
x, y = x.to(conf.device), y.to(conf.device)
# evaluate model on batch
logits = model(x)
# Get classification loss
c_loss = conf.loss(logits, y)
if conf.eval_acc:
acc += (logits.max(1)[1] == y).sum().item()
loss += c_loss.item()
tot_steps += y.shape[0]
tot_acc = acc/tot_steps
else:
tot_acc = 0.0
# ------------------------------------------------------------------------
# evaluate sparsity
conv_sparse = maf.conv_sparsity(model)
linear_sparse = maf.linear_sparsity(model)
net_sparse = maf.net_sparsity(model)
node_sparse = maf.node_sparsity(model)
# ------------------------------------------------------------------------
# evaluate regularizers of opt and append to history
reg_eval = getattr(opt, "evaluate_reg", None)
if callable(reg_eval):
reg_vals = opt.evaluate_reg()
else:
reg_vals = []
# print values
if verbosity > 0:
print(50*"-")
print('Validation Accuracy:', tot_acc)
print('Non-zero kernels:', conv_sparse)
print('Linear sparsity:', linear_sparse)
print('Overall sparsity:', net_sparse)
print('Node sparsity:', node_sparse)
print('Regularization values per group:', reg_vals)
return {'loss':loss, 'acc':tot_acc, 'conv_sparse':conv_sparse, 'linear_sparse':linear_sparse,
'reg_vals':reg_vals,'node_sparse':node_sparse}
# test step
def test(conf, model, test_loader, verbosity=1):
model.eval()
acc = 0
tot_steps = 0
loss = 0
with torch.no_grad():
for batch_idx, (x, y) in enumerate(test_loader):
# get batch data
x, y = x.to(conf.device), y.to(conf.device)
# evaluate
pred = model(x)
if conf.eval_acc:
acc += (pred.max(1)[1] == y).sum().item()
c_loss = conf.loss(pred, y)
loss += c_loss.item()
tot_steps += y.shape[0]
# print accuracy
if verbosity > 0:
print(50*"-")
print('Test Accuracy:', acc/tot_steps)
return {'acc':acc/tot_steps, 'loss':loss}
# ------------------------------------------------------------------------
# Pruning step
def prune_step(model, a1 = 0.01, a2 = 0.01, conv_group=True):
for name, module in model.named_modules():
if isinstance(module, torch.nn.Conv2d) and conv_group:
prune.ln_structured(module, name='weight', amount=a1,n=2,dim=0)
prune.ln_structured(module, name='weight', amount=a1,n=2,dim=1)
#prune.remove(module, name='weight')
elif isinstance(module, torch.nn.Linear) or isinstance(module, torch.nn.Conv2d):
prune.l1_unstructured(module, name='weight', amount=a2)
#prune.remove(module, name='weight')
class best_model:
'''saves the best model'''
def __init__(self, best_model=None, gamma = 0.0, goal_acc = 0.0):
# stores best seen score and model
self.best_score = 0.0
# if specified, a copy of the model gets saved into this variable
self.best_model = best_model
# score function
def score_fun(train_acc, test_acc):
return gamma * train_acc + (1-gamma) * test_acc + (train_acc > goal_acc)
self.score_fun = score_fun
def __call__(self, train_acc, val_acc, model=None):
# evaluate score
score = self.score_fun(train_acc, val_acc)
if score >= self.best_score:
self.best_score = score
# store model
if self.best_model is not None:
self.best_model.load_state_dict(model.state_dict())