-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
81 lines (65 loc) · 2.91 KB
/
main.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
def train(model, device, train_loader, optimizer, epoch, l1_factor, scheduler):
#Function for training model
model.train()
epoch_loss = 0
correct = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = nn.CrossEntropyLoss()(output, target)
reg_loss = 0
if l1_factor > 0:
for p in model.parameter():
reg_loss = reg_loss + p.abs().sum()
loss += l1_factor * reg_loss
epoch_loss += loss.item()
loss.backward()
optimizer.step()
if scheduler:
scheduler.step()
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
print(f'Train set: Average loss: {loss.item():.4f}, Accuracy: {100. * correct/len(train_loader.dataset):.2f}')
train_loss = epoch_loss / len(train_loader)
train_acc=100.*correct/len(train_loader.dataset)
return train_loss, train_acc
def test(model, device, test_loader):
#Function for testing model
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += nn.CrossEntropyLoss()(output, target).item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
pred_cpu = output.cpu().data.max(dim=1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_acc = 100.*correct/len(test_loader.dataset)
print(f'\nTest set: Average loss: {test_loss:.3f}, Accuracy: {100. * correct/len(test_loader.dataset):.2f}')
return test_loss, test_acc
def main(EPOCH, model, device, train_loader, test_loader, optimizer, scheduler, l1_factor):
#This function controls the run for train/test of model for number of epochs as passed in parameters
train_loss_values = []
test_loss_values = []
train_acc_values = []
test_acc_values = []
for epoch in range(1, EPOCH + 1):
print('\nEpoch {} : '.format(epoch))
# train the model
train_loss, train_acc = train(model, device, train_loader, optimizer, epoch, l1_factor, scheduler)
test_loss, test_acc = test(model, device, test_loader)
#scheduler.step(test_acc)
train_loss_values.append(train_loss)
test_loss_values.append(test_loss)
train_acc_values.append(train_acc)
test_acc_values.append(test_acc)
return train_loss_values, test_loss_values, train_acc_values, test_acc_values