-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
38 lines (35 loc) · 1.39 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
#############################################################################
### Търсене и извличане на информация. Приложение на дълбоко машинно обучение
### Стоян Михов
### Зимен семестър 2020/2021
#############################################################################
###
### Домашно задание 3
###
#############################################################################
import numpy as np
import torch
import math
def trainModel(trainCorpus, lm, optimizer, epochs, batchSize):
idx = np.arange(len(trainCorpus), dtype='int32')
lm.train()
for epoch in range(epochs):
np.random.shuffle(idx)
for b in range(0, len(idx), batchSize):
batch = [ trainCorpus[i] for i in idx[b:min(b+batchSize, len(idx))] ]
H = lm(batch)
optimizer.zero_grad()
H.backward()
optimizer.step()
print("Epoch:",epoch,'/',epochs,", Batch:",b // batchSize, '/', len(idx) // batchSize, ", loss: ",H.item())
def perplexity(lm, testCorpus, batchSize):
lm.eval()
H = 0.
c = 0
for b in range(0,len(testCorpus),batchSize):
batch = testCorpus[b:min(b+batchSize, len(testCorpus))]
l = sum(len(s)-1 for s in batch)
c += l
with torch.no_grad():
H += l * lm(batch)
return math.exp(H/c)