running_loss
in this code ? i know it calculated the loss , and we need to get the probability .
please take a look at the comment sections
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# this loop through 938 images and labels (length of trainloader
images = images.view(images.shape[0], -1)
optimizer.zero_grad()
output = model.forward(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() #what this line does
else:
print(f"Training loss: {running_loss/len(trainloader)}")
thank you , but why we need (running_loss += loss.item()) the trainloader has different numbers !? why we mixed the loss values of different numbers ?
maybe a stupid question , sorry
You are calculating the running loss with this line of code so that you could later e.g. calculate the mean loss of that epoch.
Let me know, if I misunderstood the question.
The average of the batch losses will give you an estimate of the “epoch loss” during training.
Since you are calculating the loss anyway, you could just sum it and calculate the mean after the epoch finishes.
This training loss is used to see, how well your model performs on the training dataset.
Alternatively you could also plot the batch loss values, but this is usually not necessary and will give you a lot of outputs.
I’ve just used Colab in the past a few times for debugging purposes. Not sure if the runtime changed, but I don’t thing Colab is a good fit for “real time” deployment, e.g. since the notebook runtime is limited.
What if we use .detach() when working with Pytorch lightning. It would give us the data without any computational graph. Will it be correct to use .detach() instead of .item() ?
.detach() will return a tensor, which is detached from the computation graph, while .item() will return the Python scalar. I don’t know how and where this is needed in PyTorch Lightning depending on the use case detach() might also work.
Thanks for such a quick reply. Yes, I understand this concept now. So because in Pytorch docs it is mentioned that tensors can be passed for logging, I am sure I can do the following:
However, I am little unsure if the following implementation is correct in which I replaced .item() with .detach() before the loss value is returned by the model. I am not getting any syntax error though but I am little worried if it would interfere with grad_calculation and affect the performance.
NOTE - The reason I am trying to replace .item() is because I am training the model on multiple gpus and it was taking very long to train just one epoch. So while going through the pytorch lightning docs I came across this