udacity / deep-learning-v2-pytorch

Projects and exercises for the latest Deep Learning ND program https://www.udacity.com/course/deep-learning-nanodegree--nd101
MIT License
5.26k stars 5.32k forks source link

why there is difference for averaging validation loss and testloss #415

Open guth0b opened 1 year ago

guth0b commented 1 year ago

to get total average valid loss you divide by len(valid_loader) but for avaerage test loss you divide by len(test_loader.dataset). This does not seems correct. And also you are multiplying the loss with batch_size in test runs, but you dont do it for validation. Why?

Siddharth-Latthe-07 commented 1 month ago

Validation Loss: Per Batch Averaging: During validation, you might be interested in how well the model is performing per batch, especially if batch sizes are consistent. Thus, dividing by the number of batches is sufficient. Simplification: Assuming consistent batch sizes, the validation loss calculation can be simplified by dividing by the number of batches.

Test Loss: Per Sample Averaging: For the test set, it's common to report the loss per sample, giving a more precise evaluation of the model's performance on each data point. Accuracy: Multiplying by the batch size ensures that the total loss reflects all samples accurately, especially if the last batch has fewer samples.

sample snippet for consistent approach:-

  1. validation loss:-
    
    total_valid_loss = 0.0
    total_valid_samples = 0
    for data in valid_loader:
    inputs, labels = data
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    total_valid_loss += loss.item() * inputs.size(0)  # Multiply by batch size
    total_valid_samples += inputs.size(0)

average_valid_loss = total_valid_loss / total_valid_samples # Divide by total samples

2. Test Loss:- 

total_test_loss = 0.0 total_test_samples = 0 for data in test_loader: inputs, labels = data outputs = model(inputs) loss = criterion(outputs, labels) total_test_loss += loss.item() * inputs.size(0) # Multiply by batch size total_test_samples += inputs.size(0)

average_test_loss = total_test_loss / total_test_samples # Divide by total samples


Hope, this helps 
Thanks