Pytorch code isn't showing Epoch neither errors

I tried to run this PyTorch code, it doesn’t show any Epoch and also not showing any errors or warnings, can someone please help?

loss_func = nn.CrossEntropyLoss()

from torch import optim
optimizer = optim.Adam(cnn.parameters(), lr = 0.01)

from torch.autograd import Variable
num_epochs = 20
def train(num_epochs, cnn, loaders):

# Train the model
total_step = len(loaders['train'])
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(loaders['train']):
        # gives batch data, normalize x when iterate train_loader
        b_x = Variable(images)   # batch x
        b_y = Variable(labels)   # batch y
        output = cnn(b_x)[0]               
        loss = loss_func(output, b_y)

        # clear gradients for this training step   
        # backpropagation, compute gradients 
        # apply gradients             
        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

train(num_epochs, cnn, loaders)

Your code is unfortunately not formatted so a bit hard to debug.
You can post code snippets by wrapping them into three backticks ```.

To debug the issue further, add print statements to your script and check which lines are executed and where the code gets stuck.

1 Like

Thank you so much for your reply and help, I tried to edit my post so that it is easier now to debug, however in some cells I had to take screenshot so it is easier to read.
I tried to add print after each line/part and all lines/parts are printed without a problem and still the code is not getting the Epoch. can you please advise? Thank you

This might indicate that your loaders['train'] DataLoader does not contain 100 batches and you might need to adapt the if condition to print the epoch stats.
What does len(loaders['train']) return?

When I add print to the total_step which it is = len(loaders[‘train’])
then I get value 40, if this what you mean!

Yes, that’s what I meant.
It shows that your DataLoader only contains 40 batches and your if condition will thus never return True since you explicitly only print the first loss value after 100 batches:

  if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

Remove the if condition or change the modulo operation to return True for less iterations.

1 Like

Oh thank you so much, I changed the modulo operation conditions and now it is showing the Epoch, thank you so much for your help :slight_smile: