-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_doc2vec.py
79 lines (62 loc) · 2.48 KB
/
train_doc2vec.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
import argparse
import logging
import os
import re
import gensim
import jieba
import jieba.posseg as jp
import smart_open
INVALID_TOKEN_PATTERN = re.compile(r'^\W+$')
NUMBERS_PATTERN = re.compile(r'[0-9]+.?')
def tokenize(text):
tokens = []
for w, pos in jp.lcut(text.lower()):
if not pos:
continue
if INVALID_TOKEN_PATTERN.match(w):
continue
# if NUMBERS_PATTERN.match(w):
# continue
if not w.strip():
continue
tokens.append(w.strip())
return tokens
def read_corpus(fname):
with smart_open.open(fname, encoding="utf8") as f:
for i, line in enumerate(f):
tokens = tokenize(line.strip('\n').strip())
yield gensim.models.doc2vec.TaggedDocument(tokens, [i])
def train(input_file, model_path, vocab_path, epochs=10, **kwargs):
model = gensim.models.doc2vec.Doc2Vec(vector_size=100, min_count=7, max_vocab_size=100000, works=16)
train_corpus = read_corpus(input_file)
model.build_vocab(train_corpus)
vocab_dir = '/'.join(str(vocab_path).split('/')[:-1])
if not os.path.exists(vocab_dir):
os.makedirs(vocab_dir)
with open(vocab_path, mode='wt', encoding='utf8') as fout:
for k, v in model.wv.vocab.items():
fout.write(k + '\t' + str(v.count) + '\n')
model.train(train_corpus, total_examples=model.corpus_count, epochs=10)
model.save(model_path)
# for epoch in range(epochs):
# logging.info('Start training in epoch: %d' % epoch)
# model.train(train_corpus, total_examples=model.corpus_count, epochs=1)
# logging.info('Finished epoch %d' % epoch)
# output = model_path + '.ckpt.' + str(epoch)
# model.save(output)
# logging.info('Saved ckpt to %s' % output)
logging.info('Done!')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input_file')
parser.add_argument('--model_save_path')
parser.add_argument('--vocab_save_path')
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--jieba_user_dict', default=None)
args, _ = parser.parse_known_args()
logging.basicConfig(filename='log/train.doc2vec.log', level=logging.INFO)
if args.jieba_user_dict:
jieba.load_userdict(args.jieba_user_dict)
logging.info('Load userdict finished, path is %s' % args.jieba_user_dict)
jieba.initialize()
train(args.input_file, args.model_save_path, args.vocab_save_path, args.epochs)