-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·294 lines (222 loc) · 5.88 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// amount to change on each step
#define LEARNING_CONST 0.05
#define TRAINING "train-images-idx3-ubyte"
#define LABELS "train-labels-idx1-ubyte"
typedef struct Image Image;
typedef struct Layer Layer;
typedef struct Perceptron Perceptron;
typedef struct Answer Answer;
typedef unsigned char Label;
// represents the image of one number
struct Image {
unsigned char pixels[28*28];
};
// each perceptron has a weight for each pixel in an image
struct Perceptron {
double weights[28*28];
unsigned char *inputs;
double output;
};
struct Layer {
Perceptron cells[10];
};
struct Answer {
int vect[10];
};
Image weightsToImage(Perceptron p){
Image img;
int i=0;
for (i; i < 28*28; i++){
img.pixels[i] = (int) (p.weights[i] * 255);
}
return img;
}
// prints an unsigned character based on a table of characters
// of varying darkness
void printValue(unsigned int n, int simpleFlag){
if (simpleFlag){ printf("%c ", (n>0) ? 'X' : '.'); return; }
char c[8] = {'.','-',':','!','$','#','@','@'};
printf("%c ",c[n/(255/7)]);
}
// method to print 28 x 28 grid
void printImage(Image *image,int simpleFlag){
int i=0;
for (i; i<28; i++){
int j=0;
for (j; j<28;j++){
printValue(image->pixels[(28*i) + j],simpleFlag);
}
printf("\n");
}
}
// loads next image into *img
int getNextImage(Image *img,FILE *fd){
if (fread(img,1,28*28,fd) != 28*28){
printf("Done reading images.\n");
return 0;
}
/* This turns every pixel to 1 if it is not 0
int i=0;
for (i;i<28*28;i++){
if (img->pixels[i]) img->pixels[i] = 1;
}
*/
return 1;
}
// loads next label into *label
int getNextLabel(Label *label,FILE *fd){
if (fread(label,1,1,fd) != 1){
printf("Done reading labels.\n");
return 0;
}
return 1;
}
Perceptron * newPerceptron(unsigned char * input){
Perceptron *p = malloc(sizeof(Perceptron));
p->inputs = input;
int i=0;
// randomize all weights on new perceptron
for (i; i<28*28; i++){
// random percentage
p->weights[i] = (double) rand() / (double)(unsigned) RAND_MAX;
}
return p;
}
void updateOutput(Perceptron *p){
p->output = 0;
int i=0;
for (i;i<28*28;i++){
p->output += p->weights[i] * (double) p->inputs[i];
}
p->output /= (double)(28*28); // between 0 and 1
}
void updateWeight(Perceptron *p, int error){
int i = 0;
for (i; i<28*28;i++){
// always increase or decrease by learning constant
p->weights[i] += LEARNING_CONST * p->inputs[i] * error;
//p->weights[i] += LEARNING_CONST * ( (double)(p->inputs[i]) / (double)255 ) * error;
}
}
void testLayer(Layer l){
char *images_name = "t10k-images-idx3-ubyte";
char *labels_name = "t10k-labels-idx1-ubyte";
FILE *images = fopen(images_name,"r");
FILE *labels = fopen(labels_name,"r");
Image *img = malloc(sizeof(Image));
Label label = 0;
// move the layer's outputs to our new image object
int i=0; // reusable iterable
for (i; i<10; i++){
l.cells[i].inputs = img->pixels;
}
fseek(images,16,SEEK_SET);
fseek(labels,8,SEEK_SET);
double correct = 0;
double incorrect = 0;
while (1){
if (!getNextImage(img,images)) break;
if (!getNextLabel(&label,labels)) break;
// update cell outputs
for (i=0; i<10; i++){
updateOutput(&(l.cells[i]));
}
// find our machine's answer
double max = l.cells[0].output;
int maxPlace = 0;
for (i=1; i<10; i++){
if (l.cells[i].output > max){
maxPlace = i;
max = l.cells[i].output;
}
}
if (maxPlace == label) correct++;
else incorrect++;
printf("Thought answer was %d ; it was %d :: Correct: %f, Wrong: %f, ratio: %f\n",maxPlace,label,correct,incorrect,correct/(correct+incorrect));
}
}
Layer trainLayer(int print){
// open training files
char *images_name = TRAINING;
char *labels_name = LABELS;
FILE *images = fopen(images_name,"r");
FILE *labels = fopen(labels_name,"r");
Image *img = malloc(sizeof(Image));
Label label = 0;
Layer l;
int i=0; // reusable iterable
for (i; i<10; i++){
l.cells[i] = *newPerceptron(img->pixels);
}
// move to first pixel and label (ignore headers)
fseek(images,16,SEEK_SET);
fseek(labels,8,SEEK_SET);
double correct = 0;
double incorrect = 0;
while (1){
if (!getNextImage(img,images)) break;
if (!getNextLabel(&label,labels)) break;
if (print){
printImage(img,1);
printf("Label: %d\n",label);
getchar();
}
// these need allocated inside loop
Answer *machineAnswer = malloc(sizeof(Answer));
Answer *correctAnswer = malloc(sizeof(Answer));
// reset these because apparently malloc hates me
for (i=0; i<10; i++){
correctAnswer->vect[i] = 0;
machineAnswer->vect[i] = 0;
}
// set correct answer
correctAnswer->vect[label] = 1;
// update each perceptron's output
for (i=0; i<10; i++){
updateOutput(&(l.cells[i]));
}
double max = l.cells[0].output;
int maxPlace = 0;
for (i=1; i<10; i++){
if (l.cells[i].output > max){
maxPlace = i;
max = l.cells[i].output;
}
}
if (maxPlace == label) correct++;
else incorrect++;
machineAnswer->vect[maxPlace] = 1; // set our machine's answer
// find error
// update weight of each perceptron as we do so
for (i=0; i<10; i++){
//error->vect[i] = correctAnswer->vect[i] - machineAnswer->vect[i];
updateWeight(&(l.cells[i]),correctAnswer->vect[i] - machineAnswer->vect[i]);
}
printf("Thought answer was %d ; it was %d :: Correct: %f, Wrong: %f, ratio: %f\n",maxPlace,label,correct,incorrect,correct/(correct+incorrect));
// reset answers
free(machineAnswer);
free(correctAnswer);
}
return l;
}
int main(int argc, char **argv){
srand(time(NULL));
int simpleFlag = 0;
if (argc > 1){
sscanf(argv[1],"%d",&simpleFlag);
}
Layer l = trainLayer(simpleFlag);
printf("Done training.\n Here's what each number looks like (hopefully) : \n");
int i=0;
for (i; i < 10; i++){
Image q = weightsToImage(l.cells[i]);
printf("%d: \n",i);
printImage(&q,0);
printf("\n\n");
}
getchar();
testLayer(l);
}