-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn_v3.py
213 lines (173 loc) · 6.1 KB
/
nn_v3.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import random
class Linear:
def __init__(self, fan_in, fan_out, bias=True):
self.weight = torch.randn((fan_in, fan_out), generator=g) / fan_in ** 0.5
self.bias = torch.zeros(fan_out) if bias else None
def __call__(self, x):
self.out = x @ self.weight
if self.bias is not None:
self.out += self.bias
return self.out
def parameters(self):
return [self.weight] + ([] if self.bias is None else [self.bias])
class BatchNorm1d:
def __init__(self, dim, eps=1e-5, momentum=0.1):
self.eps = eps
self.momentum = momentum
self.training = True
# parameters (trained with backprop)
self.gamma = torch.ones(dim)
self.beta = torch.zeros(dim)
# buffers (trained with running 'momentum update')
self.running_mean = torch.zeros(dim)
self.running_var = torch.ones(dim)
def __call__(self, x):
if self.training:
xmean = x.mean(0, keepdims=True)
xvar = x.var(0, keepdims=True, unbiased=True)
else:
xmean = self.running_mean
xvar = self.running_var
xhat = (x - xmean) / torch.sqrt(xvar + self.eps) # normalize to unit variance
self.out = self.gamma * xhat + self.beta
if self.training:
with torch.no_grad():
self.running_mean = (1-self.momentum) * self.running_mean + self.momentum * xmean
self.running_var = (1-self.momentum) * self.running_var + self.momentum * xvar
return self.out
def parameters(self):
return [self.gamma, self.beta]
class Tanh:
def __call__(self, x):
self.out = torch.tanh(x)
return self.out
def parameters(self):
return []
def createWordsMapping(filename = 'data/names.txt'):
words = open(filename, 'r').read().splitlines()
chars = sorted(list(set(''.join(words))))
stoi = {s:i+1 for i,s in enumerate(chars)}
stoi['.'] = 0
itos = {i:s for s,i in stoi.items()}
n_vocab = len(stoi)
return words, stoi, itos, n_vocab
def buildDataset(words, block_size):
X, Y = [], []
for w in words:
context = [0] * block_size
for ch in w + '.':
ix = stoi[ch]
X.append(context)
Y.append(ix)
context = context[1:] + [ix]
X = torch.tensor(X)
Y = torch.tensor(Y)
return X,Y
def buildDatasets(words, block_size):
random.seed(42)
random.shuffle(words)
n1 = int(0.8 * len(words))
n2 = int(0.9 * len(words))
Xtr, Ytr = buildDataset(words[:n1], block_size)
Xdev, Ydev = buildDataset(words[n1:n2], block_size)
Xte, Yte = buildDataset(words[n2:], block_size)
return Xtr, Ytr, Xdev, Ydev, Xte, Yte
def initializeModelWeights(n_vocab, block_size, n_embed, n_hidden):
C = torch.randn((n_vocab, n_embed), generator=g)
layers = [
Linear(n_embed * block_size, n_hidden), BatchNorm1d(n_hidden), Tanh(),
Linear(n_hidden, n_hidden), BatchNorm1d(n_hidden), Tanh(),
Linear(n_hidden, n_hidden), BatchNorm1d(n_hidden), Tanh(),
Linear(n_hidden, n_hidden), BatchNorm1d(n_hidden), Tanh(),
Linear(n_hidden, n_hidden), BatchNorm1d(n_hidden), Tanh(),
Linear(n_hidden, n_vocab), BatchNorm1d(n_vocab)
]
with torch.no_grad():
layers[-1].gamma *= 0.1 # Make last layer less confident
for layer in layers[:-1]:
if isinstance(layer, Linear):
layer.weight *= 5/3
parameters = [C] + [p for layer in layers for p in layer.parameters()]
for p in parameters:
p.requires_grad = True
print(f'Total Parameters: {sum(p.nelement() for p in parameters)}')
return layers, parameters
def trainModel(X, Y, layers, parameters, n_epochs, batch_size):
lossi = []
for epoch in range(n_epochs):
# Minibatch construct
ix = torch.randint(0, X.shape[0], (batch_size,), generator=g)
X_batch, Y_batch = X[ix], Y[ix]
# Forward Pass
emb = parameters[0][X_batch] # embed characters into vectors
x = emb.view(emb.shape[0], -1) # concatentae the vectors
for layer in layers:
x = layer(x)
loss = F.cross_entropy(x, Y_batch)
# Backward Pass
for layer in layers:
layer.out.retain_grad()
for p in parameters:
p.grad = None
loss.backward()
# Update parameters
lr = 0.1 if epoch < 100000 else 0.01 # stop learning rate decay
for p in parameters:
p.data += -lr * p.grad
# Track Stats
lossi.append(loss.log10().item())
if epoch % 10000 == 0:
print(f'{epoch:7d}/{n_epochs:7d}: {loss.item():.4f}')
return lossi, parameters
@torch.no_grad()
def loss(X, Y, layers, parameters):
emb = parameters[0][X]
x = emb.view(emb.shape[0], -1)
for layer in layers:
if isinstance(layer, BatchNorm1d):
layer.training = False
x = layer(x)
loss = F.cross_entropy(x, Y)
return loss
def generateExample(layers, parameters, block_size, itos):
out = []
context = [0] * block_size
while True:
emb = parameters[0][torch.tensor([context])] # (1,block_size, d)
x = emb.view(emb.shape[0], -1)
for layer in layers:
if isinstance(layer, BatchNorm1d):
layer.training = False
x = layer(x)
probs = F.softmax(x, dim=1)
ix = torch.multinomial(probs, num_samples=1, generator=g).item()
context = context[1:] + [ix]
out.append(ix)
if ix == 0:
break
return ''.join(itos[i] for i in out)
def generateExamples(layers, parameters, block_size, itos, numExamples = 20):
examples = []
for _ in range(numExamples):
example = generateExample(layers, parameters, block_size, itos)
examples.append(example)
return examples
if __name__ == '__main__':
BLOCK_SIZE = 3
N_EMBED = 10
N_HIDDEN = 100
N_EPOCHS = 200000
BATCH_SIZE = 32
g = torch.Generator().manual_seed(2147483647)
words, stoi, itos, n_vocab = createWordsMapping()
Xtr, Ytr, Xdev, Ydev, Xte, Yte = buildDatasets(words, BLOCK_SIZE)
layers, parameters = initializeModelWeights(n_vocab, BLOCK_SIZE, N_EMBED, N_HIDDEN)
lossi, parameters = trainModel(Xtr, Ytr, layers, parameters, N_EPOCHS, BATCH_SIZE)
plt.plot(lossi)
print(f'Train Loss: {loss(Xtr, Ytr, layers, parameters)}')
print(f'Val Loss: {loss(Xdev, Ydev, layers, parameters)}')
examples = generateExamples(layers, parameters, BLOCK_SIZE, itos)
print(f'Generated Examples: {examples}')