I'm building a neural network from scratch that is using basic c# functionality like 2D arrays, loops etc I do not want to use TensorFlow or any other packages because my goal is to move this code to C and embed it into a chip. Also im doing it as a learning process.
I made a supervised perceptron network with 16384 inputs (128x128 digits from the MNIST database) and 1 hidden layer with 128 hidden neurons and 10 outputs. My back propagation is working, im not overfitting and i can get it to work fine on one digit at a time.
My problem is when i want to batch train the network on an image like '2' for 1000 iterations and then i train it to recognise a '5' using the final weights after training the '2' it loses the ability to recognise a 2. I figured its because i have to do random character training and keep track of what im training so the network can slowly adjust to be able to weight everything correctly for numbers 0-9 but this doesnt work either. Any suggestions on how I am supposed to train a network simultaneously?
Here is my code for the batch training. Assume that the feed forward and backprop are working fine. bias for hidden outputs is 0.5 and bias for output is 0.35. Weights are initally randomized between 0.00000 and 1.0000 Thanks!
string[] images = { "C:\\NIST\\BATCH_TRAIN\\0.png",
"C:\\NIST\\BATCH_TRAIN\\1.png",
"C:\\NIST\\BATCH_TRAIN\\2.png",
"C:\\NIST\\BATCH_TRAIN\\3.png",
"C:\\NIST\\BATCH_TRAIN\\4.png",
"C:\\NIST\\BATCH_TRAIN\\5.png",
"C:\\NIST\\BATCH_TRAIN\\6.png",
"C:\\NIST\\BATCH_TRAIN\\7.png",
"C:\\NIST\\BATCH_TRAIN\\8.png",
"C:\\NIST\\BATCH_TRAIN\\9.png" };
var watch = System.Diagnostics.Stopwatch.StartNew();
Random r = new Random();
int i = 0;
int count = 0;
for (int n = 0; n < 10000; n++)
{
i = r.Next(0, 10);
if(i != 10)
{
//clear inputs
inputs.Clear();
//Load image into bitmap
Bitmap img = new Bitmap(images[i]);
int pixelCount = img.Width * img.Height;
//Get the input values
for (int k = 0; k < img.Width; k++)
{
for (int j = 0; j < img.Height; j++)
{
Color pixel = img.GetPixel(k, j);
if (pixel.GetBrightness() > 0.5) //means white
{
inputs.Add(0);
}
else
{
//Means black ink pixel found
inputs.Add(1);
}
}
}
//determine and set the target based on the image we are training
double[] target = { 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 };
target[i] = 0.91;
//Add to dictionary for tracking how many and which images were trained
d[i] = d[i] + 1;
//Feed forward
feedforward();
//Then backpropagate Output Layer to Hidden Neurons
backpropagateOH(target);
//Then backpropagate Hidden Neurons to Input Layer
backpropagateIH(target, eta);
//Then clear input list
inputs.Clear();
//increment counter
count++;
}
}
watch.Stop();
var seconds = watch.ElapsedMilliseconds/1000;