-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
59 lines (47 loc) · 1.3 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
import torch
from torch.utils.data import DataLoader
import dl
from dl import data, models
from dataclasses import dataclass
import os
@dataclass
class MyModelConfig:
data_size = 200
seq_len = 10 #actual context is seq_len
vocab = 30
batch_size = 8
epochs = 200
layers = 4
nheads = 3
hdim = 16
att_drop = .01
res_drop = .01
emb_drop = .01
conf = MyModelConfig()
def train():
# Training loop
# Load config
dataset = data.MYDS("verdict.txt", conf.seq_len, stride=5)
conf.vocab = dataset.tokenizer.n_vocab
print(f"Vocab size: {conf.vocab}")
train_loader = DataLoader(dataset, batch_size=conf.batch_size)
model = models.MyModel(conf)
model.train()
optimizer = torch.optim.AdamW(params=model.parameters(), lr=0.001, )
tsteps = -1
lsteps = 200
iters = 0
#for e in tqdm.tqdm(range(epochs)):
for i, e in enumerate(range(conf.epochs)):
for b in train_loader:
iters += 1
model.zero_grad()
loss, outs = model(b)
loss.backward()
optimizer.step()
if iters % lsteps == 0:
print(f"Epoch:{i+1}/step:{iters}: loss: {loss.item()}")
if tsteps > -1 and iters > tsteps:
break
if __name__ == "__main__":
train()