-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
59 lines (53 loc) · 1.68 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 model import Gpt
from data_utils import (
get_batch,
device,
vocab_size,
)
@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(split)
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
if __name__ == "__main__":
# hyperparameters
batch_size = 16 # how many independent sequences will we process in parallel?
block_size = 32 # what is the maximum context length for predictions?
max_iters = 5000
eval_interval = 100
learning_rate = 1e-3
eval_iters = 200
n_embd = 64
n_head = 4
n_layer = 4
dropout = 0.0
# ------------
torch.manual_seed(1337)
model = Gpt(vocab_size, n_embd, n_layer, n_head, block_size, dropout).to(device)
# train model
print(sum(p.numel() for p in model.parameters()) / 1e6, "M parameters")
# create a PyTorch optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)
for i in range(max_iters):
# every once in a while evaluate the loss on train and val sets
if i % eval_interval == 0 or iter == max_iters - 1:
losses = estimate_loss()
print(
f"step {i}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}"
)
# sample a batch of data
xb, yb = get_batch("train")
# evaluate the loss
logits, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()