-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
360 lines (313 loc) · 12.4 KB
/
run.py
File metadata and controls
360 lines (313 loc) · 12.4 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#############################################################################
### Търсене и извличане на информация. Приложение на дълбоко машинно обучение
### Стоян Михов
### Зимен семестър 2025/2026
#############################################################################
###
### Машинен превод чрез генеративен езиков модел
###
#############################################################################
from utils.train import get_lr
from bpe.tokenizer import encode_word
from bpe.loader import get_bpe, build_bpe_lookup
from parameters.model import ModelParams
from utils.meta import progress_bar
from utils.file import save_to_file, load_from_file
from utils.corpus import prepare_data, read_corpus_from_disk, get_train_corpus
import sys
import numpy as np
import torch
from torch import nn
import torch.nn.functional as F
import math
import time
from nltk.translate.bleu_score import corpus_bleu
from model import DecoderTransformer
from parameters.nlp import (
UNK_TOKEN_IDX,
TRANS_TOKEN_IDX,
PAD_TOKEN_IDX,
START_TOKEN_IDX,
END_TOKEN_IDX,
SPECIAL_TOKENS,
)
from parameters.hyper import (
MAX_EPOCHS,
LEARNING_RATE,
DEVICE,
BATCH_SIZE,
CLIP_GRAD,
LOG_EVERY,
TEST_EVERY,
)
from parameters.corpus import (
WORDS_FILE_NAME,
CORPUS_FILE_NAME,
MODEL_FILE_NAME,
)
def perplexity(nmt, test, BATCH_SIZE):
test_size = len(test)
H = 0.0
c = 0
for b in range(0, test_size, BATCH_SIZE):
batch = test[b : min(b + BATCH_SIZE, test_size)]
padded = prepare_padded_batch(batch, DEVICE)
c += (padded[:, 1:] != PAD_TOKEN_IDX).sum().item()
with torch.no_grad():
# H += L * nmt(batch)
logits = nmt(padded[:, :-1])
H += F.cross_entropy(
logits.transpose(
-1, -2
), # \in \mathbb{R}^{B \times N-1 \times K} but cross entropy wants the classes second
padded[:, 1:],
ignore_index=PAD_TOKEN_IDX,
reduction="sum",
).item()
return math.exp(H / c)
def prepare_padded_batch(tokens: list[list[int]], device):
m = max(len(tl) for tl in tokens)
sents_padded = [s + (m - len(s)) * [PAD_TOKEN_IDX] for s in tokens]
return torch.tensor(sents_padded, dtype=torch.long, device=device)
class RuntimeUtils:
@staticmethod
def prepare():
(train_corpus, dev_corpus), vocab = prepare_data()
save_to_file((train_corpus, dev_corpus), CORPUS_FILE_NAME)
save_to_file(vocab, WORDS_FILE_NAME)
print("Data prepared.")
@staticmethod
def test_prepare():
(train_corpus, dev_corpus) = load_from_file(CORPUS_FILE_NAME)
vocab = load_from_file(WORDS_FILE_NAME)
print("Loaded prepared data:")
print(train_corpus[:5])
print(dev_corpus[:5])
print(list(vocab.items())[:100])
@staticmethod
def train(argv_1: str):
(train_corpus, dev_corpus) = load_from_file(CORPUS_FILE_NAME)
vocab = load_from_file(WORDS_FILE_NAME)
model_params = ModelParams(N=len(vocab))
learning_rate = LEARNING_RATE
nmt = DecoderTransformer(model_params).to(DEVICE)
optimizer = torch.optim.AdamW(nmt.parameters(), lr=learning_rate)
if argv_1 == "extratrain":
nmt.load(MODEL_FILE_NAME)
(iter, best_perplexity, learning_rate, osd) = torch.load(
f"{MODEL_FILE_NAME}.optim"
)
optimizer.load_state_dict(osd)
for param_group in optimizer.param_groups:
param_group["lr"] = learning_rate
else:
best_perplexity = math.inf
iter = 0
idx = np.arange(len(train_corpus), dtype="int32")
nmt.train()
beginTime = time.time()
for epoch in range(MAX_EPOCHS):
np.random.shuffle(idx)
words = 0
train_time = time.time()
for b in range(0, len(idx), BATCH_SIZE):
######################################################################################################
### Може да се наложи да се променя скоростта на спускане learning_rate в зависимост от итерацията
######################################################################################################
iter += 1
lr = get_lr(iter)
for param_group in optimizer.param_groups:
param_group["lr"] = lr
batch = [
train_corpus[i] for i in idx[b : min(b + BATCH_SIZE, len(idx))]
]
words += sum(len(s) - 1 for s in batch)
padded = prepare_padded_batch(batch, DEVICE)
logits = nmt(padded[:, :-1])
H = F.cross_entropy(
logits.transpose(
-1, -2
), # \in \mathbb{R}^{B \times N-1 \times K} but cross entropy wants the classes second
padded[:, 1:],
ignore_index=PAD_TOKEN_IDX,
)
optimizer.zero_grad()
H.backward()
nn.utils.clip_grad_norm_(nmt.parameters(), CLIP_GRAD)
optimizer.step()
if iter % LOG_EVERY == 0:
print(
"Iteration:",
iter,
"Epoch:",
epoch + 1,
"/",
MAX_EPOCHS,
", Batch:",
b // BATCH_SIZE + 1,
"/",
len(idx) // BATCH_SIZE + 1,
", loss: ",
H.item(),
"words/sec:",
words / (time.time() - train_time),
"time elapsed:",
(time.time() - beginTime),
)
train_time = time.time()
words = 0
if iter % TEST_EVERY == 0:
nmt.eval()
currentPerplexity = perplexity(nmt, dev_corpus, BATCH_SIZE)
nmt.train()
print("Current model perplexity: ", currentPerplexity)
if currentPerplexity < best_perplexity:
best_perplexity = currentPerplexity
print("Saving new best model.")
nmt.save(MODEL_FILE_NAME)
torch.save(
(
iter,
best_perplexity,
learning_rate,
optimizer.state_dict(),
),
f"{MODEL_FILE_NAME}.optim",
)
print("Reached maximum number of epochs!")
nmt.eval()
currentPerplexity = perplexity(nmt, dev_corpus, BATCH_SIZE)
print("Last model perplexity: ", currentPerplexity)
if currentPerplexity < best_perplexity:
best_perplexity = currentPerplexity
print("Saving last model.")
nmt.save(MODEL_FILE_NAME)
torch.save(
(iter, best_perplexity, learning_rate, optimizer.state_dict()),
f"{MODEL_FILE_NAME}.optim",
)
@staticmethod
def perplexity(argv_2: str, argv_3: str):
vocab = load_from_file(WORDS_FILE_NAME)
source_corpus, target_corpus = get_train_corpus()
(bpe_vocab, bpe_merges) = get_bpe(source_corpus, target_corpus)
bpe_lookup = build_bpe_lookup(bpe_vocab)
model_params = ModelParams(N=len(vocab))
nmt = DecoderTransformer(model_params).to(DEVICE)
nmt.load(MODEL_FILE_NAME)
source_test = read_corpus_from_disk(argv_2)
target_test = read_corpus_from_disk(argv_3)
test = []
test_corpus = zip(source_test, target_test)
for source_s, target_s in test_corpus:
sent = [START_TOKEN_IDX]
for w in source_s:
if w in bpe_lookup:
ew = bpe_lookup[w]
else:
ew = encode_word(w, bpe_merges)
ew = ew.split(" ")
for t in ew:
sent.append(vocab.get(t, UNK_TOKEN_IDX))
sent.append(TRANS_TOKEN_IDX)
for w in target_s:
if w in bpe_lookup:
ew = bpe_lookup[w]
else:
ew = encode_word(w, bpe_merges)
ew = ew.split(" ")
for t in ew:
sent.append(vocab.get(t, UNK_TOKEN_IDX))
sent.append(END_TOKEN_IDX)
test.append(sent)
nmt.eval()
print("Model perplexity: ", perplexity(nmt, test, BATCH_SIZE))
@staticmethod
@progress_bar
def translate(argv_2: str, argv_3: str, **kwargs):
vocab = load_from_file(WORDS_FILE_NAME)
source_corpus, target_corpus = get_train_corpus()
(bpe_vocab, bpe_merges) = get_bpe(source_corpus, target_corpus)
bpe_lookup = build_bpe_lookup(bpe_vocab)
words = list(vocab)
source_test = read_corpus_from_disk(argv_2)
test = []
for s in source_test:
sent = [START_TOKEN_IDX]
for w in s:
if w in bpe_lookup:
ew = bpe_lookup[w]
else:
ew = encode_word(w, bpe_merges)
ew = ew.split(" ")
for t in ew:
sent.append(
vocab.get(
t,
UNK_TOKEN_IDX,
)
)
sent.append(TRANS_TOKEN_IDX)
test.append(sent)
model_params = ModelParams(N=len(vocab))
nmt = DecoderTransformer(model_params).to(DEVICE)
nmt.load(MODEL_FILE_NAME)
nmt.eval()
with open(argv_3, "w") as file:
kwargs["pb_init"](len(test))
for s in test:
r = nmt.generate(s)
st = r.index(TRANS_TOKEN_IDX)
result = [words[i] for i in r[st + 1 : -1]]
file.write("".join(result).replace("</w>", " ") + "\n")
kwargs["pb_tick"]()
@staticmethod
def generate(argv_2: str):
vocab = load_from_file(WORDS_FILE_NAME)
source_corpus, target_corpus = get_train_corpus()
(bpe_vocab, bpe_merges) = get_bpe(source_corpus, target_corpus)
bpe_lookup = build_bpe_lookup(bpe_vocab)
words = list(vocab)
test = []
for w in argv_2.split():
if w in SPECIAL_TOKENS:
ew = w
elif w in bpe_lookup:
ew = bpe_lookup[w]
else:
ew = encode_word(w, bpe_merges)
ew = ew.split(" ")
for t in ew:
test.append(
vocab.get(
t,
UNK_TOKEN_IDX,
)
)
model_params = ModelParams(N=len(vocab))
nmt = DecoderTransformer(model_params).to(DEVICE)
nmt.load(MODEL_FILE_NAME)
nmt.eval()
r = nmt.generate(test)
result = [words[i] for i in r]
print("".join(result).replace("</w>", " ") + "\n")
@staticmethod
def bleu(argv_2: str, argv_3: str):
ref = [[s] for s in read_corpus_from_disk(argv_2)]
hyp = read_corpus_from_disk(argv_3)
bleu_score = corpus_bleu(ref, hyp)
print("Corpus BLEU: ", (bleu_score * 100))
if len(sys.argv) > 1 and sys.argv[1] == "prepare":
RuntimeUtils.prepare()
elif len(sys.argv) > 1 and sys.argv[1] == "test_prepare":
RuntimeUtils.test_prepare()
elif len(sys.argv) > 1 and sys.argv[1] in ("train", "extratrain"):
RuntimeUtils.train(sys.argv[1])
elif len(sys.argv) > 3 and sys.argv[1] == "perplexity":
RuntimeUtils.perplexity(sys.argv[2], sys.argv[3])
elif len(sys.argv) > 3 and sys.argv[1] == "translate":
RuntimeUtils.translate(sys.argv[2], sys.argv[3])
elif len(sys.argv) > 2 and sys.argv[1] == "generate":
RuntimeUtils.generate(sys.argv[2])
elif len(sys.argv) > 3 and sys.argv[1] == "bleu":
RuntimeUtils.bleu(sys.argv[2], sys.argv[3])