Exemplo n.º 1
0
    def predict(self, seqs, demo=True):
        if demo:
            input_X = dev2vec(seqs,
                              word_dict=self.vocab,
                              max_seq_len=self.sequence_length)
        else:
            input_X, _ = pad_sequences(seqs)

        inputs = torch.LongTensor(input_X)
        outputs = self(inputs)
        return torch.argmax(outputs, dim=1).numpy()
Exemplo n.º 2
0
    def predict_prob(self, seqs, demo=True):
        if demo:
            input_X = dev2vec(seqs,
                              word_dict=self.vocab,
                              max_seq_len=self.sequence_length)
        else:
            input_X, _ = pad_sequences(seqs)

        inputs = torch.LongTensor(input_X)
        outputs = torch.exp(self(inputs))
        # softmax
        outputs_softmax = outputs / torch.Tensor.reshape(
            torch.sum(outputs, dim=1), [-1, 1])
        return outputs_softmax.detach().numpy()
Exemplo n.º 3
0
    def predict_prob(self, sess, seqs, demo=True):
        """预测概率"""
        if demo:
            input_X = dev2vec(seqs,
                              word_dict=self.vocab,
                              max_seq_len=self.sequence_length)
        else:
            input_X, _ = pad_sequences(seqs)
        possibility = sess.run(self.possibility,
                               feed_dict={
                                   self.input_x: input_X,
                                   self.dropout_kp: 1.0
                               })

        return possibility
Exemplo n.º 4
0
 def predict(self, sess, seqs, demo=True):
     """预测标签"""
     if demo:
         input_X = dev2vec(seqs,
                           word_dict=self.vocab,
                           max_seq_len=self.sequence_length)
     else:
         input_X, _ = pad_sequences(seqs)
     predictions = sess.run(self.predictions,
                            feed_dict={
                                self.input_x: input_X,
                                self.keep_prob: 1.0,
                                self.tst: True
                            })
     return predictions
Exemplo n.º 5
0
def train():
    sents, tags = read_data()
    x = dev2vec(sents, word2int, max_seq_len=FLAGS.sequence_length)
    y = [tag2label[t] for t in tags]

    model_path = os.path.join(MODEL_PATH, FLAGS.DEMO)
    if not os.path.exists(model_path):
        os.makedirs(model_path)
    model_path = os.path.join(model_path, 'm.h5')

    x = np.array(x)
    y = np_utils.to_categorical(y)

    print(x.shape, y.shape)
    train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.2, random_state=421)
    lstm = Lstm(model_path, word2int, tag2label, input_length=FLAGS.sequence_length)
    lstm.train(train_x, train_y, test_x, test_y)