def train(self, epoch: int) -> None:
    '\n        Train an epoch\n\n        Parameters\n        ----------\n        epoch : int\n            Current number of epoch\n        '
    self.decoder.train()
    self.encoder.train()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter(tag='loss', writer=self.writer)
    top5accs = AverageMeter(tag='top5acc', writer=self.writer)
    start = time.time()
    for (i, (imgs, caps, caplens)) in enumerate(self.train_loader):
        data_time.update((time.time() - start))
        imgs = imgs.to(self.device)
        caps = caps.to(self.device)
        caplens = caplens.to(self.device)
        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 = 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())
        self.decoder_optimizer.zero_grad()
        if (self.encoder_optimizer is not None):
            self.encoder_optimizer.zero_grad()
        loss.backward()
        if (self.grad_clip is not None):
            clip_gradient(self.decoder_optimizer, self.grad_clip)
            if (self.encoder_optimizer is not None):
                clip_gradient(self.encoder_optimizer, self.grad_clip)
        self.decoder_optimizer.step()
        if (self.encoder_optimizer is not None):
            self.encoder_optimizer.step()
        step = (((epoch - 1) * self.len_epoch) + i)
        self.writer.set_step(step=step, mode='train')
        top5 = accuracy(scores, targets, 5)
        losses.update(loss.item(), sum(decode_lengths))
        top5accs.update(top5, sum(decode_lengths))
        batch_time.update((time.time() - start))
        start = time.time()
        if ((i % self.print_freq) == 0):
            print('Epoch: [{0}][{1}/{2}]\tBatch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\tData Load Time {data_time.val:.3f} ({data_time.avg:.3f})\tLoss {loss.val:.4f} ({loss.avg:.4f})\tTop-5 Accuracy {top5.val:.3f} ({top5.avg:.3f})'.format(epoch, i, len(self.train_loader), batch_time=batch_time, data_time=data_time, loss=losses, top5=top5accs))
Train an epoch

Parameters
----------
epoch : int
    Current number of epoch
def train(self, epoch: int) -> None:
    '\n        Train an epoch\n\n        Parameters\n        ----------\n        epoch : int\n            Current number of epoch\n        '
    self.decoder.train()
    self.encoder.train()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter(tag='loss', writer=self.writer)
    top5accs = AverageMeter(tag='top5acc', writer=self.writer)
    start = time.time()
    for (i, (imgs, caps, caplens)) in enumerate(self.train_loader):
        data_time.update((time.time() - start))
        imgs = imgs.to(self.device)
        caps = caps.to(self.device)
        caplens = caplens.to(self.device)
        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 = 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())
        self.decoder_optimizer.zero_grad()
        if (self.encoder_optimizer is not None):
            self.encoder_optimizer.zero_grad()
        loss.backward()
        if (self.grad_clip is not None):
            clip_gradient(self.decoder_optimizer, self.grad_clip)
            if (self.encoder_optimizer is not None):
                clip_gradient(self.encoder_optimizer, self.grad_clip)
        self.decoder_optimizer.step()
        if (self.encoder_optimizer is not None):
            self.encoder_optimizer.step()
        step = (((epoch - 1) * self.len_epoch) + i)
        self.writer.set_step(step=step, mode='train')
        top5 = accuracy(scores, targets, 5)
        losses.update(loss.item(), sum(decode_lengths))
        top5accs.update(top5, sum(decode_lengths))
        batch_time.update((time.time() - start))
        start = time.time()
        if ((i % self.print_freq) == 0):
            print('Epoch: [{0}][{1}/{2}]\tBatch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\tData Load Time {data_time.val:.3f} ({data_time.avg:.3f})\tLoss {loss.val:.4f} ({loss.avg:.4f})\tTop-5 Accuracy {top5.val:.3f} ({top5.avg:.3f})'.format(epoch, i, len(self.train_loader), batch_time=batch_time, data_time=data_time, loss=losses, top5=top5accs))