Deep Neural Network for MNIST Handwriting Recognition  1.0
Deep Neural Network for MNIST Handwriting Recognition
/Users/mlind/Coding/Github/mnist-dnn/dnn.h
Go to the documentation of this file.
1 
9 #ifndef DNN_HEADER
10 #define DNN_HEADER
11 
12 // Include project libraries
13 #include "util/mnist-utils.h"
14 #include "util/mnist-stats.h"
15 
16 #define MAX_CONVOLUTIONAL_FILTER 10 // check mechanism to avoid users defining wrong conv models
17 
19 typedef struct Vector3D Vector3D;
20 typedef struct Value2D Value2D;
21 typedef struct Volume Volume;
22 typedef struct Network Network;
23 typedef struct Layer Layer;
24 typedef struct Column Column;
25 typedef struct Node Node;
26 typedef struct Connection Connection;
27 
28 typedef double Weight;
29 typedef unsigned long ByteSize;
30 
33 
34 
35 
36 
41 struct Volume{
42  int width;
43  int height;
44  int depth;
45 };
46 
47 
48 
49 
55  LayerType layerType; // what kind of layer is this (INP,CONV,FC,OUT)
56  ActFctType activationType; // what activation function is applied
57  Volume nodeMap; // what is the width/height/depth of this layer
58  int filter; // size of the filter window (conv layers only)
59 };
60 
61 
62 
63 
71 struct Connection{
72  Node *nodePtr; // pointer to the target node
73  Weight *weightPtr; // pointer to a weight that is applied to this connection
74 };
75 
76 
77 
78 
83 struct Node{
84  ByteSize size; // actual byte size of this structure in run-time
85  Weight bias; // value of the bias weight of this node
86  double output; // result of activation function applied to this node
87  double errorSum; // result of error back propagation applied to this node
88  int backwardConnCount; // number of connections to the previous layer
89  int forwardConnCount; // number of connections to the following layer
90  Connection connections[]; // array of connections
91 };
92 
93 
94 
95 
100 struct Column{
101  ByteSize size; // actual byte size of this structure in run-time
102  int maxConnCountPerNode; // maximum number of connections per node in this layer
103  int nodeCount; // number of nodes in this column
104  Node nodes[]; // array of nodes
105 };
106 
107 
108 
109 
114 struct Layer{
115  int id; // index of this layer in the network
116  ByteSize size; // actual byte size of this structure in run-time
117  LayerDefinition *layerDef; // pointer to the definition of this layer
118  Weight *weightsPtr; // pointer to the weights of this layer
119  int columnCount; // number of columns in this layer
120  Column columns[]; // array of columns
121 };
122 
123 
124 
125 
130 struct Network{
131  ByteSize size; // actual byte size of this structure in run-time
132  double learningRate; // factor by which connection weight changes are applied
133  int weightCount; // number of weights in the net's weight block
134  Weight *weightsPtr; // pointer to the start of the network's weights block
135  Weight nullWeight; // memory slot for a weight pointed to by dead connections
136  int layerCount; // number of layers in the network
137  Layer layers[]; // array of layers (of different sizes)
138 };
139 
140 
141 
142 
148 int getLayerNodeCount(LayerDefinition *layerDef);
149 
150 
151 
152 
161 
162 
163 
164 
170 int getLayerWeightCount(LayerDefinition *layerDef);
171 
172 
173 
174 
183 
184 
185 
186 
196 
197 
198 
199 
207 int calcStride(int tgtWidth, int filter, int srcWidth);
208 
209 
210 
211 
218 void feedInput(Network *nn, Vector *v);
219 
220 
221 
222 
229 void feedForwardNetwork(Network *nn);
230 
231 
232 
233 
250 void backPropagateNetwork(Network *nn, int targetClassification);
251 
252 
253 
254 
261 
262 
263 
264 
273 Network *createNetwork(int layerCount, LayerDefinition *layerDefs);
274 
275 
276 
277 
284 LayerDefinition *setLayerDefinitions(int layerCount, ...);
285 
286 
287 
288 
289 #endif
Data structure attached to a node and pointing to another node as well as to a weight.
Definition: dnn.h:71
double errorSum
Definition: dnn.h:87
Network * createNetwork(int layerCount, LayerDefinition *layerDefs)
Creates the neural network based on a given array of layer definitions.
Definition: dnn.c:1372
unsigned long ByteSize
Definition: dnn.h:29
Weight nullWeight
Definition: dnn.h:135
int backwardConnCount
Definition: dnn.h:88
Variably-sized data structure defining a vector with "count" doubles.
Definition: mnist-utils.h:53
Utitlies for displaying details of processing the MNIST data set in the terminal screen.
Definition: dnn.h:32
Data structure defining a 3-dimensional vector used to define the size of a node map.
Definition: dnn.h:41
Node * nodePtr
Definition: dnn.h:72
Definition: dnn.h:32
int getNetworkClassification(Network *nn)
Returns the network's classification of the input image by choosing the node with the hightest output...
Definition: dnn.c:735
int filter
Definition: dnn.h:58
Layer layers[]
Definition: dnn.h:137
int height
Definition: dnn.h:43
ByteSize size
Definition: dnn.h:101
void feedInput(Network *nn, Vector *v)
Feeds some Vector data into the INPUT layer of the network.
Definition: dnn.c:710
ActFctType
Definition: dnn.h:32
int getLayerNodeCount(LayerDefinition *layerDef)
Returns the number of nodes in a layer.
Definition: dnn.c:47
Weight * weightsPtr
Definition: dnn.h:118
int width
Definition: dnn.h:42
Volume nodeMap
Definition: dnn.h:57
int depth
Definition: dnn.h:44
Definition: dnn.h:31
Definition: dnn.h:31
double Weight
Definition: dnn.h:28
double output
Definition: dnn.h:86
int calcStride(int tgtWidth, int filter, int srcWidth)
Calculates the stride (number of nodes/columns that are skipped) in a convolutional kernel...
Definition: dnn.c:802
int getLayerWeightCount(LayerDefinition *layerDef)
Returns the number of weights for a layer (based on a given layer definition)
Definition: dnn.c:138
ByteSize getLayerSize(LayerDefinition *layerDef)
Returns the memory (byte) size of a specific layer based on a given layer definition.
Definition: dnn.c:272
ByteSize size
Definition: dnn.h:84
Variably-sized data structure modeling a vector of nodes.
Definition: dnn.h:100
int nodeCount
Definition: dnn.h:103
LayerType layerType
Definition: dnn.h:55
Definition: dnn.h:31
LayerDefinition * setLayerDefinitions(int layerCount,...)
Returns a pointer to an array of a variable number of layer definitions.
Definition: dnn.c:1510
void backPropagateNetwork(Network *nn, int targetClassification)
Backpropagates the output nodes' errors from output layer backwards to first layer.
Definition: dnn.c:585
ActFctType activationType
Definition: dnn.h:56
double learningRate
Definition: dnn.h:132
Definition: dnn.h:32
int getNodeBackwardConnectionCount(LayerDefinition *layerDef)
Returns the number of backward connections of a NODE (not of a layer)
Definition: dnn.c:63
Variably-sized data structure holding a definable number of columns that form a layer.
Definition: dnn.h:114
Node nodes[]
Definition: dnn.h:104
int weightCount
Definition: dnn.h:133
Definition: dnn.h:31
Weight * weightPtr
Definition: dnn.h:73
ByteSize size
Definition: dnn.h:131
Weight * weightsPtr
Definition: dnn.h:134
LayerType
Definition: dnn.h:31
Connection connections[]
Definition: dnn.h:90
Definition: dnn.h:31
ByteSize size
Definition: dnn.h:116
void feedForwardNetwork(Network *nn)
Feeds forward (=calculating a node's output value and applying an activation function) layer by layer...
Definition: dnn.c:692
ByteSize getLayerWeightBlockSize(LayerDefinition *layerDef)
Returns the memory (byte) size of the weights block for a specific layer.
Definition: dnn.c:190
Weight bias
Definition: dnn.h:85
int layerCount
Definition: dnn.h:136
int columnCount
Definition: dnn.h:119
Column columns[]
Definition: dnn.h:120
Utitlies for handling the MNIST data set files.
Data structure allowing users to define the characteristics of a network.
Definition: dnn.h:54
Variably-sized data structure modeling a neuron with a variable number of connections/weights.
Definition: dnn.h:83
Definition: dnn.h:32
int maxConnCountPerNode
Definition: dnn.h:102
int forwardConnCount
Definition: dnn.h:89
LayerDefinition * layerDef
Definition: dnn.h:117
Variably-sized data structure that serves as the over container for a whole network.
Definition: dnn.h:130
struct Vector3D Vector3D
Definition: dnn.h:19
int id
Definition: dnn.h:115
struct Value2D Value2D
Definition: dnn.h:20