-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_add_on.py
49 lines (41 loc) · 2.06 KB
/
test_add_on.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
import unittest
from easy_bert.bert4classification.classification_predictor import ClassificationPredictor
from easy_bert.bert4classification.classification_trainer import ClassificationTrainer
from easy_bert.bert4sequence_labeling.sequence_labeling_predictor import SequenceLabelingPredictor
from easy_bert.bert4sequence_labeling.sequence_labeling_trainer import SequenceLabelingTrainer
class MyTestCase(unittest.TestCase):
def test_bert2classification(self):
print('test_bert2classification~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
pretrained_model_dir, model_dir = './models/chinese-roberta-wwm-ext', './tests/test_model'
texts = [
'天气真好',
'今天运气很差',
]
labels = ['正面', '负面']
trainer = ClassificationTrainer(
pretrained_model_dir, model_dir, add_on='bilstm', rnn_hidden=256, rnn_lr=1e-3
)
trainer.train(texts, labels, validate_texts=texts, validate_labels=labels, batch_size=2, epoch=20)
predictor = ClassificationPredictor(pretrained_model_dir, model_dir)
labels = predictor.predict(texts)
print(labels)
def test_bert2sequence_labeling(self):
print('test_bert2sequence_labeling~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
pretrained_model_dir, model_dir = './models/chinese-roberta-wwm-ext', './tests/test_model'
texts = [
['你', '好', '呀'],
['一', '马', '当', '先', '就', '是', '好'],
]
labels = [
['B', 'E', 'S'],
['B', 'M', 'M', 'E', 'S', 'S', 'S']
]
trainer = SequenceLabelingTrainer(
pretrained_model_dir, model_dir, add_on='bilstm', rnn_hidden=256, rnn_lr=1e-3
)
trainer.train(texts, labels, validate_texts=texts, validate_labels=labels, batch_size=2, epoch=20)
predictor = SequenceLabelingPredictor(pretrained_model_dir, model_dir)
labels = predictor.predict(texts)
print(labels)
if __name__ == '__main__':
unittest.main()