-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
test_classifier_models.py
204 lines (153 loc) · 6.35 KB
/
test_classifier_models.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# encoding: utf-8
"""
@author: BrikerMan
@contact: eliyar917@gmail.com
@blog: https://eliyar.biz
@version: 1.0
@license: Apache Licence
@file: test_classifier_models.py
@time: 2019-01-27 13:28
"""
import time
import os
import random
import logging
import tempfile
import unittest
from kashgari.embeddings import WordEmbeddings, BERTEmbedding
from kashgari.tasks.classification import BLSTMModel, CNNModel, CNNLSTMModel, ClassificationModel
from kashgari.utils.logger import init_logger
init_logger()
SEQUENCE_LENGTH = 30
train_x = [
list('语言学(英语:linguistics)是一门关于人类语言的科学研究'),
list('语言学(英语:linguistics)是一门关于人类语言的科学研究'),
list('语言学(英语:linguistics)是一门关于人类语言的科学研究'),
list('语言学包含了几种分支领域。'),
list('在语言结构(语法)研究与意义(语义与语用)研究之间存在一个重要的主题划分'),
]
train_y = ['a', 'a', 'a', 'b', 'c']
eval_x = [
list('语言学是一门关于人类语言的科学研究。'),
list('语言学包含了几种分支领域。'),
list('在语言结构研究与意义研究之间存在一个重要的主题划分。'),
list('语法中包含了词法,句法以及语音。'),
list('语音学是语言学的一个相关分支,它涉及到语音与非语音声音的实际属性,以及它们是如何发出与被接收到的。'),
list('与学习语言不同,语言学是研究所有人类语文发展有关的一门学术科目。'),
list('在语言结构(语法)研究与意义(语义与语用)研究之间存在一个重要的主题划分'),
]
eval_y = ['a', 'a', 'a', 'b', 'c', 'a', 'c']
class EmbeddingManager(object):
word2vec_embedding = None
bert_embedding = None
@classmethod
def get_bert(cls):
if cls.bert_embedding is None:
dir_path = os.path.dirname(os.path.realpath(__file__))
bert_path = os.path.join(dir_path, 'data', 'test_bert_checkpoint')
cls.bert_embedding = BERTEmbedding(bert_path, sequence_length=15)
logging.info('bert_embedding seq len: {}'.format(cls.bert_embedding.sequence_length))
return cls.bert_embedding
@classmethod
def get_w2v(cls):
if cls.word2vec_embedding is None:
cls.word2vec_embedding = WordEmbeddings('sgns.weibo.bigram', sequence_length=SEQUENCE_LENGTH, limit=5000)
return cls.word2vec_embedding
class TestBLSTMModelModel(unittest.TestCase):
model: ClassificationModel = None
@classmethod
def setUpClass(cls):
cls.epochs = 3
cls.model = BLSTMModel()
def test_build(self):
self.model.fit(train_x, train_y, epochs=1)
assert len(self.model.label2idx) == 4
assert len(self.model.token2idx) > 4
def test_fit(self):
self.model.fit(train_x, train_y, eval_x, eval_y, epochs=self.epochs)
def test_fit_class_weight(self):
self.model.fit(train_x, train_y, eval_x, eval_y, class_weight=True, batch_size=128, epochs=2)
def test_label_token_convert(self):
self.test_fit()
assert isinstance(self.model.convert_label_to_idx('a'), int)
assert isinstance(self.model.convert_idx_to_label(1), str)
assert all(isinstance(i, int) for i in self.model.convert_label_to_idx(['a']))
assert all(isinstance(i, str) for i in self.model.convert_idx_to_label([1, 2]))
sentence = random.choice(eval_x)
tokens = self.model.embedding.tokenize(sentence)
assert min(30, len(sentence)+2) == min(len(tokens), SEQUENCE_LENGTH)
def test_predict(self):
self.test_fit()
sentence = list('语言学包含了几种分支领域。')
assert isinstance(self.model.predict(sentence), str)
assert isinstance(self.model.predict([sentence]), list)
logging.info('test predict: {} -> {}'.format(sentence, self.model.predict(sentence)))
self.model.predict(sentence, output_dict=True)
def test_eval(self):
self.test_fit()
self.model.evaluate(eval_x, eval_y)
def test_save_and_load(self):
self.test_fit()
model_path = os.path.join(tempfile.gettempdir(), 'kashgari_model', str(time.time()))
self.model.save(model_path)
new_model = BLSTMModel.load_model(model_path)
assert new_model is not None
sentence = list('语言学包含了几种分支领域。')
result = new_model.predict(sentence)
assert isinstance(result, str)
@classmethod
def tearDownClass(cls):
del cls.model
logging.info('tearDownClass {}'.format(cls))
class TestBLSTMModelWithWord2Vec(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 3
embedding = EmbeddingManager.get_w2v()
cls.model = BLSTMModel(embedding)
class TestBLSTMModelWithBERT(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 1
embedding = EmbeddingManager.get_bert()
cls.model = BLSTMModel(embedding)
def test_save_and_load(self):
super(TestBLSTMModelWithBERT, self).test_save_and_load()
class TestCNNModel(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 3
TestCNNModel.model = CNNModel()
def test_fit(self):
super(TestCNNModel, self).test_fit()
class TestCNNModelWithWord2Vec(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 3
embedding = EmbeddingManager.get_w2v()
cls.model = CNNModel(embedding)
class TestCNNModelWithBERT(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 1
embedding = EmbeddingManager.get_bert()
TestCNNModelWithBERT.model = CNNModel(embedding)
class TestLSTMCNNModel(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 3
cls.model = CNNLSTMModel()
class TestLSTMCNNModelWithWord2Vec(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 3
embedding = EmbeddingManager.get_w2v()
cls.model = CNNLSTMModel(embedding)
class TestLSTMCNNModelWithBERT(TestBLSTMModelModel):
@classmethod
def setUpClass(cls):
cls.epochs = 1
embedding = EmbeddingManager.get_bert()
cls.model = CNNLSTMModel(embedding)
if __name__ == "__main__":
unittest.main()