-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.cpp
More file actions
270 lines (223 loc) · 7.59 KB
/
Copy pathnn.cpp
File metadata and controls
270 lines (223 loc) · 7.59 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "nn.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <numeric>
#include <stdexcept>
#include <cassert>
float NeuralNet::xavier(int fan_in, int fan_out) {
float limit = std::sqrt(6.0f / (fan_in + fan_out));
std::uniform_real_distribution<float> dist(-limit, limit);
return dist(rng_);
}
void NeuralNet::relu(std::vector<float>& v) {
for (auto& x : v) x = x > 0.f ? x : 0.f;
}
void NeuralNet::softmax(std::vector<float>& v) {
float mx = *std::max_element(v.begin(), v.end());
float sum = 0.f;
for (auto& x : v) { x = std::exp(x - mx); sum += x; }
for (auto& x : v) x /= sum;
}
void NeuralNet::init(const std::vector<std::string>& class_labels, unsigned seed) {
rng_.seed(seed);
labels = class_labels;
num_classes = static_cast<int>(labels.size());
embed.resize(VOCAB_SIZE * EMBED_DIM);
for (auto& e : embed) e = xavier(VOCAB_SIZE, EMBED_DIM);
w1.resize(HIDDEN1 * EMBED_DIM);
b1.assign(HIDDEN1, 0.f);
for (auto& w : w1) w = xavier(EMBED_DIM, HIDDEN1);
w2.resize(HIDDEN2 * HIDDEN1);
b2.assign(HIDDEN2, 0.f);
for (auto& w : w2) w = xavier(HIDDEN1, HIDDEN2);
wo.resize(num_classes * HIDDEN2);
bo.assign(num_classes, 0.f);
for (auto& w : wo) w = xavier(HIDDEN2, num_classes);
a0.resize(EMBED_DIM);
a1.resize(HIDDEN1);
a2.resize(HIDDEN2);
ao.resize(num_classes);
}
std::vector<float> NeuralNet::forward(const std::vector<int>& tokens) {
std::fill(a0.begin(), a0.end(), 0.f);
int n = 0;
for (int tid : tokens) {
if (tid < 0 || tid >= VOCAB_SIZE) continue;
for (int d = 0; d < EMBED_DIM; ++d)
a0[d] += embed[tid * EMBED_DIM + d];
++n;
}
if (n > 0) for (auto& x : a0) x /= n;
std::vector<float> pre1(HIDDEN1, 0.f);
for (int o = 0; o < HIDDEN1; ++o) {
pre1[o] = b1[o];
for (int i = 0; i < EMBED_DIM; ++i)
pre1[o] += w1[o * EMBED_DIM + i] * a0[i];
}
a1 = pre1;
relu(a1);
std::vector<float> pre2(HIDDEN2, 0.f);
for (int o = 0; o < HIDDEN2; ++o) {
pre2[o] = b2[o];
for (int i = 0; i < HIDDEN1; ++i)
pre2[o] += w2[o * HIDDEN1 + i] * a1[i];
}
a2 = pre2;
relu(a2);
std::vector<float> preo(num_classes, 0.f);
for (int o = 0; o < num_classes; ++o) {
preo[o] = bo[o];
for (int i = 0; i < HIDDEN2; ++i)
preo[o] += wo[o * HIDDEN2 + i] * a2[i];
}
ao = preo;
softmax(ao);
return ao;
}
int NeuralNet::predict(const std::vector<int>& tokens) {
auto probs = forward(tokens);
return static_cast<int>(
std::max_element(probs.begin(), probs.end()) - probs.begin());
}
std::string NeuralNet::predict_label(const std::vector<int>& tokens) {
int idx = predict(tokens);
return labels[idx];
}
float NeuralNet::train_step(const std::vector<int>& tokens, int label, float lr) {
std::fill(a0.begin(), a0.end(), 0.f);
int n = 0;
for (int tid : tokens) {
if (tid < 0 || tid >= VOCAB_SIZE) continue;
for (int d = 0; d < EMBED_DIM; ++d)
a0[d] += embed[tid * EMBED_DIM + d];
++n;
}
if (n > 0) for (auto& x : a0) x /= n;
std::vector<float> pre1(HIDDEN1, 0.f);
for (int o = 0; o < HIDDEN1; ++o) {
pre1[o] = b1[o];
for (int i = 0; i < EMBED_DIM; ++i)
pre1[o] += w1[o * EMBED_DIM + i] * a0[i];
}
a1 = pre1; relu(a1);
std::vector<float> pre2(HIDDEN2, 0.f);
for (int o = 0; o < HIDDEN2; ++o) {
pre2[o] = b2[o];
for (int i = 0; i < HIDDEN1; ++i)
pre2[o] += w2[o * HIDDEN1 + i] * a1[i];
}
a2 = pre2; relu(a2);
std::vector<float> preo(num_classes, 0.f);
for (int o = 0; o < num_classes; ++o) {
preo[o] = bo[o];
for (int i = 0; i < HIDDEN2; ++i)
preo[o] += wo[o * HIDDEN2 + i] * a2[i];
}
ao = preo; softmax(ao);
// Cross-entropy loss
float loss = -std::log(std::max(ao[label], 1e-9f));
std::vector<float> d_preo = ao;
d_preo[label] -= 1.f;
// Output
std::vector<float> d_a2(HIDDEN2, 0.f);
for (int o = 0; o < num_classes; ++o) {
bo[o] -= lr * d_preo[o];
for (int i = 0; i < HIDDEN2; ++i) {
d_a2[i] += wo[o * HIDDEN2 + i] * d_preo[o];
wo[o * HIDDEN2 + i] -= lr * d_preo[o] * a2[i];
}
}
// Layer 2 Backprop ReLU
std::vector<float> d_pre2(HIDDEN2, 0.f);
for (int i = 0; i < HIDDEN2; ++i)
d_pre2[i] = (pre2[i] > 0.f) ? d_a2[i] : 0.f;
std::vector<float> d_a1(HIDDEN1, 0.f);
for (int o = 0; o < HIDDEN2; ++o) {
b2[o] -= lr * d_pre2[o];
for (int i = 0; i < HIDDEN1; ++i) {
d_a1[i] += w2[o * HIDDEN1 + i] * d_pre2[o];
w2[o * HIDDEN1 + i] -= lr * d_pre2[o] * a1[i];
}
}
// Layer 1 backprop ReLU
std::vector<float> d_pre1(HIDDEN1, 0.f);
for (int i = 0; i < HIDDEN1; ++i)
d_pre1[i] = (pre1[i] > 0.f) ? d_a1[i] : 0.f;
std::vector<float> d_a0(EMBED_DIM, 0.f);
for (int o = 0; o < HIDDEN1; ++o) {
b1[o] -= lr * d_pre1[o];
for (int i = 0; i < EMBED_DIM; ++i) {
d_a0[i] += w1[o * EMBED_DIM + i] * d_pre1[o];
w1[o * EMBED_DIM + i] -= lr * d_pre1[o] * a0[i];
}
}
if (n > 0) {
float scale = 1.f / n;
for (int tid : tokens) {
if (tid < 0 || tid >= VOCAB_SIZE) continue;
for (int d = 0; d < EMBED_DIM; ++d)
embed[tid * EMBED_DIM + d] -= lr * d_a0[d] * scale;
}
}
return loss;
}
bool NeuralNet::save(const std::string& path) const {
std::ofstream f(path, std::ios::binary);
if (!f) return false;
uint32_t magic = 0x4E564953; // 'NVIS'
f.write(reinterpret_cast<const char*>(&magic), 4);
uint32_t nc = static_cast<uint32_t>(num_classes);
f.write(reinterpret_cast<const char*>(&nc), 4);
for (const auto& lbl : labels) {
uint32_t len = static_cast<uint32_t>(lbl.size());
f.write(reinterpret_cast<const char*>(&len), 4);
f.write(lbl.data(), len);
}
auto write_vec = [&](const std::vector<float>& v) {
f.write(reinterpret_cast<const char*>(v.data()),
static_cast<std::streamsize>(v.size() * sizeof(float)));
};
write_vec(embed);
write_vec(w1); write_vec(b1);
write_vec(w2); write_vec(b2);
write_vec(wo); write_vec(bo);
return f.good();
}
bool NeuralNet::load(const std::string& path) {
std::ifstream f(path, std::ios::binary);
if (!f) return false;
uint32_t magic = 0;
f.read(reinterpret_cast<char*>(&magic), 4);
if (magic != 0x4E564953) return false;
uint32_t nc = 0;
f.read(reinterpret_cast<char*>(&nc), 4);
num_classes = static_cast<int>(nc);
labels.resize(num_classes);
for (auto& lbl : labels) {
uint32_t len = 0;
f.read(reinterpret_cast<char*>(&len), 4);
lbl.resize(len);
f.read(lbl.data(), len);
}
auto read_vec = [&](std::vector<float>& v, size_t n) {
v.resize(n);
f.read(reinterpret_cast<char*>(v.data()),
static_cast<std::streamsize>(n * sizeof(float)));
};
read_vec(embed, VOCAB_SIZE * EMBED_DIM);
read_vec(w1, HIDDEN1 * EMBED_DIM); read_vec(b1, HIDDEN1);
read_vec(w2, HIDDEN2 * HIDDEN1); read_vec(b2, HIDDEN2);
read_vec(wo, num_classes * HIDDEN2); read_vec(bo, num_classes);
a0.resize(EMBED_DIM);
a1.resize(HIDDEN1);
a2.resize(HIDDEN2);
ao.resize(num_classes);
return f.good();
}
int NeuralNet::param_count() const {
return static_cast<int>(
embed.size() + w1.size() + b1.size() +
w2.size() + b2.size() +
wo.size() + bo.size());
}