import torch


def validate(model, device, validation_loader, criterion):
    """
    Run validation on the model

    Parameters
    ----------
    model : pytorch model
        Fully instantiated and configured pytorch model
    device : str
        Device to use for validation: {'cpu', 'gpu'}
    validation_loader : pytorch data loader
        Images used for validation
    criterion : pytorch function
        Formula for evaluating model

    Returns
    -------
    tuple of floats
        Loss and accuracy calculated from the validation
    """

    model.eval()

    validation_loss = 0
    accuracy = 0

    with torch.no_grad():
        for inputs, labels in iter(validation_loader):
            inputs, labels = inputs.to(device), labels.to(device)
            outputs = model.forward(inputs)
            validation_loss += criterion(outputs, labels).item()
            ps = torch.exp(outputs)
            equality = (labels.data == ps.max(dim=1)[1])
            accuracy += equality.type(torch.FloatTensor).mean()

    model.train()

    return validation_loss, accuracy