def cal_nll(model, data_loader, criterion, gpu=cfg.CUDA):
        """NLL score for general text generation model."""
        total_loss = 0
        with torch.no_grad():
            for i, data in enumerate(data_loader):
                inp, target = data['input'], data['target']
                if gpu:
                    inp, target = inp.cuda(), target.cuda()

                hidden = model.init_hidden(data_loader.batch_size)
                pred = model.forward(inp, hidden)
                loss = criterion(pred, target.view(-1))
                total_loss += loss.item()
        return round(total_loss / len(data_loader), 4)
NLL score for general text generation model.
metrics/nll.py