-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_bigram.py
78 lines (61 loc) · 2.31 KB
/
train_bigram.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
import torch
import math
device = torch.device("cpu")
from tqdm import tqdm
from bigram import BigramLanguageModel
from data_loader import load_data, get_batch
torch.manual_seed(1337)
## Hyper-Parameters
batch_size = 32 # how many independent sequences will we process in parallel?
block_size = 8 # what is the maximum context length for predictions?
max_iters = 3000
eval_interval = 300
learning_rate = 1e-2
device = 'cuda' if torch.cuda.is_available() else 'cpu'
eval_iters = 200
@torch.no_grad()
def estimate_loss():
out = {}
model.eval()
for split in ['train', 'val']:
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch(train_data, val_data, block_size, batch_size,split)
X, Y = X.to(device), Y.to(device)
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
data_path = 'input.txt'
train_data, val_data, encoder, decoder, vocab_size = load_data(data_path)
block_size = 8
model = BigramLanguageModel(vocab_size)
m = model.to(device)
print(sum(p.numel() for p in m.parameters())/1e6, 'M parameters')
n = BigramLanguageModel(vocab_size)
optim = torch.optim.AdamW(m.parameters(), lr=1e-3)
print("Training the model in progress:")
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)
for iter in range(max_iters):
# every once in a while evaluate the loss on train and val sets
if iter % eval_interval == 0 or iter == max_iters - 1:
losses = estimate_loss()
print(f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
# sample a batch of data
xb, yb = get_batch(train_data, val_data, block_size, batch_size,'train')
xb, yb = xb.to(device), yb.to(device)
# evaluate the loss
logits, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
idx = torch.zeros((1, 1), dtype=torch.long)
idx.to(device)
print("Generation Result without training:")
print(decoder(n.generate(idx, 100)[0].cpu().tolist()))
print('-------------------------------------------------------- \n')
idx = torch.zeros((1, 1), dtype=torch.long)
idx = idx.to(device)
print("Generation Result with training:")
print(decoder(model.generate(idx, max_new_tokens=300)[0].cpu().tolist()))