MNIST-1LNN  1.0
A simple 1-layer neural network to recognize handwritten single digit numbers from the MNIST image files.
Data Structures | Macros | Typedefs | Functions
1lnn.h File Reference

Machine learning functionality for a 1-layer neural network. More...

#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  Cell
 Core unit of the neural network (neuron and synapses) More...
 
struct  Layer
 The single (output) layer of this network (a layer is number cells) More...
 
struct  Vector
 Data structure containing defined number of integer values (the output vector contains values for 0-9) More...
 

Macros

#define NUMBER_OF_INPUT_CELLS   784
 use 28*28 input cells (= number of pixels per MNIST image) More...
 
#define NUMBER_OF_OUTPUT_CELLS   10
 use 10 output cells to model 10 digits (0-9) More...
 
#define LEARNING_RATE   0.05
 Incremental increase for changing connection weights. More...
 

Typedefs

typedef struct Cell Cell
 
typedef struct Layer Layer
 
typedef struct Vector Vector
 

Functions

Vector getTargetOutput (int targetIndex)
 Returns an output vector with targetIndex set to 1, all others to 0. More...
 
void initLayer (Layer *l)
 Initialize layer by setting all weights to random values [0-1]. More...
 
int getLayerPrediction (Layer *l)
 Returns the index of the cell with the hightest output. More...
 
void setCellInput (Cell *c, MNIST_Image *img)
 Sets a cell's input according to the pixels of a given MNIST image. More...
 
void calcCellOutput (Cell *c)
 Calculates a cell's output by suming all input-weight-products. More...
 
double getCellError (Cell *c, int target)
 Returns the difference between a target value and the cell's ouput. More...
 
void updateCellWeights (Cell *c, double err)
 Updates a cell's weights based on given error and LEARNING_RATE. More...
 
void trainCell (Cell *c, MNIST_Image *img, int target)
 Performs the training algorithm. More...
 
void testCell (Cell *c, MNIST_Image *img, int target)
 Performs the testing of the trained network. More...
 

Detailed Description

Machine learning functionality for a 1-layer neural network.

Author
Matt Lind
Date
July 2015

Definition in file 1lnn.h.

Macro Definition Documentation

#define LEARNING_RATE   0.05

Incremental increase for changing connection weights.

Definition at line 14 of file 1lnn.h.

#define NUMBER_OF_INPUT_CELLS   784

use 28*28 input cells (= number of pixels per MNIST image)

Definition at line 11 of file 1lnn.h.

#define NUMBER_OF_OUTPUT_CELLS   10

use 10 output cells to model 10 digits (0-9)

Definition at line 12 of file 1lnn.h.

Typedef Documentation

typedef struct Cell Cell

Definition at line 18 of file 1lnn.h.

typedef struct Layer Layer

Definition at line 19 of file 1lnn.h.

typedef struct Vector Vector

Definition at line 20 of file 1lnn.h.

Function Documentation

void calcCellOutput ( Cell c)

Calculates a cell's output by suming all input-weight-products.

Parameters
cA cell of a NN layer

Calculates a cell's output by suming all input-weight-products and normalizes to [0-1].

Definition at line 104 of file 1lnn.c.

double getCellError ( Cell c,
int  target 
)

Returns the difference between a target value and the cell's ouput.

Parameters
cThe cell whose output is to be compared
targetThe desired value (= correct answer in supervised learning)

Returns the difference between a target value and the cell's ouput

Definition at line 122 of file 1lnn.c.

int getLayerPrediction ( Layer l)

Returns the index of the cell with the hightest output.

Parameters
lA pointer to a NN layer

The output prediction is derived by simply sorting all output values and using the index (=0-9 number) of the highest value as the prediction.

Definition at line 62 of file 1lnn.c.

Vector getTargetOutput ( int  targetIndex)

Returns an output vector with targetIndex set to 1, all others to 0.

Parameters
targetIndexIndex of the output that is to be set to 1

Returns an output vector with targetIndex set to 1, all others to 0

Definition at line 22 of file 1lnn.c.

void initLayer ( Layer l)

Initialize layer by setting all weights to random values [0-1].

Parameters
lA pointer to a NN layer

Initialize layer by setting all weights to random values [0-1]

Attention
It actually makes no difference whether the weights are initialized to a constant (e.g. 0.5) or to a random number. The result (85% success rate) will not change significantly.

Definition at line 40 of file 1lnn.c.

void setCellInput ( Cell c,
MNIST_Image img 
)

Sets a cell's input according to the pixels of a given MNIST image.

Parameters
cA pointer to a cell
imgA pointer to an MNIST image

Creates an input vector of length NUMBER_OF_INPUT_CELLS of a given MNIST image, setting input vector cells to [0,1] based on the pixels of the image. Scalar pixel intensity [=grey-scale] is ignored, only 0 or 1 [=black-white].

Definition at line 89 of file 1lnn.c.

void testCell ( Cell c,
MNIST_Image img,
int  target 
)

Performs the testing of the trained network.

Parameters
cPointer to the cell that is to be trained
imgPointer to the image that is to be processed
targetDesired output value

Performs the testing of the trained network Same as training a cell, but without updating weights (learning)

Definition at line 169 of file 1lnn.c.

void trainCell ( Cell c,
MNIST_Image img,
int  target 
)

Performs the training algorithm.

Parameters
cPointer to the cell that is to be trained
imgPointer to the image that is to be processed
targetDesired output value

Performs the training algorithm: feeding input, calculate output, calculate error, update weights)

Definition at line 151 of file 1lnn.c.

void updateCellWeights ( Cell c,
double  err 
)

Updates a cell's weights based on given error and LEARNING_RATE.

Parameters
cThe cell whose weights are to be updated.
errThe error (difference between desired output and actual output

Updates a cell's weights based on given error and LEARNING_RATE

Definition at line 136 of file 1lnn.c.