MNIST-1LNN  1.0
A simple 1-layer neural network to recognize handwritten single digit numbers from the MNIST image files.
1lnn.h
Go to the documentation of this file.
1 /**
2  * @file 1lnn.h
3  * @brief Machine learning functionality for a 1-layer neural network
4  * @author Matt Lind
5  * @date July 2015
6  */
7 
8 
9 #include <stdio.h>
10 
11 #define NUMBER_OF_INPUT_CELLS 784 ///< use 28*28 input cells (= number of pixels per MNIST image)
12 #define NUMBER_OF_OUTPUT_CELLS 10 ///< use 10 output cells to model 10 digits (0-9)
13 
14 #define LEARNING_RATE 0.05 ///< Incremental increase for changing connection weights
15 
16 
17 
18 typedef struct Cell Cell;
19 typedef struct Layer Layer;
20 typedef struct Vector Vector;
21 
22 
23 
24 
25 /**
26  * @brief Core unit of the neural network (neuron and synapses)
27  */
28 
29 struct Cell{
32  double output;
33  double bias;
34 };
35 
36 
37 
38 
39 /**
40  * @brief The single (output) layer of this network (a layer is number cells)
41  */
42 
43 struct Layer{
45 };
46 
47 
48 
49 
50 /**
51  * @brief Data structure containing defined number of integer values (the output vector contains values for 0-9)
52  */
53 
54 struct Vector{
56 };
57 
58 
59 
60 
61 /**
62  * @brief Returns an output vector with targetIndex set to 1, all others to 0
63  * @param targetIndex Index of the output that is to be set to 1
64  */
65 
66 Vector getTargetOutput(int targetIndex);
67 
68 
69 
70 
71 /**
72  * @brief Initialize layer by setting all weights to random values [0-1]
73  * @param l A pointer to a NN layer
74  */
75 
76 void initLayer(Layer *l);
77 
78 
79 
80 
81 /**
82  * @brief Returns the index of the cell with the hightest output
83  * @param l A pointer to a NN layer
84  */
85 
87 
88 
89 
90 
91 /**
92  * @brief Sets a cell's input according to the pixels of a given MNIST image
93  * @param c A pointer to a cell
94  * @param img A pointer to an MNIST image
95  */
96 
97 void setCellInput(Cell *c, MNIST_Image *img);
98 
99 
100 
101 
102 /**
103  * @brief Calculates a cell's output by suming all input-weight-products
104  * @param c A cell of a NN layer
105  */
106 
107 void calcCellOutput(Cell *c);
108 
109 
110 
111 
112 /**
113  * @brief Returns the difference between a target value and the cell's ouput
114  * @param c The cell whose output is to be compared
115  * @param target The desired value (= correct answer in supervised learning)
116  */
117 
118 double getCellError(Cell *c, int target);
119 
120 
121 
122 
123 /**
124  * @brief Updates a cell's weights based on given error and LEARNING_RATE
125  * @param c The cell whose weights are to be updated.
126  * @param err The error (difference between desired output and actual output
127  */
128 
129 void updateCellWeights(Cell *c, double err);
130 
131 
132 
133 
134 /**
135  * @brief Performs the training algorithm
136  * @param c Pointer to the cell that is to be trained
137  * @param img Pointer to the image that is to be processed
138  * @param target Desired output value
139  */
140 
141 void trainCell(Cell *c, MNIST_Image *img, int target);
142 
143 
144 
145 
146 /**
147  * @brief Performs the testing of the trained network
148  * @param c Pointer to the cell that is to be trained
149  * @param img Pointer to the image that is to be processed
150  * @param target Desired output value
151  */
152 
153 
154 void testCell(Cell *c, MNIST_Image *img, int target);
155 
Cell cell[NUMBER_OF_OUTPUT_CELLS]
Definition: 1lnn.h:44
Data structure containing defined number of integer values (the output vector contains values for 0-9...
Definition: 1lnn.h:54
int getLayerPrediction(Layer *l)
Returns the index of the cell with the hightest output.
Definition: 1lnn.c:62
void calcCellOutput(Cell *c)
Calculates a cell's output by suming all input-weight-products.
Definition: 1lnn.c:104
Vector getTargetOutput(int targetIndex)
Returns an output vector with targetIndex set to 1, all others to 0.
Definition: 1lnn.c:22
double bias
Definition: 1lnn.h:33
void testCell(Cell *c, MNIST_Image *img, int target)
Performs the testing of the trained network.
Definition: 1lnn.c:169
void trainCell(Cell *c, MNIST_Image *img, int target)
Performs the training algorithm.
Definition: 1lnn.c:151
double output
Definition: 1lnn.h:32
void initLayer(Layer *l)
Initialize layer by setting all weights to random values [0-1].
Definition: 1lnn.c:40
#define NUMBER_OF_OUTPUT_CELLS
use 10 output cells to model 10 digits (0-9)
Definition: 1lnn.h:12
Core unit of the neural network (neuron and synapses)
Definition: 1lnn.h:29
The single (output) layer of this network (a layer is number cells)
Definition: 1lnn.h:43
double weight[NUMBER_OF_INPUT_CELLS]
Definition: 1lnn.h:31
double getCellError(Cell *c, int target)
Returns the difference between a target value and the cell's ouput.
Definition: 1lnn.c:122
#define NUMBER_OF_INPUT_CELLS
use 28*28 input cells (= number of pixels per MNIST image)
Definition: 1lnn.h:11
int val[NUMBER_OF_OUTPUT_CELLS]
Definition: 1lnn.h:55
double input[NUMBER_OF_INPUT_CELLS]
Definition: 1lnn.h:30
void updateCellWeights(Cell *c, double err)
Updates a cell's weights based on given error and LEARNING_RATE.
Definition: 1lnn.c:136
Data block defining a MNIST image.
Definition: mnist-utils.h:40
void setCellInput(Cell *c, MNIST_Image *img)
Sets a cell's input according to the pixels of a given MNIST image.
Definition: 1lnn.c:89