I’ve built a CNN model which I’m now attempting to training using the conventional for-loop. I’ve been having an issue the past few days in which I’m not able to get the for-loop to work properly. Essentially, what happens is that the loop prints the epochs continuously until I have to interrupt.
This is the code I’m using:
epochs = 30
training_loss = []
for epoch in range(epochs):
running_loss = 0
#Train
cnn.train()
for images, labels in trainloader:
if cuda:
images, labels = images.cuda(), labels.cuda()
optimiser.zero_grad()
outputs = cnn(images)
loss = criterion(outputs, labels)
running_loss += loss.item() * images.size(0)
loss.backward()
optimiser.step()
epochs_train_loss = running_loss / len(trainloader.dataset)
print ("Epoch {}, Training Loss: {}".format(epoch, training_loss))
Could you check the length of the trainloader via print(len(trainloader))? This should return the number of batches and iterations in this loop.
Assuming you are using the map-style Dataset, which implements the __len__ method, each epoch should only yield the specified number of samples.
The DataLoader loop should exit after 163 iterations and would be executed epochs times.
Add a print statement to the inner loop and check the current loop index.
Since this loop apparently never exits, it would help to debug this issue.
I made some changes so that my len(trainloader) is now 82. However, the cell keeps running and running after Iteration: 72 is achieved. I’m not too sure why. Is there some stoppage logic I could use perhaps?
It seems the cell is not running anymore, but hangs instead.
Are you seeing the same issue in a terminal, if you execute the script directly without a notebook?
You’re right in that it just hangs after it finishes executing.
I’m not too sure how to go about running this in terminal as I’ve only worked in Notebooks. Interestingly enough, the same thing happens if I used Skorch’s NeuralNetClassifier:
It sounds rather like a multiprocessing issue (or code parts are executed after the training, which are not shown in the posted code).
You should be able to export the notebook as a Python script file and run it in a terminal.