예제 #1
0
    def __init__(self, data=None, device=None, is_test=False, keep=0):
        """Create a Batch from a list of examples."""
        if data is not None:
            self.batch_size = len(data)
            if keep:
                data0 = [x[:-6] for x in data]
                data1 = [x[-6:] for x in data]
            else:
                data0 = data
            # (src_s, tgt, segs_s, clss_s, src_sent_labels_s, org_sent_labels_s, poss_s, src_txt_s, tgt_txt,
            #  src_txt), (src, segs, clss, src_sent_labels, org_sent_labels, poss)
            pre_src_s = [x[0] for x in data0]
            pre_segs_s = [x[2] for x in data0]
            pre_clss_s = [x[3] for x in data0]
            pre_src_sent_labels_s = [x[4] for x in data0]
            org_sent_labels_s = [x[5] for x in data0]
            # print('data0 item len ', len(data0[-1]),len(data0[-2]), ' | data1 item len: ', len(data1[-1]),len(data1[-2]))
            poss_s = [x[6] for x in data0]
            self.batch_pre(pre_src=pre_src_s,
                           pre_segs=pre_segs_s,
                           pre_clss=pre_clss_s,
                           pre_src_sent_labels=pre_src_sent_labels_s,
                           org_sent_labels=org_sent_labels_s,
                           poss=poss_s,
                           device=device,
                           postfix='_s')
            if keep:
                pre_src = [x[0] for x in data1]
                pre_segs = [x[1] for x in data1]
                pre_clss = [x[2] for x in data1]
                pre_src_sent_labels = [x[3] for x in data1]
                org_sent_labels = [x[4] for x in data1]
                poss = [x[5] for x in data1]
                self.batch_pre(pre_src=pre_src,
                               pre_segs=pre_segs,
                               pre_clss=pre_clss,
                               pre_src_sent_labels=pre_src_sent_labels,
                               org_sent_labels=org_sent_labels,
                               poss=poss,
                               device=device,
                               postfix='')
            pre_tgt = [x[1] for x in data0]
            tgt = torch.tensor(self._pad(pre_tgt, 0))
            mask_tgt = logical_not(tgt == 0)
            setattr(self, 'tgt', tgt.to(device))
            setattr(self, 'mask_tgt', mask_tgt.to(device))

            if (is_test):
                src_str_s = [x[-3] for x in data0]
                setattr(self, 'src_str_s', src_str_s)
                src_str = [x[-2] for x in data0]
                setattr(self, 'src_str', src_str)
                tgt_str = [x[-1] for x in data0]
                setattr(self, 'tgt_str', tgt_str)
예제 #2
0
    def __init__(self, data=None, device=None, is_test=False):
        """Create a Batch from a list of examples."""
        if data is not None:
            self.batch_size = len(data)
            pre_src = [x[0] for x in data]
            pre_tgt = [x[1] for x in data]
            pre_segs = [x[2] for x in data]
            pre_clss = [x[3] for x in data]
            pre_src_sent_labels = [x[4] for x in data]

            src = torch.tensor(self._pad(pre_src, 0))
            tgt = torch.tensor(self._pad(pre_tgt, 0))

            segs = torch.tensor(self._pad(pre_segs, 0))
            mask_src = logical_not(src == 0)
            mask_tgt = logical_not(tgt == 0)


            clss = torch.tensor(self._pad(pre_clss, -1))
            src_sent_labels = torch.tensor(self._pad(pre_src_sent_labels, 0))
            mask_cls = logical_not(clss == -1)
            clss[clss == -1] = 0
            setattr(self, 'clss', clss.to(device))
            setattr(self, 'mask_cls', mask_cls.to(device))
            setattr(self, 'src_sent_labels', src_sent_labels.to(device))


            setattr(self, 'src', src.to(device))
            setattr(self, 'tgt', tgt.to(device))
            setattr(self, 'segs', segs.to(device))
            setattr(self, 'mask_src', mask_src.to(device))
            setattr(self, 'mask_tgt', mask_tgt.to(device))


            if (is_test):
                src_str = [x[-2] for x in data]
                setattr(self, 'src_str', src_str)
                tgt_str = [x[-1] for x in data]
                setattr(self, 'tgt_str', tgt_str)
예제 #3
0
    def batch_pre(self, pre_src, pre_segs, pre_clss, pre_src_sent_labels,
                  org_sent_labels, poss, device, postfix):
        src = torch.tensor(self._pad(pre_src, 0))
        segs = torch.tensor(self._pad(pre_segs, 0))
        mask_src = logical_not(src == 0)
        clss = torch.tensor(self._pad(pre_clss, -1))
        src_sent_labels = torch.tensor(self._pad(pre_src_sent_labels, 0))
        mask_cls = logical_not(clss == -1)
        clss[clss == -1] = 0
        setattr(self, 'clss' + postfix, clss.to(device))
        setattr(self, 'mask_cls' + postfix, mask_cls.to(device))
        setattr(self, 'src_sent_labels' + postfix, src_sent_labels.to(device))
        setattr(self, 'src' + postfix, src.to(device))
        setattr(self, 'segs' + postfix, segs.to(device))
        setattr(self, 'mask_src' + postfix, mask_src.to(device))
        # 下面都是要预测的给他pad -1, 意思是看到-1 就停止算loss, 不用计算mask ,mask 是作为输入时才要的
        org_sent_labels = torch.tensor(self._pad(org_sent_labels, -1))
        setattr(self, 'org_sent_labels' + postfix, org_sent_labels.to(device))
        poss = torch.tensor(self._pad(
            poss, -1))  # 和sent albels 不一样,不能用0pad, 因为0也是一个意义值,用-1合适
        setattr(self, 'poss' + postfix, poss.to(device))

        assert torch.equal(logical_not(poss == -1),
                           mask_cls)  # 2020/3/30 18:18 如果这关过了就很开心了
예제 #4
0
    def forward(self, top_vecs, mask):
        """ See :obj:`EncoderBase.forward()`"""

        batch_size, n_sents = top_vecs.size(0), top_vecs.size(1)
        pos_emb = self.pos_emb.pe[:, :n_sents]
        x = top_vecs * mask[:, :, None].float()
        x = x + pos_emb

        for i in range(self.num_inter_layers):
            x = self.transformer_inter[i](
                i, x, x, logical_not(mask))  # all_sents * max_tokens * dim

        x = self.layer_norm(x)
        sent_scores = self.sigmoid(self.wo(x))
        sent_scores = sent_scores.squeeze(-1) * mask.float()

        return sent_scores
 def forward(self, top_vecs, mask):
     batch_size, n_sents = top_vecs.size(0), top_vecs.size(1)
     x = top_vecs * mask[:, :, None].float()
     if self.pos_emb is not None:
         pos_emb = self.pos_emb.pe[:, :n_sents]
         x = x + pos_emb
     # B x T x C -> T x B x C
     x = x.transpose(0, 1)
     encoder_padding_mask = logical_not(mask)
     for layer in self.layers:
         x = layer(x, encoder_padding_mask)  # all_sents * max_tokens * dim
     if self.normalize:
         x = self.layer_norm(x)
     return {
         'encoder_out': x,  # T x B x C
         'encoder_padding_mask': encoder_padding_mask,  # B x T
     }