import torch


def validating(network, loader):
    """
    Validating the network during and after training

    :param network: trained network
    :param loader: dataloader
    :return: accuracy
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    correct_num = 0
    total_num = 0
    for i, data in enumerate(loader, 0):
        digits, labels = data
        total_num += labels.size(0)
        digits, labels = digits.to(device), labels.to(device)
        outputs = network(digits)
        _, predicted = torch.max(outputs, 1)
        correct_num += ((predicted == labels).sum().to("cpu")).item()
    accuracy = correct_num / total_num
    return accuracy