Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
guth0b opened this issue Jun 7, 2023 · 1 comment
Open

Comments

@guth0b
Copy link

guth0b commented Jun 7, 2023

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
Copy link

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
  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants