-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4.c
More file actions
332 lines (313 loc) · 7.56 KB
/
example4.c
File metadata and controls
332 lines (313 loc) · 7.56 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
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
#include <example4.c>
#include <genann.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct genann;
typedef double (*genann_actfun)(const struct genann *ann, double a);
typedef struct genann
{
int inputs;
int hidden_layers;
int hidden;
int outputs;
genann_actfun activation_hidden;
genann_actfun activation_output;
int total_weights;
int total_neurons;
double *weight;
double *output;
double *delta;
} genann;
genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);
genann *genann_read(FILE *in);
void genann_randomize(genann *ann);
genann *genann_copy(const genann *ann);
void genann_free(genann *ann);
const double *genann_run(const genann *ann, const double *inputs);
void genann_train(const genann *ann, const double *inputs, const double *desired_outputs, double learning_rate);
void genann_write(const genann *ann, FILE *out);
void genann_init_sigmoid_lookup(const genann *ann);
double genann_act_sigmoid(const genann *ann, double a);
double genann_act_sigmoid_cached(const genann *ann, double a);
double genann_act_threshold(const genann *ann, double a);
double genann_act_linear(const genann *ann, double a);
const char *iris_data = "example/iris.data";
double *input;
double *class;
int samples;
const char *class_names[] = {"Iris-setosa", "Iris-versicolor", "Iris-virginica"};
void load_data()
{
FILE *in = fopen("example/iris.data", "r");
if (!in)
{
printf("Could not open file: %s\n", iris_data);
exit(1);
}
char line[1024];
while ((!feof(in)) && fgets(line, 1024, in))
{
samples += 1;
}
fseek(in, 0, 0);
printf("Loading %d data points from %s\n", samples, iris_data);
input = malloc(((sizeof(double)) * samples) * 4);
class = malloc(((sizeof(double)) * samples) * 3);
int i;
int j;
for (i = 0; i < samples; i += 1)
{
helper_load_data_1(&j, in, line, i);
}
fclose(in);
}
int main(int argc, char *argv[])
{
printf("GENANN example 4.\n");
printf("Train an ANN on the IRIS dataset using backpropagation.\n");
srand(time(0));
load_data();
genann *ann = genann_init(4, 1, 4, 3);
int i;
int j;
int loops = 5000;
printf("Training for %d loops over data.\n", loops);
for (i = 0; i < loops; i += 1)
{
for (j = 0; j < samples; j += 1)
{
genann_train(ann, input + (j * 4), class + (j * 3), .01);
}
}
int correct = 0;
for (j = 0; j < samples; j += 1)
{
const double *guess = genann_run(ann, input + (j * 4));
if (class[(j * 3) + 0] == 1.0)
{
if ((guess[0] > guess[1]) && (guess[0] > guess[2]))
{
correct += 1;
}
}
else
if (class[(j * 3) + 1] == 1.0)
{
if ((guess[1] > guess[0]) && (guess[1] > guess[2]))
{
correct += 1;
}
}
else
if (class[(j * 3) + 2] == 1.0)
{
if ((guess[2] > guess[0]) && (guess[2] > guess[1]))
{
correct += 1;
}
}
else
{
printf("Logic error.\n");
exit(1);
}
}
printf("%d/%d correct (%0.1f%%).\n", correct, samples, (((double) correct) / samples) * 100.0);
genann_free(ann);
free(input);
free(class);
return 0;
}
struct genann;
typedef double (*genann_actfun)(const struct genann *ann, double a);
typedef struct genann
{
int inputs;
int hidden_layers;
int hidden;
int outputs;
genann_actfun activation_hidden;
genann_actfun activation_output;
int total_weights;
int total_neurons;
double *weight;
double *output;
double *delta;
} genann;
genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);
genann *genann_read(FILE *in);
void genann_randomize(genann *ann);
genann *genann_copy(const genann *ann);
void genann_free(genann *ann);
const double *genann_run(const genann *ann, const double *inputs);
void genann_train(const genann *ann, const double *inputs, const double *desired_outputs, double learning_rate);
void genann_write(const genann *ann, FILE *out);
void genann_init_sigmoid_lookup(const genann *ann);
double genann_act_sigmoid(const genann *ann, double a);
double genann_act_sigmoid_cached(const genann *ann, double a);
double genann_act_threshold(const genann *ann, double a);
double genann_act_linear(const genann *ann, double a);
const char *iris_data = "example/iris.data";
double *input;
double *class;
int samples;
const char *class_names[] = {"Iris-setosa", "Iris-versicolor", "Iris-virginica"};
void load_data()
{
FILE *in = fopen("example/iris.data", "r");
if (!in)
{
printf("Could not open file: %s\n", iris_data);
exit(1);
}
char line[1024];
while ((!feof(in)) && fgets(line, 1024, in))
{
samples += 1;
}
fseek(in, 0, 0);
printf("Loading %d data points from %s\n", samples, iris_data);
input = malloc(((sizeof(double)) * samples) * 4);
class = malloc(((sizeof(double)) * samples) * 3);
int i;
int j;
for (i = 0; i < samples; i += 1)
{
double *p = input + (i * 4);
double *c = class + (i * 3);
c[0] = (c[1] = (c[2] = 0.0));
if (fgets(line, 1024, in) == 0)
{
perror("fgets");
exit(1);
}
char *split = strtok(line, ",");
unsigned int split_idx = 0;
for (j = 0; j < 4; j += 1)
{
p[j] = atof(split);
split_idx = strtok(0, ",");
}
split[(strlen(split) - 1) + split_idx] = 0;
if (strcmp(split, class_names[0]) == 0)
{
c[0] = 1.0;
}
else
if (strcmp(split, class_names[1]) == 0)
{
c[1] = 1.0;
}
else
if (strcmp(split, class_names[2]) == 0)
{
c[2] = 1.0;
}
else
{
printf("Unknown class %s.\n", split);
exit(1);
}
}
fclose(in);
}
int main(int argc, char *argv[])
{
printf("GENANN example 4.\n");
printf("Train an ANN on the IRIS dataset using backpropagation.\n");
srand(time(0));
load_data();
genann *ann = genann_init(4, 1, 4, 3);
int i;
int j;
int loops = 5000;
printf("Training for %d loops over data.\n", loops);
for (i = 0; i < loops; i += 1)
{
for (j = 0; j < samples; j += 1)
{
genann_train(ann, input + (j * 4), class + (j * 3), .01);
}
}
int correct = 0;
for (j = 0; j < samples; j += 1)
{
const double *guess = genann_run(ann, input + (j * 4));
if (class[(j * 3) + 0] == 1.0)
{
if ((guess[0] > guess[1]) && (guess[0] > guess[2]))
{
correct += 1;
}
}
else
if (class[(j * 3) + 1] == 1.0)
{
if ((guess[1] > guess[0]) && (guess[1] > guess[2]))
{
correct += 1;
}
}
else
if (class[(j * 3) + 2] == 1.0)
{
if ((guess[2] > guess[0]) && (guess[2] > guess[1]))
{
correct += 1;
}
}
else
{
printf("Logic error.\n");
exit(1);
}
}
printf("%d/%d correct (%0.1f%%).\n", correct, samples, (((double) correct) / samples) * 100.0);
genann_free(ann);
free(input);
free(class);
return 0;
}
void helper_load_data_1(int * const j_ref, FILE * const in, char line[1024], int i)
{
int j = *j_ref;
double *p = input + (i * 4);
double *c = class + (i * 3);
c[0] = (c[1] = (c[2] = 0.0));
if (fgets(line, 1024, in) == 0)
{
perror("fgets");
exit(1);
}
char *split = strtok(line, ",");
unsigned int split_idx = 0;
for (j = 0; j < 4; j += 1)
{
p[j] = atof(split);
split_idx = strtok(0, ",");
}
split[(strlen(split) - 1) + split_idx] = 0;
if (strcmp(split, class_names[0]) == 0)
{
c[0] = 1.0;
}
else
if (strcmp(split, class_names[1]) == 0)
{
c[1] = 1.0;
}
else
if (strcmp(split, class_names[2]) == 0)
{
c[2] = 1.0;
}
else
{
printf("Unknown class %s.\n", split);
exit(1);
}
*j_ref = j;
}