import torch


def compute_accuracy(net, data_loader, device, print_result=True):
    """
    Computes accuracy of a given network.
    :param net:                 torch.nn.Module, network
    :param data_loader:         torch.utils.data.DataLoader
    :param device:              torch.device
    :param print_result:        bool, whether to print the found accuracy or not
    :return: float, accuracy in percentage
    """
    correct = 0
    total = 0

    net.eval()

    with torch.no_grad():
        for data in data_loader:
            images, labels = data[0].to(device), data[1].to(device)

            outputs = net(images, labels)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

    net.train()

    if print_result:
        print('Accuracy: %.1f %%' % (
                100 * correct / total))
    return 100 * correct / total