-
Notifications
You must be signed in to change notification settings - Fork 54
/
predict.py
84 lines (75 loc) · 2.64 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import torch
import pickle as pkl
import numpy as np
from importlib import import_module
key = {
0: 'finance',
1: 'realty',
2: 'stocks',
3: 'education',
4: 'science',
5: 'society',
6: 'politics',
7: 'sports',
8: 'game',
9: 'entertainment'
}
class Predict:
def __init__(self, model_name='TextCNN', dataset='THUCNews', embedding='embedding_SougouNews.npz', use_word=False):
if use_word:
self.tokenizer = lambda x: x.split(' ') # 以空格隔开,word-level
else:
self.tokenizer = lambda x: [y for y in x] # char-level
self.x = import_module('models.' + model_name)
self.config = self.x.Config(dataset, embedding)
self.vocab = pkl.load(open(self.config.vocab_path, 'rb'))
self.pad_size = self.config.pad_size
self.model = self.x.Model(self.config).to('cpu')
self.model.load_state_dict(torch.load(self.config.save_path, map_location='cpu'))
def build_predict_text(self, texts):
words_lines = []
seq_lens = []
for text in texts:
words_line = []
token = self.tokenizer(text)
seq_len = len(token)
if self.pad_size:
if len(token) < self.pad_size:
token.extend(['<PAD>'] * (self.pad_size - len(token)))
else:
token = token[:self.pad_size]
seq_len = self.pad_size
# word to id
for word in token:
words_line.append(self.vocab.get(word, self.vocab.get('<UNK>')))
words_lines.append(words_line)
seq_lens.append(seq_len)
return torch.LongTensor(words_lines), torch.LongTensor(seq_lens)
def predict(self, query):
query = [query]
# 返回预测的索引
data = self.build_predict_text(query)
with torch.no_grad():
outputs = self.model(data)
num = torch.argmax(outputs)
return key[int(num)]
def predict_list(self, querys):
# 返回预测的索引
data = self.build_predict_text(querys)
with torch.no_grad():
outputs = self.model(data)
num = torch.argmax(outputs, dim=1)
pred = [key[index] for index in list(np.array(num))]
return pred
if __name__ == "__main__":
pred = Predict('TextCNN')
# 预测一条
query = "学费太贵怎么办?"
print(pred.predict(query))
# 预测一个列表
querys = ["学费太贵怎么办?", "金融怎么样"]
print(pred.predict_list(querys))