Calculating Validation Losses After Training Finished

username: nmtp
cooked: <p>I’m new to PyTorch and to CNNs in general so I apologize if this is a strange question. I’ve trained a pretrained VGG-16 network and saved the checkpoint from each epoch along the way, but I’ve realized that while I have the training losses printed for each epoch, I didn’t have the validation losses.</p>
<p>So I’m wondering: Is it possible to run through in model.eval() and manually calculate the losses for each epoch with the checkpoints I have saved along the way? Would I need to call loss.backward() here as well?</p>

username: sathvik_udupa
cooked: <p>You call loss.backward() when you need to backpropogate your loss to your weights, which happens during training, you need not do it in validation step. You can load the weights to the model from the checkpoints and compute the loss from the output of the model directly.</p>

username: nmtp
cooked: <p>When I printed out loss.item() after calling loss.backward() during the training process, would this have changed the value of the loss?</p>
<p>When I load the weights to the model from checkpoints and compute loss directly on the training data, I get a significantly different loss than when I was printing it during training. Is this normal or am I doing something wrong?</p>

username: sathvik_udupa
cooked: <p>First case, I’m not sure. I save the loss.item() in another variable before loss.backward()</p>
<p>Second case, when you are training, each loss.backward() changes the model weights so the next batch loss would be different from what you would expect while doing model.eval()</p>
<p>Hope that answers your question, would be happy to elaborate if not.</p>

username: nmtp
cooked: <p>Thanks for your answers, it’s very helpful in understanding how things work.</p>
<p>Just wondering one more thing – I’m still getting some weird numbers and noticed the code I’m using has a different batch size for the dataloader for training data (batchsize = 2) and for the validation data (batchsize = 1).</p>
<p>Since the loss is currently loss/len(dataloader), would that be the source of why my validation losses are significantly below my training losses? What would be the correct way of recording the losses per epoch for both the validation and the training if the batch sizes are different?</p>

username: sathvik_udupa
cooked: <p>By default, loss functions such as crossentrophy(or log_softmax + nllloss) use mean reduction on the batch losses.<br>
Given<br>
N = No of datapoints<br>
bs = batch_size<br>
len(loader) = N/bs<br>
Loss = (l1 + l2 + l3 + …)/bs + (l1 + l2 + l3 + …)/bs + …<br>
Then Loss/len(loader) = Loss*bs/N = (Sum of all l)/N<br>
So you can see that the batch size you use for test case doesn’t matter(there will be change in value if the last batch is not full)</p>
<p>You can keep the default mean reduction in your loss function, sum up all your losses, divide it by len(loader) for both train and val regardless of different batch sizes. One other way for train is to append all your batch losses and return it, this gives better loss fluctuation information though your plots may get messy.</p>

username: nmtp
cooked: <p>Thank you again. That makes a little more sense. I’m still a little confused why the validation losses are significantly less than the training losses if that is the case. I can see that the reduction on the MSELoss was set to be “sum”; would the correct thing here be to set that to “elementwise-mean” to ensure that validation/training is the same?</p>
<p>Is there a reason why different reduction would be used?</p>

username: sathvik_udupa
cooked: <p>If it is set to sum, then you will get different scales of loss,<br>
L = sum of all(losses)*batch_size/N.<br>
With lower batch_size for validation, your loss will be lesser than train.</p>
<p>To correct this, you can either divide each batch loss with batch size or equivalently perform loss.mean(). This should give you proper values.</p>