import torch


def validate(model, testloader, criterion):
    """Validate the model

    Arguments:
        model -- The network to validate
        testloader -- Test data
        criterion -- Loss function to use
    """
    test_loss = 0
    accuracy = 0
    for images, targets in testloader:
        images.resize_(images.size()[0], 784)

        output = model.forward(images)
        test_loss += criterion(output, targets).item()

        class_probabilities = torch.exp(output)
        equality = (targets.data == class_probabilities.max(dim=1)[1])
        accuracy += equality.type(torch.FloatTensor).mean()

    return test_loss, accuracy