def validate(self) -> float:
    '\n        Validate an epoch.\n\n        Returns\n        -------\n        bleu4 : float\n            BLEU-4 score\n        '
    self.decoder.eval()
    if (self.encoder is not None):
        self.encoder.eval()
    batch_time = AverageMeter()
    losses = AverageMeter()
    top5accs = AverageMeter()
    start = time.time()
    ground_truth = list()
    prediction = list()
    with torch.no_grad():
        for (i, (imgs, caps, caplens, allcaps)) in enumerate(self.val_loader):
            imgs = imgs.to(self.device)
            caps = caps.to(self.device)
            caplens = caplens.to(self.device)
            if (self.encoder is not None):
                imgs = self.encoder(imgs)
            if (self.caption_model == 'att2all'):
                (scores, caps_sorted, decode_lengths, alphas, sort_ind) = self.decoder(imgs, caps, caplens)
            else:
                (scores, caps_sorted, decode_lengths, sort_ind) = self.decoder(imgs, caps, caplens)
            targets = caps_sorted[:, 1:]
            scores_copy = scores.clone()
            scores = pack_padded_sequence(scores, decode_lengths, batch_first=True)[0]
            targets = pack_padded_sequence(targets, decode_lengths, batch_first=True)[0]
            loss = self.loss_function(scores, targets)
            if (self.caption_model == 'att2all'):
                loss += (self.tau * ((1.0 - alphas.sum(dim=1)) ** 2).mean())
            losses.update(loss.item(), sum(decode_lengths))
            top5 = accuracy(scores, targets, 5)
            top5accs.update(top5, sum(decode_lengths))
            batch_time.update((time.time() - start))
            start = time.time()
            if ((i % self.print_freq) == 0):
                print('Validation: [{0}/{1}]\tBatch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\tLoss {loss.val:.4f} ({loss.avg:.4f})\tTop-5 Accuracy {top5.val:.3f} ({top5.avg:.3f})\t'.format(i, len(self.val_loader), batch_time=batch_time, loss=losses, top5=top5accs))
            allcaps = allcaps[sort_ind]
            for j in range(allcaps.shape[0]):
                img_caps = allcaps[j].tolist()
                img_captions = list(map((lambda c: [w for w in c if (w not in {self.word_map['<start>'], self.word_map['<pad>']})]), img_caps))
                ground_truth.append(img_captions)
            (_, preds) = torch.max(scores_copy, dim=2)
            preds = preds.tolist()
            temp_preds = list()
            for (j, p) in enumerate(preds):
                temp_preds.append(preds[j][:decode_lengths[j]])
            preds = temp_preds
            prediction.extend(preds)
            assert (len(ground_truth) == len(prediction))
        metrics = Metrics(ground_truth, prediction, self.rev_word_map)
        bleu4 = metrics.belu[3]
        cider = metrics.cider
        print('\n * LOSS - {loss.avg:.3f}, TOP-5 ACCURACY - {top5.avg:.3f}, BLEU-4 - {bleu}, CIDEr - {cider}\n'.format(loss=losses, top5=top5accs, bleu=bleu4, cider=cider))
    return bleu4
Validate an epoch.

Returns
-------
bleu4 : float
    BLEU-4 score
def validate(self) -> float:
    '\n        Validate an epoch.\n\n        Returns\n        -------\n        bleu4 : float\n            BLEU-4 score\n        '
    self.decoder.eval()
    if (self.encoder is not None):
        self.encoder.eval()
    batch_time = AverageMeter()
    losses = AverageMeter()
    top5accs = AverageMeter()
    start = time.time()
    ground_truth = list()
    prediction = list()
    with torch.no_grad():
        for (i, (imgs, caps, caplens, allcaps)) in enumerate(self.val_loader):
            imgs = imgs.to(self.device)
            caps = caps.to(self.device)
            caplens = caplens.to(self.device)
            if (self.encoder is not None):
                imgs = self.encoder(imgs)
            if (self.caption_model == 'att2all'):
                (scores, caps_sorted, decode_lengths, alphas, sort_ind) = self.decoder(imgs, caps, caplens)
            else:
                (scores, caps_sorted, decode_lengths, sort_ind) = self.decoder(imgs, caps, caplens)
            targets = caps_sorted[:, 1:]
            scores_copy = scores.clone()
            scores = pack_padded_sequence(scores, decode_lengths, batch_first=True)[0]
            targets = pack_padded_sequence(targets, decode_lengths, batch_first=True)[0]
            loss = self.loss_function(scores, targets)
            if (self.caption_model == 'att2all'):
                loss += (self.tau * ((1.0 - alphas.sum(dim=1)) ** 2).mean())
            losses.update(loss.item(), sum(decode_lengths))
            top5 = accuracy(scores, targets, 5)
            top5accs.update(top5, sum(decode_lengths))
            batch_time.update((time.time() - start))
            start = time.time()
            if ((i % self.print_freq) == 0):
                print('Validation: [{0}/{1}]\tBatch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\tLoss {loss.val:.4f} ({loss.avg:.4f})\tTop-5 Accuracy {top5.val:.3f} ({top5.avg:.3f})\t'.format(i, len(self.val_loader), batch_time=batch_time, loss=losses, top5=top5accs))
            allcaps = allcaps[sort_ind]
            for j in range(allcaps.shape[0]):
                img_caps = allcaps[j].tolist()
                img_captions = list(map((lambda c: [w for w in c if (w not in {self.word_map['<start>'], self.word_map['<pad>']})]), img_caps))
                ground_truth.append(img_captions)
            (_, preds) = torch.max(scores_copy, dim=2)
            preds = preds.tolist()
            temp_preds = list()
            for (j, p) in enumerate(preds):
                temp_preds.append(preds[j][:decode_lengths[j]])
            preds = temp_preds
            prediction.extend(preds)
            assert (len(ground_truth) == len(prediction))
        metrics = Metrics(ground_truth, prediction, self.rev_word_map)
        bleu4 = metrics.belu[3]
        cider = metrics.cider
        print('\n * LOSS - {loss.avg:.3f}, TOP-5 ACCURACY - {top5.avg:.3f}, BLEU-4 - {bleu}, CIDEr - {cider}\n'.format(loss=losses, top5=top5accs, bleu=bleu4, cider=cider))
    return bleu4