示例#1
0
    def forward(self, inputs, char_seq_length):
        f_lstm_out, b_lstm_out = self.get_lstm_features(
            inputs, char_seq_length)

        lengths = list(map(int, char_seq_length))
        rb_lstm_out = reverse_padded_sequence(b_lstm_out, lengths)

        lstm_out = torch.cat((f_lstm_out, rb_lstm_out), dim=-1)

        return lstm_out, (f_lstm_out, b_lstm_out)
示例#2
0
文件: cw_ner.py 项目: wml1993/CCW-NER
    def forward(self, gaz_list, reverse_gaz_list, word_inputs, word_seq_lengths, mask):
        """
        Args:
            gaz_list: the forward gaz_list
            reverse_gaz_list: the backward gaz list
            word_inputs: word ids
            word_seq_lengths: each sentence length
            mask: sentence mask
        """
        batch_size = word_inputs.size(0)
        seq_len = word_inputs.size(1)
        lengths = list(map(int, word_seq_lengths))

        ## get batch gaz ids
        batch_gaz_ids, batch_gaz_length, batch_gaz_mask = get_batch_gaz(reverse_gaz_list, batch_size, seq_len, self.gpu)
        reverse_batch_gaz_ids, reverse_batch_gaz_length, reverse_batch_gaz_mask = get_batch_gaz(gaz_list, batch_size, seq_len, self.gpu)
        reverse_batch_gaz_ids = reverse_padded_sequence(reverse_batch_gaz_ids, lengths)
        reverse_batch_gaz_length = reverse_padded_sequence(reverse_batch_gaz_length, lengths)
        reverse_batch_gaz_mask = reverse_padded_sequence(reverse_batch_gaz_mask, lengths)

        ## word embedding
        word_embs = self.word_embedding(word_inputs)
        reverse_word_embs = reverse_padded_sequence(word_embs, lengths)

        ## gaz embedding
        gaz_embs = self.gaz_embed((batch_gaz_ids, batch_gaz_length, batch_gaz_mask))
        reverse_gaz_embs = self.gaz_embed((reverse_batch_gaz_ids, reverse_batch_gaz_length, reverse_batch_gaz_mask))

        ## lstm
        forward_inputs = torch.cat((word_embs, gaz_embs), dim=-1)
        backward_inputs = torch.cat((reverse_word_embs, reverse_gaz_embs), dim=-1)

        lstm_outs, _ = self.lstm((forward_inputs, backward_inputs), word_seq_lengths)

        ## hidden2tag
        outs = self.hidden2tag(lstm_outs)

        ## crf and loss
        _, tag_seq = self.crf._viterbi_decode(outs, mask)

        return tag_seq
示例#3
0
    def forward(self, char_inputs, char_seq_lengths, mask):
        batch_size = char_inputs.size(0)
        sent_len = char_inputs.size(1)

        lengths = list(map(int, char_seq_lengths))
        reverse_char_inputs = reverse_padded_sequence(char_inputs, lengths)

        char_emb = self.char_embedding(char_inputs)
        reverse_char_emb = self.char_embedding(reverse_char_inputs)

        lstm_out, _ = self.lstm((char_emb, reverse_char_emb), char_seq_lengths)

        # softmax
        outs = self.hidden2tag(lstm_out)

        # CRF
        _, tag_seq = self.crf._viterbi_decode(outs, mask)

        return tag_seq
示例#4
0
    def neg_log_likelihood_loss(self, char_inputs, char_seq_lengths, batch_label, mask):
        batch_size = char_inputs.size(0)
        sent_len = char_inputs.size(1)

        lengths = list(map(int, char_seq_lengths))
        reverse_char_inputs = reverse_padded_sequence(char_inputs, lengths)

        char_emb = self.char_embedding(char_inputs)
        reverse_char_emb = self.char_embedding(reverse_char_inputs)

        lstm_out, _ = self.lstm((char_emb, reverse_char_emb), char_seq_lengths)

        # softmax
        outs = self.hidden2tag(lstm_out)

        # CRF
        ## crf and loss
        loss = self.crf.neg_log_likelihood_loss(outs, mask, batch_label)
        _, tag_seq = self.crf._viterbi_decode(outs, mask)

        return loss, tag_seq