def compute_validation_loss(
    config, model, train_iterator, criterion, task_idx, lowercase=False
):
    """Compute validation loss for a task.

    Args:
        config(dict): configuration list.
        model(MultitaskModel): model.
        train_iterator(BufferedDataIterator): Multi Parallel corpus data iterator.
        criterion(nn.CrossEntropyLoss): criterion function for loss.
        task_idx(int): Task index.
        lowercase(bool): If lowercase the data.

    Returns: float as the mean of the loss.

    """
    val_src = config["data"]["paths"][task_idx]["val_src"]
    val_trg = config["data"]["paths"][task_idx]["val_trg"]

    if lowercase:
        val_src = [
            line.strip().lower().split()
            for line in open(val_src, "r", encoding="utf-8")
        ]
        val_trg = [
            line.strip().lower().split()
            for line in open(val_trg, "r", encoding="utf-8")
        ]
    else:
        val_src = [
            line.strip().split()
            for line in open(val_src, "r", encoding="utf-8")
        ]
        val_trg = [
            line.strip().split()
            for line in open(val_trg, "r", encoding="utf-8")
        ]

    batch_size = config["training"]["batch_size"]
    losses = []
    for j in range(0, len(val_src), batch_size):
        minibatch = get_validation_minibatch(
            val_src,
            val_trg,
            j,
            batch_size,
            train_iterator.src[task_idx]["word2id"],
            train_iterator.trg[task_idx]["word2id"],
        )
        decoder_logit = model(minibatch, task_idx)

        loss = criterion(
            decoder_logit.contiguous().view(-1, decoder_logit.size(2)),
            minibatch["output_trg"].contiguous().view(-1),
        )

        # losses.append(loss.data[0])
        losses.append(loss.item())

    return np.mean(losses)
Compute validation loss for a task.

    Args:
        config(dict): configuration list.
        model(MultitaskModel): model.
        train_iterator(BufferedDataIterator): Multi Parallel corpus data iterator.
        criterion(nn.CrossEntropyLoss): criterion function for loss.
        task_idx(int): Task index.
        lowercase(bool): If lowercase the data.

    Returns: float as the mean of the loss.

    
utils_nlp/models/gensen/utils.py