import torch


def test(model, test_loader, criterion):
    """ Test the model.

    :param model: The model to test
    :param test_loader: DataLoader with the test data
    :param criterion: Criterion to use for loss calculation
    :return: Loss of the test
    """
    model.eval()

    test_loss = 0

    with torch.no_grad():
        for data, target in test_loader:
            output = model(data)

            test_loss += criterion(output, target).item() * data.size(0)

    test_loss /= len(test_loader.dataset)

    return test_loss