-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
195 lines (137 loc) · 4.15 KB
/
train.py
File metadata and controls
195 lines (137 loc) · 4.15 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#
#
# Let's build GPT from scratch
#
# https://www.youtube.com/watch?v=kCc8FmEb1nY
#
# Data comes from:
#
# https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt
#
# You probably want a M-x run-python
#
# And you may well need a M-x pyvenv-activate before you do that
#
with open('input.txt', 'r', encoding='utf8') as f:
text = f.read()
# print(f"length of dataset in characters is {len(text)}")
# print(text[:1000])
#
# Get unique characters (we are building a character based NN)
#
chars = sorted(list(set(text)))
vocab_size = len(chars)
# print(''.join(chars))
# print(vocab_size)
#
# chars to ints and vice versa
#
stoi = {ch: i for i, ch in enumerate(chars)}
itos = {i: ch for i, ch in enumerate(chars)}
def encode(s):
return [stoi[c] for c in s]
def decode(x):
return ''.join([itos[i] for i in x])
# print(encode("hii there"))
# print(decode(encode("hii there")))
import torch
data = torch.tensor(encode(text), dtype=torch.long)
# print(data.shape, data.dtype)
# print(data[:100])
#
# Split tensor encoded dataset into training and validation portions
#
n = int(0.9 * len(data))
train_data = data[:n]
val_data = data[n:]
#
# Set the block size (context size) to 8 and see that plus 1
#
block_size = 8
train_data[:block_size + 1]
#
# See how input and output training data relates to each other
#
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"when input is {context} the target is {target}")
#
# Getting slightly more serious, let's batch this together
#
torch.manual_seed(1337)
# batch_size = 4 # how many independent sequences in parallel
batch_size = 32 # how many independent sequences in parallel
def get_batch(split):
data = train_data if split == "train" else val_data
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
xb, yb = get_batch('train')
print('inputs:')
print(xb.shape)
print(xb)
print('targets:')
print(yb.shape)
print(yb)
print("----")
#
# Simplest neural network you can feed this to is a Bigram
#
import torch.nn as nn
from torch.nn import functional as F
torch.manual_seed(1337)
# batch_size = 32
class BigramLanguageModel(nn.Module):
def __init__(self, vocab_size):
super().__init__()
self.token_embedding_table = nn.Embedding(vocab_size, vocab_size)
def forward(self, idx, targets=None):
logits = self.token_embedding_table(idx)
if targets == None:
loss = None
else:
# Futz with how torch represents things to massage into cross_entropy input
B, T, C = logits.shape
logits = logits.view(B*T, C)
targets = targets.view(B*T)
# How good was the logits to the expected output?
loss = F.cross_entropy(logits, targets)
return logits, loss
def generate(self, idx, max_new_tokens):
for _ in range(max_new_tokens):
logits, loss = self(idx)
logits = logits[:, -1, :]
probs = F.softmax(logits, dim=-1)
idx_next = torch.multinomial(probs, num_samples = 1)
idx = torch.cat((idx, idx_next), dim=1)
return idx
m = BigramLanguageModel(vocab_size)
logits, loss = m(xb, yb)
print(logits.shape)
print(loss)
#
# Ok, let's generate something (we haven't done any training ...)
#
idx = torch.zeros((1, 1), dtype=torch.long) # the most cmplex repr of zero :-)
print(decode(m.generate(idx, max_new_tokens=100)[0].tolist()))
#
# oh man did that suck ... we need some training ... start with an optimizer
#
optimizer = torch.optim.AdamW(m.parameters(), lr =1e-3)
# batch_size = 32
for steps in range(10000):
xb, xy = get_batch("train")
logits, loss = m(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
print(loss.item())
#
# Let's try that output again
#
idx = torch.zeros((1, 1), dtype=torch.long) # the most cmplex repr of zero :-)
print(decode(m.generate(idx, max_new_tokens=100)[0].tolist()))