-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.py
71 lines (51 loc) · 1.58 KB
/
nn.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
from imports import *
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
if __name__ == "__main__":
device = torch.device("cpu")
words = open("./data/names.txt", "r").read().splitlines()
unique_chars = sorted(list(set(''.join(words))))
stoi = {s:i+1 for i,s in enumerate(unique_chars)}
stoi['.'] = 0
itos = {i:s for s,i in stoi.items()}
# BUILD NN INPUT
xs, ys = [], []
for word in words:
word_lst = ['.'] + list(word) + ['.']
for ch1, ch2 in zip(word_lst, word_lst[1:]):
idx1 = stoi[ch1]
idx2 = stoi[ch2]
xs.append(idx1)
ys.append(idx2)
xs = torch.tensor(xs)
ys = torch.tensor(ys)
g = torch.Generator().manual_seed(214783647)
W = torch.randn((27, 27), generator=g, requires_grad=True).to(device)
num = xs.nelement()
ic(num)
for i in range(100):
xenc = F.one_hot(xs, num_classes=27).float().to(device)
logits = xenc @ W
counts = logits.exp()
probs = counts/counts.sum(1, keepdim=True)
loss = -probs[torch.arange(num), ys].log().mean() + 0.01 * (W**2).mean()
ic(loss.item())
W.grad = None
loss.backward()
W.data += -50. * W.grad
# SAMPLING WORDS FROM NN
for i in range(10):
ix = 0
out = []
while True:
xenc = F.one_hot(torch.tensor([ix]), num_classes=27).float()
logits = xenc @ W
counts = logits.exp()
probs = counts/counts.sum(1, keepdim=True)
ix = torch.multinomial(probs, num_samples=1, replacement=True, generator=g).item()
out.append(itos[ix])
if ix == 0:
break
print("".join(out))
e()