-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
73 lines (57 loc) · 2 KB
/
main.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
# -*- coding: utf-8 -*-
"""
@Time : 2019-08-08 18:34
@FileName: main.py
@author: 王炳宁
@contact: [email protected]
"""
import re
import torch
import sentencepiece as spm
from model import BertModel
sp = spm.SentencePieceProcessor()
sp.load('resource/sentencepiece.unigram.35000.model')
vocab_size = sp.get_piece_size()
n_embedding = 512
n_layer = 8
model = BertModel(vocab_size, n_embedding, n_layer)
model.eval()
model.load_state_dict(torch.load('resource/model.{}.{}.th'.format(n_embedding, n_layer),
map_location='cpu'))
# you should enable cuda if it is available
# model.cuda()
# if you are using a GPU that has tensor cores (nvidia volta, Turing architecture), you can enable half precision
# inference and training, we recommend to use the nvidia official apex to make everything as clean as possible from
# apex import amp [model] = amp.initialize([model], opt_level="O2")
device = model.embedding.weight.data.device
def clean_text(txt):
txt = txt.lower()
txt = re.sub('\s*', '', txt)
return txt
def get_sequence_ids(text):
return sp.EncodeAsIds(clean_text(text))
def get_sequence_embedding(text):
ids = [get_sequence_ids(text)]
ids = torch.LongTensor(ids).to(device)
with torch.no_grad():
hidden = model.inference(ids)
return hidden
def get_pair_embedding(s1, s2):
"""
during training, we use sentence pairs to train bert,
so it could also be utilized for inference
:param s1: sentence 1, usually the question, premise
:param s2: sentence 1, such as the document, hypothesis
:return: embedding
"""
s1_ids = get_sequence_ids(s1)
s2_ids = get_sequence_ids(s2)
ids = [s1_ids + [vocab_size] + s2_ids]
ids = torch.LongTensor(ids).to(device)
with torch.no_grad():
hidden = model.inference(ids)
return hidden
if __name__ == '__main__':
sentence = '我们一起学猫叫,一起喵喵喵喵'
embedding = get_sequence_embedding(sentence)
print(embedding.size())