I have had a lots of problems with this notebook but hopefully this is the last one:
I now have:
- All my inputs as tensors
- Both the data and model (including fc) on the GPU
- Resized all the images to the same size
-
Changed requires_grad = True
for the fc
My model will only do one forward pass though before sitting idle. I think it is something to do with the if statement. When steps = 1 then steps % print_every != 0
and I am assuming the code doesn’t know what to do next?
# Train the classifier
def train_classifier(model, optimizer, criterion,train_loader, valid_loader, epochs):
steps = 0
print_every = 1
for e in range(epochs):
model.train()
running_loss = 0
for images, labels in iter(train_loader):
images, labels = images.cuda(), labels.cuda()
steps += 1
print("Steps: " + str(steps))
optimizer.zero_grad()
output = model.forward(images)
print("Output " + str(output))
loss = criterion(output, labels)
print("Loss: " + str(loss))
loss.backward()
optimizer.step()
running_loss += loss.item()
print("Running loss: " + str(running_loss))
if steps % print_every == 0:
model.eval()
# Turn off gradients for validation, saves memory and computations
with torch.no_grad():
validation_loss, accuracy = validation(model, valid_loader, criterion, device)
print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(running_loss/print_every),
"Validation Loss: {:.3f}.. ".format(validation_loss/len(validate_loader)),
"Validation Accuracy: {:.3f}".format(accuracy/len(validate_loader)))
running_loss = 0
model.train()
Output:
Steps: 1
Output tensor([[-0.8534, -0.5550],
[-0.8226, -0.5786],
[-0.9021, -0.5204],
[-0.5913, -0.8066],
[-0.7069, -0.6796],
[-0.6809, -0.7055],
[-0.7779, -0.6150],
[-0.9171, -0.5103],
[-0.7158, -0.6710],
[-0.6874, -0.6989]], device='cuda:0', grad_fn=<LogSoftmaxBackward>)
Loss: tensor(0.7274, device='cuda:0', grad_fn=<NllLossBackward>)
Running loss: 0.7273765206336975
As we can see the loop is working fine until the if statement.