Lepish commited on
Commit
5adf94f
·
verified ·
1 Parent(s): 68603e7

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +53 -1
train.py CHANGED
@@ -1 +1,53 @@
1
- <TO_BE_INSERTED_TRAIN_PY_CODE>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, pickle, json, torch
2
+ import torch.nn as nn
3
+ from model import GPT, GPTConfig
4
+
5
+ with open("data/ai_gf/input.txt", "r", encoding="utf-8") as f:
6
+ text = f.read()
7
+
8
+ chars = sorted(list(set(text)))
9
+ vocab_size = len(chars)
10
+ stoi = {ch: i for i, ch in enumerate(chars)}
11
+ itos = {i: ch for ch, i in stoi.items()}
12
+
13
+ def encode(s): return [stoi[c] for c in s]
14
+ def decode(l): return ''.join([itos[i] for i in l])
15
+
16
+ with open("meta.pkl", "wb") as f:
17
+ pickle.dump({'stoi': stoi, 'itos': itos}, f)
18
+
19
+ config = {
20
+ "vocab_size": vocab_size,
21
+ "block_size": 64,
22
+ "n_layer": 4,
23
+ "n_head": 4,
24
+ "n_embd": 128,
25
+ "dropout": 0.0,
26
+ "bias": False
27
+ }
28
+ with open("config.json", "w") as f:
29
+ json.dump(config, f)
30
+
31
+ data = torch.tensor(encode(text), dtype=torch.long)
32
+ gpt_config = GPTConfig(**config)
33
+ model = GPT(gpt_config)
34
+
35
+ optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
36
+ batch_size = 4
37
+ steps = 3000
38
+
39
+ print("Training on CPU...")
40
+ for step in range(steps):
41
+ ix = torch.randint(len(data) - config["block_size"], (batch_size,))
42
+ x = torch.stack([data[i:i+config["block_size"]] for i in ix])
43
+ y = torch.stack([data[i+1:i+1+config["block_size"]] for i in ix])
44
+ logits, _ = model(x)
45
+ loss = nn.functional.cross_entropy(logits.view(-1, vocab_size), y.view(-1))
46
+ optimizer.zero_grad()
47
+ loss.backward()
48
+ optimizer.step()
49
+ if step % 10 == 0:
50
+ print(f"Step {step}/{steps}, Loss: {loss.item():.4f}")
51
+
52
+ torch.save(model.state_dict(), "checkpoint.pt")
53
+ print("Training complete.")