import torch


def validate(model, dl_val, device):
    """
    run a complete validation epoch
    :param model:
    :param dl_val:
    :param device:
    :return: validation loss
    """
    model.eval()
    val_loss = 0
    with torch.no_grad():
        for X, Y in dl_val:
            X = X.to(device)
            Y = Y.to(device)
            loss, _ = model(X, Y)
            loss = loss.mean()
            val_loss += loss.item()
    val_loss /= len(dl_val)
    return val_loss