import torch


def accuracy(testloader, model, criterion, gpu):
    """
        Compute how accurate the trained model predicts data it was not trained on
    """
    correct = 0
    total = 0
    test_loss = 0

    with torch.no_grad():
        for data in testloader:
            images, labels = data
            if gpu==True:
                images, labels = images.to('cuda'), labels.to('cuda')

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

            #tensor with probability, tensor with index of flower category
            prob = torch.exp(output) #tensor with prob. of each flower category
            pred = prob.max(dim=1) #tensor giving us flower label most likely

            #calculate number correct
            matches = (pred[1] == labels.data)
            correct += matches.sum().item()
            total += 64

        acc = 100*(correct/total)
        return acc