(self, batch, output, target, copy_attn, align)
return loss, stats
Compute the loss. The args must match self._make_shard_state().
        Args:
            batch: the current batch.
            output: the predict output from the model.
            target: the validate target to compare output with.
            copy_attn: the copy attention value.
            align: the align info.
Compute the loss. The args must match self._make_shard_state().
        Args:
            batch: the current batch.
            output: the predict output from the model.
            target: the validate target to compare output with.
            copy_attn: the copy attention value.
            align: the align info.
Compute the loss . The args must match self . _make_shard_state () . Args : batch : the current batch . output : the predict output from the model . target : the validate target to compare output with . copy_attn : the copy attention value . align : the align info .
def _compute_loss(self, batch, output, target, copy_attn, align):
        """
        Compute the loss. The args must match self._make_shard_state().
        Args:
            batch: the current batch.
            output: the predict output from the model.
            target: the validate target to compare output with.
            copy_attn: the copy attention value.
            align: the align info.
        """
        target = target.view(-1)
        align = align.view(-1)
        scores = self.generator(self._bottle(output),
                                self._bottle(copy_attn),
                                batch.src_map)
        loss = self.criterion(scores, align, target)
        scores_data = scores.data.clone()
        scores_data = inputters.TextDataset.collapse_copy_scores(
            self._unbottle(scores_data, batch.batch_size),
            batch, self.tgt_vocab, batch.dataset.src_vocabs)
        scores_data = self._bottle(scores_data)

        # Correct target copy token instead of <unk>
        # tgt[i] = align[i] + len(tgt_vocab)
        # for i such that tgt[i] == 0 and align[i] != 0
        target_data = target.data.clone()
        correct_mask = target_data.eq(0) * align.data.ne(0)
        correct_copy = (align.data + len(self.tgt_vocab)) * correct_mask.long()
        target_data = target_data + correct_copy

        # Compute sum of perplexities for stats
        loss_data = loss.sum().data.clone()
        stats = self._stats(loss_data, scores_data, target_data)

        if self.normalize_by_length:
            # Compute Loss as NLL divided by seq length
            # Compute Sequence Lengths
            pad_ix = batch.dataset.fields['tgt'].vocab.stoi[inputters.PAD_WORD]
            tgt_lens = batch.tgt.ne(pad_ix).float().sum(0)
            # Compute Total Loss per sequence in batch
            loss = loss.view(-1, batch.batch_size).sum(0)
            # Divide by length of each sequence and sum
            loss = torch.div(loss, tgt_lens).sum()
        else:
            loss = loss.sum()

        return loss, stats
def _compute_loss ( self , batch , output , target , copy_attn , align ) : target = target . view ( - 1 ) align = align . view ( - 1 ) scores = self . generator ( self . _bottle ( output ) , self . _bottle ( copy_attn ) , batch . src_map ) loss = self . criterion ( scores , align , target ) scores_data = scores . data . clone ( ) scores_data = inputters . TextDataset . collapse_copy_scores ( self . _unbottle ( scores_data , batch . batch_size ) , batch , self . tgt_vocab , batch . dataset . src_vocabs ) scores_data = self . _bottle ( scores_data ) # Correct target copy token instead of <unk> # tgt[i] = align[i] + len(tgt_vocab) # for i such that tgt[i] == 0 and align[i] != 0 target_data = target . data . clone ( ) correct_mask = target_data . eq ( 0 ) * align . data . ne ( 0 ) correct_copy = ( align . data + len ( self . tgt_vocab ) ) * correct_mask . long ( ) target_data = target_data + correct_copy # Compute sum of perplexities for stats loss_data = loss . sum ( ) . data . clone ( ) stats = self . _stats ( loss_data , scores_data , target_data ) if self . normalize_by_length : # Compute Loss as NLL divided by seq length # Compute Sequence Lengths pad_ix = batch . dataset . fields [ 'tgt' ] . vocab . stoi [ inputters . PAD_WORD ] tgt_lens = batch . tgt . ne ( pad_ix ) . float ( ) . sum ( 0 ) # Compute Total Loss per sequence in batch loss = loss . view ( - 1 , batch . batch_size ) . sum ( 0 ) # Divide by length of each sequence and sum loss = torch . div ( loss , tgt_lens ) . sum ( ) else : loss = loss . sum ( ) return loss , stats