-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.c
172 lines (141 loc) · 5.55 KB
/
nn.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
#include "nn.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
static void initLayer(int numberOfNodes, int numberOfWeights, Layer* layer);
static void initNode(int numberOfWeights, Node* node);
static double sigmoid(double value);
static double sigmoidDerivative(double nodeOutput);
static void feedForwardLayer(Layer* previousLayer, Layer* layer);
static void feedForward(Network* network, Image* img);
static void updateNode(Layer* previousLayer, double backPropValue, Node* node);
static void backPropagate(Network* network, int label);
static uint8_t getClassification(Layer* layer);
void initNetwork(Network* network){
initLayer(IMAGE_SIZE, 0, &network->inputLayer);
initLayer(HIDDEN_LAYER_SIZE, IMAGE_SIZE, &network->hiddenLayer);
initLayer(OUTPUT_SIZE, HIDDEN_LAYER_SIZE, &network->outputLayer);
}
void trainNetwork(Network* network){
FILE *imageFile;
FILE *labelFile;
ImageFileHeader imageFileHeader;
imageFile = openImageFile(TRAINING_SET_IMAGE_FILE_NAME, &imageFileHeader);
labelFile = openLabelFile(TRAINING_SET_LABEL_FILE_NAME);
for(int i=0; i<imageFileHeader.maxImages; i++){
Image img;
getImage(imageFile, &img);
uint8_t label = getLabel(labelFile);
feedForward(network, &img);
backPropagate(network, label);
}
}
void testNetwork(Network *network){
FILE *imageFile;
FILE *labelFile;
ImageFileHeader imageFileHeader;
imageFile = openImageFile(TEST_SET_IMAGE_FILE_NAME, &imageFileHeader);
labelFile = openLabelFile(TEST_SET_LABEL_FILE_NAME);
int errCount = 0;
for(int i=0; i<imageFileHeader.maxImages; i++){
Image img;
getImage(imageFile, &img);
uint8_t lbl = getLabel(labelFile);
feedForward(network, &img);
uint8_t classification = getClassification(&network->outputLayer);
if (classification!=lbl){
errCount++;
}
}
fclose(imageFile);
fclose(labelFile);
printf("Test Accuracy: %0.2f%%\n", ((double)(imageFileHeader.maxImages - errCount) / imageFileHeader.maxImages) * 100);
}
static void initLayer(int numberOfNodes, int numberOfWeights, Layer* layer){
Node* nodes = malloc(numberOfNodes * sizeof(Node));
for(int hn=0; hn<numberOfNodes; ++hn){
Node* node = &nodes[hn];
initNode(numberOfWeights, node);
}
layer->numberOfNodes = numberOfNodes;
layer->nodes = nodes;
}
static void initNode(int numberOfWeights, Node* node){
//Initialize weights between -0.7 and 0.7
double* weights = malloc(numberOfWeights * sizeof(double));
for(int w=0; w<numberOfWeights; ++w){
weights[w] = 0.7 * (rand()/(double)(RAND_MAX));
if (w%2){
weights[w] = -weights[w];
}
}
node->numberOfWeights = numberOfWeights;
node->weights = weights;
node->bias = rand()/(double)(RAND_MAX);
}
static double sigmoid(double value){
return 1.0 / (1.0 + exp(-value));
}
static double sigmoidDerivative(double nodeOutput){
return nodeOutput * (1- nodeOutput);
}
static void feedForwardLayer(Layer* previousLayer, Layer* layer){
for(int hn=0; hn<layer->numberOfNodes; ++hn){
Node* node = &layer->nodes[hn];
node->output = node->bias;
for(int w=0; w<previousLayer->numberOfNodes; ++w){
node->output += previousLayer->nodes[w].output * node->weights[w];
}
node->output = sigmoid(node->output);
}
}
static void feedForward(Network* network, Image* img){
//Populate the input layer with normalized input
for(int i=0; i<IMAGE_SIZE; ++i)
{
network->inputLayer.nodes[i].output = (double)(img->pixels[i] / 255.0);
}
feedForwardLayer(&network->inputLayer, &network->hiddenLayer);
feedForwardLayer(&network->hiddenLayer, &network->outputLayer);
}
static void updateNode(Layer* previousLayer, double backPropValue, Node* node){
for(int hn=0; hn<previousLayer->numberOfNodes; ++hn){
Node* previousLayerNode = &previousLayer->nodes[hn];
node->weights[hn] += LEARNING_RATE * previousLayerNode->output * backPropValue;
}
node->bias += LEARNING_RATE * backPropValue;
}
static void backPropagate(Network* network, int label){
Layer* hiddenLayer = &network->hiddenLayer;
Layer* outputLayer = &network->outputLayer;
for(int on=0; on<outputLayer->numberOfNodes; ++on){
Node* outputNode = &outputLayer->nodes[on];
int nodeTarget = (on==label) ? 1:0;
double errorDelta = nodeTarget - outputNode->output;
double backPropValue = errorDelta * sigmoidDerivative(outputNode->output);
outputNode->backPropValue = backPropValue;
updateNode(&network->hiddenLayer, outputNode->backPropValue, outputNode);
}
for(int hn=0; hn<hiddenLayer->numberOfNodes; ++hn){
Node* hiddenNode = &hiddenLayer->nodes[hn];
double outputNodesBackPropSum = 0;
for(int on=0; on<outputLayer->numberOfNodes; ++on){
Node* outputNode = &outputLayer->nodes[on];
outputNodesBackPropSum += outputNode->backPropValue * outputNode->weights[hn];
}
double hiddenNodeBackPropValue = outputNodesBackPropSum * sigmoidDerivative(hiddenNode->output);
updateNode(&network->inputLayer, hiddenNodeBackPropValue, hiddenNode);
}
}
static uint8_t getClassification(Layer* layer){
double maxOutput = 0;
int maxIndex = 0;
for(int on=0; on<layer->numberOfNodes; ++on){
double nodeOutput = layer->nodes[on].output;
if(nodeOutput > maxOutput){
maxOutput = nodeOutput;
maxIndex = on;
}
}
return (uint8_t)maxIndex;
}