-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
82 lines (74 loc) · 3.33 KB
/
training.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
import torch
from torchvision import datasets, transforms, models
from torch import nn
from torch import optim
def training(epoch, processor, model, trainloader, validloader, traindataset, learning_rate, filename):
#processor parameter
if (processor == 'gpu'):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
else:
device = torch.device("cpu")
print('processor: '+str(device))
#parameters for model training and testing
criterion = nn.NLLLoss()
#optimizer to update weights with gradients
optimizer = optim.Adam(model.classifier.parameters(), lr=float(learning_rate))
model.to(device)
#pass through the datasets
epochs = int(epoch)
steps = 0
print("Start of Training...\n")
print_every = 5
for epoch in range(epochs):
running_loss = 0
#Train the classifier layers using backpropagation using the pre-trained network to get the features
for inputs, labels in trainloader:
steps += 1
# Move input and label tensors to the default device
inputs, labels = inputs.to(device), labels.to(device)
#feed forward pass
logps = model.forward(inputs)
#loss calculation
loss = criterion(logps, labels)
#reinitialize the gradient to zero
optimizer.zero_grad()
#back propagation pass
loss.backward()
#update of weights
optimizer.step()
running_loss += loss.item()
#training ends here
#Track the loss and accuracy on the validation set to determine the best hyperparameters
if steps % print_every == 0:
test_loss = 0
accuracy = 0
model.eval()
with torch.no_grad():
for inputs, labels in validloader:
inputs, labels = inputs.to(device), labels.to(device)
logps = model.forward(inputs)
batch_loss = criterion(logps, labels)
test_loss += batch_loss.item()
# Calculate accuracy
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
print(f"Epoch {epoch+1}/{epochs} | "
f"Train loss: {running_loss/print_every:.3f} | "
f"Validation loss: {test_loss/len(validloader):.3f} | "
f"Validation accuracy: {accuracy/len(validloader):.3f}")
running_loss = 0
model.train()
print("\n End of Training...")
savecheckpoint(epochs, optimizer, filename,traindataset,model)
def savecheckpoint(epochs, optimizer, filename, traindataset, model):
model.class_to_idx = traindataset.class_to_idx
checkpoint = {'epochs':epochs,
'classifier': model.classifier,
'class_to_idx': model.class_to_idx,
'state_dict': model.state_dict(),
'optimizer':optimizer.state_dict()
}
torch.save(checkpoint, filename)
print("\n Checkpoint available in {}".format(filename))