Repository navigation
Expand file tree
/
Copy pathmakemore.hpp
More file actions
150 lines (115 loc) · 3.15 KB
/
Copy pathmakemore.hpp
File metadata and controls
150 lines (115 loc) · 3.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
#pragma once
#include "matrix.hpp"
#include "value.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include <random>
#include <string>
#include <vector>
struct Dataset {
std::vector<std::vector<i32>> X;
std::vector<i32> Y;
};
inline std::vector<std::string> load_names(const std::string &path) {
std::ifstream file(path);
if (!file) {
std::cerr << "file not found in makemore.hpp\n";
return {};
}
std::vector<std::string> names;
std::string line;
while (std::getline(file, line)) {
if (!line.empty() && line.back() == '\r')
line.pop_back();
if (!line.empty())
names.push_back(line);
}
return names;
}
inline i32 stoi_(char c) {
if (c == '.') {
return 0;
} else {
return (c - 'a' + 1);
}
}
inline char itos_(i32 ix) {
if (ix == 0) {
return '.';
} else {
return static_cast<char>(ix + 'a' - 1);
}
}
inline Dataset build_dataset(const std::vector<std::string> &names,
i32 block_size) {
Dataset ds;
for (const auto &name : names) {
std::vector<i32> context(block_size, 0);
std::string s = name + '.';
for (char ch : s) {
i32 target = stoi_(ch);
ds.X.push_back(context);
ds.Y.push_back(target);
context.erase(context.begin());
context.push_back(target);
}
}
return ds;
}
inline Matrix one_hot(i32 idx, i32 vocab) {
Matrix v(vocab, 1, false);
v.at(idx, 0) = 1.0;
return v;
}
inline std::shared_ptr<Value> embed_context(std::shared_ptr<Value> C,
const std::vector<i32> &context,
i32 vocab) {
std::vector<std::shared_ptr<Value>> lookups;
for (i32 idx : context) {
auto oh = std::make_shared<Value>(one_hot(idx, vocab));
lookups.push_back(matmul(C, oh));
}
return concat(lookups);
}
inline i32 sample_row(const std::vector<f32> &row, std::mt19937 &gen) {
std::uniform_real_distribution<f32> dist(0.0, 1.0);
f32 r = dist(gen);
f32 cumulative = 0.0;
for (size_t i = 0; i < row.size(); ++i) {
cumulative += row[i];
if (cumulative > r)
return static_cast<i32>(i);
}
return static_cast<i32>(row.size()) - 1;
}
inline std::string generate(std::shared_ptr<Value> C, std::shared_ptr<Value> W1,
std::shared_ptr<Value> b1,
std::shared_ptr<Value> W2,
std::shared_ptr<Value> b2, i32 vocab, i32 block,
std::mt19937 &gen) {
std::string name;
std::vector<i32> context(block, 0);
while (true) {
auto x = embed_context(C, context, vocab);
auto h = tanh_(add(matmul(W1, x), b1));
auto logits = add(matmul(W2, h), b2);
std::vector<f32> probs(vocab);
f64 maxl = logits->data.at(0, 0);
f64 sum = 0.0;
for (i32 i = 0; i < vocab; ++i) {
probs[i] = std::exp(logits->data.at(i, 0) - maxl);
sum += probs[i];
}
for (i32 i = 0; i < vocab; ++i) {
probs[i] /= sum;
}
i32 next = sample_row(probs, gen);
if (next == 0)
break;
name += itos_(next);
context.erase(context.begin());
context.push_back(next);
}
return name;
}