import torch


def validation(model, validateloader, criterion, gpu):
    """
     INPUTS:
     model: a PyTorch model
     validateloader (dataloader): a dataloader for the validation images
     criterion: a loss function
     gpu (bool): whether a GPU is available

     OUTPUT:
     validation_loss: the validation loss on the validation set
     validation_accuracy: accuracy on the validation set
     """

    # We run our validation on the GPU as well
    if gpu:
        model.to('cuda')
        
    validation_loss = 0
    accuracy = 0
    for images, labels in validateloader:
        
        if gpu:
            images, labels = images.to('cuda'), labels.to('cuda')
            
        output = model.forward(images)
        validation_loss += criterion(output, labels).item()

        ps = torch.exp(output)
        equality = (labels.data == ps.max(dim=1)[1])
        accuracy += equality.type(torch.FloatTensor).mean()
    
    return validation_loss, accuracy