def train_one_epoch(model, optimizer, data_loader, device):
# Zero the performance stats for each epoch
for i, data in enumerate(data_loader):
# get the inputs; data is a list of [inputs, labels]
labels = torch.squeeze(data['labels'])
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
# forward + backward + optimize
outputs = model(inputs.float())
loss = criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
# Print performance statistics
running_loss += loss.item()
if i % 10 == 0: # print every 10 batches
speed = (i+1)/(batch_time-start_time)
print('[%5d] loss: %.3f, speed: %.2f, accuracy: %.2f %%' %
(i, running_loss, speed, accuracy))
def test_model(model, data_loader):
for i, data in enumerate(data_loader):
# get the inputs; data is a list of [inputs, labels]
labels = torch.squeeze(data['labels'])
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
# forward + backward + optimize
outputs = model(inputs.float())
_, predicted = torch.max(outputs.data, 1)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print('Finished Testing')
print('Testing accuracy: %.1f %%' %(accuracy))