-
Notifications
You must be signed in to change notification settings - Fork 188
/
test.c
200 lines (190 loc) · 5.47 KB
/
test.c
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
#include "Tinn.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
// Data object.
typedef struct
{
// 2D floating point array of input.
float** in;
// 2D floating point array of target.
float** tg;
// Number of inputs to neural network.
int nips;
// Number of outputs to neural network.
int nops;
// Number of rows in file (number of sets for neural network).
int rows;
}
Data;
// Returns the number of lines in a file.
static int lns(FILE* const file)
{
int ch = EOF;
int lines = 0;
int pc = '\n';
while((ch = getc(file)) != EOF)
{
if(ch == '\n')
lines++;
pc = ch;
}
if(pc != '\n')
lines++;
rewind(file);
return lines;
}
// Reads a line from a file.
static char* readln(FILE* const file)
{
int ch = EOF;
int reads = 0;
int size = 128;
char* line = (char*) malloc((size) * sizeof(char));
while((ch = getc(file)) != '\n' && ch != EOF)
{
line[reads++] = ch;
if(reads + 1 == size)
line = (char*) realloc((line), (size *= 2) * sizeof(char));
}
line[reads] = '\0';
return line;
}
// New 2D array of floats.
static float** new2d(const int rows, const int cols)
{
float** row = (float**) malloc((rows) * sizeof(float*));
for(int r = 0; r < rows; r++)
row[r] = (float*) malloc((cols) * sizeof(float));
return row;
}
// New data object.
static Data ndata(const int nips, const int nops, const int rows)
{
const Data data = {
new2d(rows, nips), new2d(rows, nops), nips, nops, rows
};
return data;
}
// Gets one row of inputs and outputs from a string.
static void parse(const Data data, char* line, const int row)
{
const int cols = data.nips + data.nops;
for(int col = 0; col < cols; col++)
{
const float val = atof(strtok(col == 0 ? line : NULL, " "));
if(col < data.nips)
data.in[row][col] = val;
else
data.tg[row][col - data.nips] = val;
}
}
// Frees a data object from the heap.
static void dfree(const Data d)
{
for(int row = 0; row < d.rows; row++)
{
free(d.in[row]);
free(d.tg[row]);
}
free(d.in);
free(d.tg);
}
// Randomly shuffles a data object.
static void shuffle(const Data d)
{
for(int a = 0; a < d.rows; a++)
{
const int b = rand() % d.rows;
float* ot = d.tg[a];
float* it = d.in[a];
// Swap output.
d.tg[a] = d.tg[b];
d.tg[b] = ot;
// Swap input.
d.in[a] = d.in[b];
d.in[b] = it;
}
}
// Parses file from path getting all inputs and outputs for the neural network. Returns data object.
static Data build(const char* path, const int nips, const int nops)
{
FILE* file = fopen(path, "r");
if(file == NULL)
{
printf("Could not open %s\n", path);
printf("Get it from the machine learning database: ");
printf("wget http://archive.ics.uci.edu/ml/machine-learning-databases/semeion/semeion.data\n");
exit(1);
}
const int rows = lns(file);
Data data = ndata(nips, nops, rows);
for(int row = 0; row < rows; row++)
{
char* line = readln(file);
parse(data, line, row);
free(line);
}
fclose(file);
return data;
}
// Learns and predicts hand written digits with 98% accuracy.
int main()
{
// Tinn does not seed the random number generator.
srand(time(0));
// Input and output size is harded coded here as machine learning
// repositories usually don't include the input and output size in the data itself.
const int nips = 256;
const int nops = 10;
// Hyper Parameters.
// Learning rate is annealed and thus not constant.
// It can be fine tuned along with the number of hidden layers.
// Feel free to modify the anneal rate.
// The number of iterations can be changed for stronger training.
float rate = 1.0f;
const int nhid = 28;
const float anneal = 0.99f;
const int iterations = 128;
// Load the training set.
const Data data = build("semeion.data", nips, nops);
// Train, baby, train.
const Tinn tinn = xtbuild(nips, nhid, nops);
for(int i = 0; i < iterations; i++)
{
shuffle(data);
float error = 0.0f;
for(int j = 0; j < data.rows; j++)
{
const float* const in = data.in[j];
const float* const tg = data.tg[j];
error += xttrain(tinn, in, tg, rate);
}
printf("error %.12f :: learning rate %f\n",
(double) error / data.rows,
(double) rate);
rate *= anneal;
}
// This is how you save the neural network to disk.
xtsave(tinn, "saved.tinn");
xtfree(tinn);
// This is how you load the neural network from disk.
const Tinn loaded = xtload("saved.tinn");
// Now we do a prediction with the neural network we loaded from disk.
// Ideally, we would also load a testing set to make the prediction with,
// but for the sake of brevity here we just reuse the training set from earlier.
// One data set is picked at random (zero index of input and target arrays is enough
// as they were both shuffled earlier).
const float* const in = data.in[0];
const float* const tg = data.tg[0];
const float* const pd = xtpredict(loaded, in);
// Prints target.
xtprint(tg, data.nops);
// Prints prediction.
xtprint(pd, data.nops);
// All done. Let's clean up.
xtfree(loaded);
dfree(data);
return 0;
}