-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
151 lines (135 loc) · 8.58 KB
/
train.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
import os
import json
import numpy as np
from gensim.models.word2vec import LineSentence
from gensim.models import FastText, Word2Vec
from gensim.models.callbacks import CallbackAny2Vec
import logging
logging.basicConfig(level=logging.INFO, force = True)
logger = logging.getLogger()
logger.info("Logging initialized")
class Callback(CallbackAny2Vec):
def __init__(self, loss_list):
self.epoch = 0
self.loss_list = loss_list
def on_epoch_end(self, model):
loss = model.get_latest_training_loss()
self.loss_list.append(loss)
logger.info('Loss after epoch {}:{}'.format(self.epoch, loss))
model.running_training_loss = 0.0
self.epoch = self.epoch + 1
LOSSES_FILENAME = 'losses.json'
def save_losses(name, losses):
losses_mapping = {}
if os.path.exists(LOSSES_FILENAME):
with open(LOSSES_FILENAME) as f:
losses_mapping = json.load(f)
losses_mapping[name] = losses
with open(LOSSES_FILENAME, 'w') as f:
json.dump(losses_mapping, f, ensure_ascii=False, indent=0, sort_keys=True)
def get_losses(name):
result = []
if os.path.exists(LOSSES_FILENAME):
with open(LOSSES_FILENAME) as f:
losses_mapping = json.load(f)
if name in losses_mapping:
result = losses_mapping[name]
return result
def train(arch, sg, vector_size, window, min_count, ns_exponent, sample, epochs, processed_filename,
negative=5, start_alpha=0.0001, end_alpha=0.00001, model_descr=''):
"""
:param arch: 'w2v' (for word2vec) or 'ft' (for fasttext)
:param sg: 0 or 1, same as in gensim
:param vector_size: dimension of embeddings, same as in gensim
:param window: size of context during training, same as in gensim
:param min_count: minimum frequency for a word to be included, same as in gensim
:param ns_exponent: exponent for negative sampling, same as in gensim
:param sample: threshold for downsampling words, same as in gensim,
:param epochs: amount of epochs to train, same as in gensim
:param processed_filename: filename of processed corpus
:param model_descr: additional description for model to be included along standard specs,
shouldn't contain hyphen ('-')
:return: list of losses for each epoch
"""
ns_exp_fmt = np.format_float_positional(ns_exponent, trim="-")
sample_fmt = np.format_float_positional(sample, trim="-")
name = f'{arch}-sg{sg}-d{vector_size}-w{window}-min{min_count}-nse{ns_exp_fmt}-smp{sample_fmt}-ep{epochs}'
if model_descr:
name = name + f'-descr_{model_descr}'
logger.info(f'Model name: {name}')
if os.path.exists(name + '.model') and os.path.getsize(name + '.model') > 0:
losses = get_losses(name)
if losses:
logger.info('Model already exists, skipping training, returning previously calculated losses')
return losses
logger.info('Training model from scratch...')
losses = []
if arch == 'w2v':
model = Word2Vec(sg=sg, vector_size=vector_size, window=window, min_count=min_count, workers=5,
negative=negative, ns_exponent=ns_exponent, sample=sample)
elif arch == 'ft':
model = FastText(sg=sg, vector_size=vector_size, window=window, min_count=min_count, workers=5,
negative=negative, ns_exponent=ns_exponent, sample=sample)
else:
raise Exception(f'Unknown architecture: {arch}')
sentences = LineSentence(processed_filename)
model.build_vocab(sentences, progress_per=5000000)
# we override alpha with small values, since default values result in poor train performance
model.train(sentences, epochs=epochs, start_alpha=start_alpha, end_alpha=end_alpha, total_examples=model.corpus_count,
total_words=model.corpus_total_words, compute_loss=True, report_delay=300,
callbacks=[Callback(losses)])
model.save(f'{name}.model')
model.wv.save_word2vec_format(f'{name}.vectors')
save_losses(name, losses)
return losses
if __name__ == "__main__":
train(arch='ft', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=100,
processed_filename='processed-corpus-only-nouns.txt', model_descr='only_nouns')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.1, sample=0.001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.1, sample=0.00001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.1, sample=0.00001, epochs=500,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=200,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=500,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.0001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.00001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=30, ns_exponent=0, sample=10000, epochs=300,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
processed_filename='processed-corpus-only-nouns.txt', model_descr='only_nouns')
train(arch='w2v', sg=0, vector_size=200, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=1, vector_size=100, window=3, min_count=10, ns_exponent=0.75, sample=0.001, epochs=100,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=4, min_count=100, ns_exponent=0, sample=10000, epochs=200,
processed_filename='processed-corpus.txt')
train(arch='w2v', sg=0, vector_size=100, window=4, min_count=100, ns_exponent=0, sample=10000, epochs=200,
processed_filename='processed-corpus-no-sent-split.txt', model_descr='no_sent_split')
train(arch='w2v', sg=0, vector_size=100, window=5, min_count=100, ns_exponent=0, sample=10000, epochs=200,
processed_filename='processed-corpus-no-sent-split.txt', model_descr='no_sent_split')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
negative=10, processed_filename='processed-corpus.txt', model_descr='negative_10')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
negative=15, processed_filename='processed-corpus.txt', model_descr='negative_15')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
negative=20, processed_filename='processed-corpus.txt', model_descr='negative_20')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
start_alpha=0.00001, end_alpha=0.000001, processed_filename='processed-corpus.txt', model_descr='smaller_alpha')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
start_alpha=0.001, end_alpha=0.000001, processed_filename='processed-corpus.txt', model_descr='steeper_alpha')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=200,
start_alpha=0.01, end_alpha=0.0000001, processed_filename='processed-corpus.txt', model_descr='xsteeper_alpha')
train(arch='w2v', sg=0, vector_size=100, window=3, min_count=100, ns_exponent=0, sample=10000, epochs=500,
start_alpha=0.01, end_alpha=0.0000001, processed_filename='processed-corpus.txt', model_descr='xsteeper_alpha')