-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.cpp
340 lines (271 loc) · 8.82 KB
/
nn.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <iostream>
#include <cmath>
#include "nn.h"
using namespace std;
template <class T> T sigmoid(T x) {
T out = (T)1/((T)1+(T)exp(-x));
return out;
}
template <class t> t relu(t x){
t out = max((t)0, x);
return out;
}
template <class t> t tanh(t x){
t out = tanh(x);
return out;
}
template <class t> t dsigmoid(t x){
t out = sigmoid<t>(x);
return out*(1-out);
}
template <class t> t drelu(t x){
if (x < 0){
return (t)0;
}
else {
return (t)1;
}
}
template <class t> t dtanh(t x){
t c = tanh<t>(x);
t out = (t)1 - c*c;
return out;
}
template <class t> t random(t x) {
// get random value in the interval [-x, x]
t out = (double)x*(2*(double)rand()/(double)RAND_MAX - (double)1 );
return out;
}
// constructor
template <class t, int h_layers, int in_dim, int out_dim, int h_dim>
nn::Network<t, h_layers, in_dim, out_dim, h_dim>::Network(t m){
// init w_in
for (int i=0; i < in_dim; i++){
for (int j=0; j < h_dim; j++) {
w_in[j][i] = random<t>(m);
// this -> w_in[j][i];
cout << "w_init: " << w_in[j][i] << " " << this << endl;
}
}
// init w_out
for (int i=0; i < out_dim; i++){
for (int j=0; j < h_dim; j++) {
w_out[i][j] = random<t>(m);
}
}
// init h
for (int n=0; n < h_layers; n++) {
for (int i=0; i < h_dim; i++){
for (int j=0; j < h_dim; j++) {
w_hiddens[n][i][j] = random<t>(m);
}
}
}
// init biases /{bias_out}
for (int i=0; i < h_layers+1; i++){
for (int j=0; j < h_dim; j++) {
biases[i][j] = random<t>(m);
}
}
for (int i=0; i<h_dim; i++){
cout << "biases: " << biases[0][i] << endl;
}
// init bias_out
for (int i=0; i < out_dim; i++){
bias_out[i] = random<t>(m);
}
};
template <class t, int h_layers, int in_dim, int out_dim, int h_dim>
void nn::Network<t, h_layers, in_dim, out_dim, h_dim>::forward(t fin[in_dim]){
// doing this using relus, but can replace with any other differentiable activation function (c.f. above)
// store initial input, for use in the backward step
for (int i = 0; i < in_dim; i++)
{
initial[i] = fin[i];
}
// prop through input layer
for (int i=0; i<h_dim; i++){
t store = 0;
for (int j=0; j<in_dim; j++) {
store += w_in[i][j]*fin[j];
}
// cout << "store input layer 1: " << store << endl;
input[i] = relu<t>(store + biases[0][i]); // apply the activation function
intermediate_outs[0][i] = input[i];
//cout << "input layer 1: " << input[i] << endl;
}
// prop through hidden layers
for (int n=0; n < h_layers; n++){
t aux[h_dim];
for (int i=0; i<h_dim; i++){
t store = 0;
for (int j=0; j<h_dim; j++) {
store += w_hiddens[n][i][j]*input[j];
}
aux[i] = relu<t>(store + biases[n+1][i]);
//cout << "hidden layer " << n+2 << ": "<< aux[i] << " untransformed: " << store+biases[n+1][i] << endl;
}
// update the inputs
for (int i=0; i<h_dim; i++){
input[i] = aux[i];
intermediate_outs[1+n][i] = input[i];
//cout << "new input: " << input[i] << endl;
}
}
// prop through output layer
for (int i=0; i<out_dim; i++){
t store = 0;
for (int j=0; j<h_dim; j++){
store += w_out[i][j]*input[j];
}
output[i] = store + bias_out[i];
cout << "output layer: " << output[i] << endl;
}
}
// probably will need outputs from every single layer to feed into gradient updates
template <class t, int h_layers, int in_dim, int out_dim, int h_dim>
void nn::Network<t, h_layers, in_dim, out_dim, h_dim>::backward(t true_out[out_dim], t learning_rate){
// compute rms loss
t rms_loss = 0;
for (int i=0; i < out_dim; i++) {
rms_loss += (true_out[i]-output[i])*(true_out[i]-output[i]);
}
cout << "loss: " << rms_loss << endl;
t J_output[out_dim]; // gradient vector w.r.t. the loss
for (int i=0; i < out_dim; i++) {
J_output[i] = 2*(true_out[i]-output[i]);
//cout << "J_out: " << J_output[i] << endl;
}
// compute gradients for the final layer
t J_wout_hl[out_dim];
t J_bias_hl[out_dim];
for (int i = 0; i < out_dim; i++){
J_wout_hl[i]= output[i]*J_output[i];
J_bias_hl[i] = J_output[i];
//cout << "J_out_hl: " << J_wout_hl[i] << endl;
}
// compute gradients for the intermediate layers
t J_w_hiddens[1+h_layers][h_dim];
t J_bias_hiddens[1+h_layers][h_dim];
// grads for final hidden layer
for (int j = 0; j < h_dim; j++){
t js = 0;
for (int i = 0; i < out_dim; i++){
js += J_wout_hl[i]*w_out[i][j]*drelu<t>(intermediate_outs[h_layers][j]);
}
J_w_hiddens[h_layers][j] = js;
J_bias_hiddens[h_layers][j] = js;
}
// grads for all layers except final hidden layer
for (int n = h_layers-1; n >= 0; n--){
for (int j = 0; j < h_dim; j++){
t js = 0;
for (int i = 0; i < h_dim; i++){
js += J_w_hiddens[n+1][i]*w_hiddens[n+1][i][j]* drelu<t>(intermediate_outs[n][j]);
}
J_w_hiddens[n][j] = js;
J_bias_hiddens[n][j] = js;
}
}
// *intermediate_outs[h_layers-1][k] postpone to the update step
// update the weights and biases for the final output layer using the calculated gradients via simple gradient descent
for (int i = 0; i < out_dim; i++){
for (int j = 0; j < h_dim; j++){
w_out[i][j] -= learning_rate*J_wout_hl[i]*intermediate_outs[h_layers][j];
//cout << "w_out_hl: " << w_out[i][j] << endl;
}
bias_out[i] -= learning_rate*J_bias_hl[i];
}
// update the weights and biases for the hidden (intermediate) layers
for (int i = 0; i < h_dim; i++){
for (int j = 0; j < h_dim; j++){
w_hiddens[h_layers-1][i][j] -= learning_rate*J_w_hiddens[h_layers][i]*intermediate_outs[h_layers-1][j];
}
biases[h_layers][i] -= learning_rate*J_bias_hiddens[h_layers][i];
}
// update weights and biases for all except input layer
for (int n = h_layers-1; n > 0; n--){
for (int i = 0; i < h_dim; i++){
for (int j = 0; j < h_dim; j++){
w_hiddens[n-1][i][j] -= learning_rate*J_w_hiddens[n][i]*intermediate_outs[n-1][j];
}
biases[n][i] -= learning_rate*J_bias_hiddens[n][i];
}
}
// update the weights and biases for the input layer
for (int i = 0; i < h_dim; i++){
for (int j = 0; j < in_dim; j++){
w_in[i][j] -= learning_rate*J_w_hiddens[0][i]*initial[j];
}
biases[0][i] -= learning_rate*J_bias_hiddens[0][i];
}
// DONE!
}
// deconstructor: defaulted in the header file
// template <class t, int h_layers, int in_dim, int out_dim, int h_dim>
// nn::Network<t, h_layers, in_dim, out_dim, h_dim>::~Network(){
// };
namespace experimental {
// neat idea for quantizing nn arithmetic but not really useful atm...
template <class T> T ftorial(T x) {
T out = 1;
while (x>0) {
out *= x;
x--;
}
return out;
}
template <class t> t pow(t x, int n){
t out = 1;
while (n>0) {
out *= x;
n--;
}
return out;
}
template <class t> t exp(t x) {
// arbitray precision exp
t out = 1;
for (int i=1; i<16; i++) {
out += pow<t>(x, i) / ftorial(i);
}
return out;
}
}
int main() {
// debug a few things
long x = -6, k, m;
k = sigmoid<long>(x);
cout << k << endl;
cout << experimental::ftorial(3) << endl;
long v = 1;
cout << experimental::exp<long>(v) << endl;
cout << (long)exp(v) << endl;
cout << rand()/(double)RAND_MAX << endl;
cout << rand()/(double)RAND_MAX << endl;
cout << "random function test (-2,2): " << random<float>((float)2) << endl;
srand(2225);
nn::Network<float, 1,2,1,6> *n = new nn::Network<float, 1,2,1,6>(0.5);
float inp[4][2] = {{1,1},{1,0},{0,1},{0,0}};
float out[4][1] = {{0},{1},{1},{0}};
for (int iterations = 0; iterations < 30000; iterations++){
for (int j = 0; j < 4; j++)
{
cout << j << endl;
n->forward(inp[j]);
n->backward(out[j], -0.001);
}
}
// // learn an XOR gate
// float inp[1][8] = {{1,1,1,0,0,1,0,0}};
// float out[1][4] = {{0,1,1,0}};
// for (int iterations = 0; iterations < 16; iterations++){
// n->forward(inp[0]);
// n->backward(out[0], -0.2);
// }
n->forward(inp[0]);
n->forward(inp[1]);
n->forward(inp[2]);
n->forward(inp[3]);
return 0;
}