-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigram_dev.py
171 lines (134 loc) · 4.8 KB
/
bigram_dev.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Andrej Karpathy's bigram model from his GPT tutorial."""
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
# ==============================================================================
# Raw data loading
# ==============================================================================
# Download data
if not os.path.exists("data/shakespeare.txt"):
print("Downloading Shakespeare...")
os.system(
"wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt -O data/shakespeare.txt"
)
# Load data
with open("data/shakespeare.txt", "r", encoding="utf-8") as f:
text = f.read()
print("loaded text")
print("corpus length:", len(text))
# Metadata
chars = sorted(list(set(text)))
vocab_size = len(chars)
print("chars: \\n ␣", " ".join(chars[2:]))
print("vocab size:", vocab_size)
# Encoding and decoding
stoi = {ch: i for i, ch in enumerate(chars)}
encode = lambda x: [stoi[ch] for ch in x]
itos = {i: ch for i, ch in enumerate(chars)}
decode = lambda x: "".join([itos[i] for i in x])
print("encoding and decoding sanity check:")
enc = encode(" hello ")
dec = decode(enc)
print(f"'{dec}' -> {enc}")
print()
# To tensor and proper loading
print("loading into a tensor")
data = torch.tensor(encode(text), dtype=torch.long)
print(data.shape, data.dtype)
print()
n = int(0.9 * len(data)) # 90% train, 10% val, no shuffle
train_data = data[:n]
val_data = data[n:]
block_size = 8
print("illustrating how training sequences (context is 1-block_size)")
x = train_data[:block_size]
y = train_data[1 : block_size + 1]
for t in range(block_size):
context = x[: t + 1]
target = y[t]
print(f"input: {context}, target: {target}")
print()
# ==============================================================================
# Data Loader
# ==============================================================================
torch.manual_seed(1337)
batch_size = 4
block_size = 8
def get_batch(data):
# get `batch_size` random indecies
ix = torch.randint(len(data) - block_size, (batch_size,))
x = torch.stack([data[i : i + block_size] for i in ix])
y = torch.stack([data[i + 1 : i + block_size + 1] for i in ix])
return x, y
print("testing get_batch")
xb, yb = get_batch(train_data)
print("inputs:")
print(xb.shape)
print(xb)
print("targets:")
print(yb.shape)
print(yb)
for b in range(batch_size): # batch
print(f"sample {b+1}")
for t in range(block_size): # time
context = xb[b, : t + 1]
target = yb[b, t]
print(f"input: {context}, target: {target}")
# ==============================================================================
# Bigram Model
# ==============================================================================
print()
print("Bigram model")
class Bigram(nn.Module):
def __init__(self, vocab_size):
super().__init__()
self.token_embedding = nn.Embedding(vocab_size, vocab_size)
def forward(self, idx: torch.Tensor, targets: torch.Tensor | None = None):
# (B,T,C) -> (batch_size=4, block_size=8, vocab_size=65)
logits: torch.Tensor = self.token_embedding(idx)
loss = None
if targets is not None:
B, T, C = logits.shape
logits = logits.view(B * T, C) # channels need to be second dimension
targets = targets.view(-1) # flatten to match logits
loss = F.cross_entropy(logits, targets) # nll loss
return logits, loss
def generate(self, idx, max_tokens):
# idx is a (B,T) tensor of indices in the current context
for _ in range(max_tokens):
logits, loss = self(idx)
# focus on only the last time step
logits = logits[:, -1, :] # -> (B,C)
probs = F.softmax(logits, dim=-1) # (B,C)
# sample from the probability distribution
idx_next = torch.multinomial(probs, num_samples=1) # (B,1)
idx = torch.cat([idx, idx_next], dim=1) # (B,T+1)
return idx
model = Bigram(vocab_size)
logits, loss = model(xb, yb)
print("logits shape:", logits.shape)
print("loss:", loss)
print(
decode(
model.generate(idx=torch.zeros(1, 1, dtype=torch.long), max_tokens=100)[0].tolist() # fmt: skip
)
)
print()
print("training")
batch_size = 32
optimizer = torch.optim.AdamW(model.parameters(), lr=0.001)
for i in range(10000):
optimizer.zero_grad()
x, y = get_batch(train_data)
_, loss = model(x, y)
loss.backward()
optimizer.step()
# if i % 100 == 0:
# print(f"step {i}, loss {loss.item():.2f}")
print(f"step {i}, loss {loss.item():.2f}") # type: ignore
print(
decode(
model.generate(idx=torch.zeros(1, 1, dtype=torch.long), max_tokens=100)[0].tolist() # fmt: skip
)
)