Why Does My Neural Network Always Predicts The Same Output? (synaptic.js)

132 Views Asked by At

Hello this question is related to Synaptic.js.

I'm trying to use Neural Network to read handwritten digits from images. Image size is 120x90 and it only contains black and white pixels. I'm using html5 canvas to transform images to Matrix before feeding it into a NN with 10800 inputs, 30 hidden layers and 3 outputs.

var myNetwork = new synaptic.Architect.Perceptron(10800,30,3);

var learningRate = .3; 

The code to transform Image to Matrix :

var matrix = [];                
for(i = 0; i < img.height; i++) {
        for(j = 0; j < img.width; j++) {
                var rgba = ctx.getImageData(j, i, 1, 1);
                var avg = ((rgba["data"][0] + rgba["data"][1] + rgba["data"][2]) / 3);

                var indice;

                if(avg < 150) indice = 1;
                else indice = 0;

                matrix.push(indice);
            }
        }

The code above gives a result I expected, basically it transforms 'white' pixels into 0 and 'black' pixels into 1. Attached is a matrix example for an image containing '1' (I resized the actual Matrix because it's too big) : matrix illustration

When I feed the NN two images, each containing '1' or '2' handwritten digits it categorize them just fine. Same thing when I try to feed it five more images containing '1' or '2' handwritten digits, the NN is still able to categorize them well.

The code I use to train NN :

myNetwork.propagate(learningRate, [1, 0]); //Train NN to learn '1'
myNetwork.propagate(learningRate, [0, 1]); //Train NN to learn '2'

However, when I start to feed '3' into the network, it starts to categorize every image inputs (including '1' and '2') into the third category. Basically when the output size is more than 2, the NN will always predict the same output which is the last category I train it to learn.

myNetwork.propagate(learningRate, [0, 0, 1]); //Train NN to learn '3'

I have more than 75 images per number and I have tried to feed them all but the NN always categorize them into '3'. What do I do wrong here? any help is appreciated, Thanks!

0

There are 0 best solutions below