-
Notifications
You must be signed in to change notification settings - Fork 6
/
nn.cu
233 lines (185 loc) · 5.87 KB
/
nn.cu
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <time.h>
#include "utils.c"
#include "parallel.cu"
#ifdef __APPLE__
#include <unistd.h>
#else _WIN32
#include <windows.h>
#endif
typedef struct {
int n_inputs;
int n_hidden;
int n_outputs;
float *out_input;
float *out_hidden;
float *out_output;
float *changes_input_hidden;
float *changes_hidden_output;
float *w_input_hidden;
float *w_hidden_output;
} NeuralNet;
typedef struct {
int *result;
int *data;
} Pattern;
void buildLayer(float *arr, int n, float initial) {
int i=0;
while(i < n){
*arr = initial;
arr++;
i++;
}
}
float* buildWeightsLayer(int outer_n, int inner_n, float seed) {
int total = outer_n * inner_n;
float *w = (float *)malloc(sizeof(float) * total);
for(int i=0; i < total; i++) {
if (seed == -1) {
w[i] = ((float)rand()/(float)RAND_MAX);
} else {
w[i] = seed;
}
}
return w;
}
NeuralNet buildNeuralNet(int n_inputs, int n_outputs, int n_hidden) {
float *out_input = (float *)malloc(sizeof(float) * (n_inputs + 1));
float *out_hidden = (float *)malloc(sizeof(float) * n_hidden);
float *out_output = (float *)malloc(sizeof(float) * n_outputs);
buildLayer(out_input, n_inputs + 1, 1.0f);
buildLayer(out_hidden, n_hidden, 1.0f);
buildLayer(out_output, n_outputs, 1.0f);
// Build changes layer
float *changes_input_hidden = buildWeightsLayer(n_inputs + 1, n_hidden, 0.0f);
float *changes_hidden_output = buildWeightsLayer(n_hidden, n_outputs, 0.0f);
// Build weight matrix
float *w_input_hidden = buildWeightsLayer(n_inputs + 1, n_hidden, -1.0f);
float *w_hidden_output = buildWeightsLayer(n_hidden, n_outputs, -1.0f);
NeuralNet nn;
nn.n_inputs = n_inputs + 1;
nn.n_outputs = n_outputs;
nn.n_hidden = n_hidden;
nn.out_input = out_input;
nn.out_hidden = out_hidden;
nn.out_output = out_output;
nn.changes_input_hidden = changes_input_hidden;
nn.changes_hidden_output = changes_hidden_output;
nn.w_input_hidden = w_input_hidden;
nn.w_hidden_output = w_hidden_output;
return nn;
}
float dsigmoid(float y) {
return 1.0 - pow(y,2.0f);
}
void update_pattern(Pattern pattern, NeuralNet nn) {
if (DEBUG) {
printf("\n ***** LAYER UPDATE *****\n");
}
// Write inputs
int i;
for(i=0; i < nn.n_inputs -1; i++) {
nn.out_input[i] = pattern.data[i];
}
// Run parallel update
update_layer(nn.out_input, nn.out_hidden, nn.n_inputs, nn.n_hidden, nn.w_input_hidden);
update_layer(nn.out_hidden, nn.out_output, nn.n_hidden, nn.n_outputs, nn.w_hidden_output);
if (DEBUG) {
printf("\n ***** END LAYER UPDATE *****\n");
}
}
float back_propagate_network(Pattern p, NeuralNet n) {
if (DEBUG) {
printf("\n ***** BACK PROPAGATE *****\n");
}
int i, j;
float *output_delta = (float*)malloc(sizeof(float) * n.n_outputs);
float *hidden_delta = (float*)malloc(sizeof(float) * n.n_hidden);
// Calculate output delta
for (i=0; i < n.n_outputs; i++) {
float error = p.result[i] - n.out_output[i];
output_delta[i] = dsigmoid(n.out_output[i]) * error;
}
// Calculate hidden delta
for(i=0; i < n.n_hidden; i++) {
float error = 0.0f;
for (j=0; j < n.n_outputs; j++) {
error += output_delta[j] * n.w_hidden_output[i * n.n_outputs + j];
}
hidden_delta[i] = dsigmoid(n.out_hidden[i]) * error;
}
// Set hidden-output weights
setWeightsForLayers(n.w_hidden_output, n.changes_hidden_output, output_delta, n.out_hidden, n.n_hidden, n.n_outputs);
if (DEBUG) {
printf("\nHidden-Output weights\n");
drawMatrix(n.w_hidden_output, n.n_outputs, n.n_hidden);
_sleep(1);
}
setWeightsForLayers(n.w_input_hidden, n.changes_input_hidden, hidden_delta, n.out_input, n.n_inputs, n.n_hidden);
if (DEBUG) {
printf("\nInput-Hidden weights\n");
drawMatrix(n.w_input_hidden, n.n_hidden, n.n_inputs);
_sleep(1);
}
// Calculate error
float error = 0.0f;
for (i=0; i < n.n_outputs; i++) {
error = error + 0.5f * pow(p.result[i] - n.out_output[i], 2);
}
if (DEBUG) {
printf("\n ***** Error for this pattern is: %f *****\n", error);
_sleep(2);
}
return error;
}
void train_network(Pattern *patterns, int n_patterns, int n_iterations, NeuralNet nn) {
int i, j;
for (i=0; i < n_iterations; i++) {
float error = 0;
for (j=0; j < n_patterns; j++) {
update_pattern(patterns[j], nn);
error += back_propagate_network(patterns[j], nn);
}
if (i % 10 == 0) {
printf("Error is: %-.5f\n", error);
if (DEBUG) _sleep(2);
}
}
}
Pattern makePatternSingleOutput(int *data, int result) {
Pattern p;
p.data = data;
p.result = (int *)malloc(sizeof(int));
p.result[0] = result;
return p;
}
int main() {
srand((unsigned)time(NULL));
int n_inputs = 2;
int n_hidden = 4;
int n_outputs = 1;
// Build output layer
NeuralNet nn = buildNeuralNet(n_inputs, n_outputs, n_hidden);
// Build training samples
int _p1[] = {0,0};
Pattern p1 = makePatternSingleOutput(_p1, 0);
int _p2[] = {0,1};
Pattern p2 = makePatternSingleOutput(_p2, 1);
int _p3[] = {1,1};
Pattern p3 = makePatternSingleOutput(_p3, 1);
int _p4[] = {1,0};
Pattern p4 = makePatternSingleOutput(_p4, 1);
Pattern patterns[] = {p3, p2, p1, p4};
// Train the network
train_network(patterns, 4, 1000, nn);
printf("\n\nTesting the network\n");
update_pattern(p2, nn);
for (int i=0; i < nn.n_outputs; i++) {
printf("Output: %f, expected: %i\n", nn.out_output[i], p2.result[i]);
}
cudaDeviceReset();
return 0;
}