Skip to content

fix(chapter-2): add missing loss_function to the test function#2

Open
imteekay wants to merge 1 commit intolmoroney:mainfrom
imteekay:fix-chapter-2
Open

fix(chapter-2): add missing loss_function to the test function#2
imteekay wants to merge 1 commit intolmoroney:mainfrom
imteekay:fix-chapter-2

Conversation

@imteekay
Copy link

@imteekay imteekay commented Nov 16, 2025

- def test(dataloader, model):
+ def test(dataloader, model, loss_function):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    model.eval()  # Set the model to evaluation mode
    test_loss, correct = 0, 0
    with torch.no_grad():
        for X, y in dataloader:
            pred = model(X)
            test_loss += loss_function(pred, y).item()
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= num_batches
    correct /= size
    print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")

# Evaluate the model
- test(test_loader, model)
+ test(test_loader, model, loss_function)

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

Successfully merging this pull request may close these issues.

1 participant