Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in AI and Deep Learning by (50.2k points)

I have tried to build a neural network in JavaScript. My javascript code looks like this:

function NeuralFactor(weight) {

    var self = this;

    this.weight = weight;

    this.delta =  0;

}

function Sigmoid(value) {

    return 1 / (1 + Math.exp(-value));

}

function Neuron(isInput) {

    var self = this;

    this.pulse = function() {

        self.output = 0;

        self.input.forEach(function(item) {

            self.output += item.signal.output * item.factor.weight;

        });

        self.output += self.bias.weight;

        self.output = Sigmoid(self.output);

    };

    this.bias = new NeuralFactor(isInput ? 0 : Math.random());

    this.error = 0;

    this.input = [];

    this.output = 0;

    this.findInput = function(signal) {

        var input = self.input.filter(function(input) {

            return signal == input.signal;

        })[0];

        return input;

    };

}

function NeuralLayer() {

    var self = this;

    this.pulse = function() {

        self.neurons.forEach(function(neuron) {

            neuron.pulse();

        });

    };

    this.neurons = [];

    this.train = function(learningRate) {

        self.neurons.forEach(function(neuron) {

            neuron.bias.weight += neuron.bias.delta * learningRate;

            neuron.bias.delta = 0;

            neuron.input.forEach(function(input) {

                input.factor.weight += input.factor.delta * learningRate;

                input.factor.delta = 0;

            })

        })

}

function NeuralNet(inputCount, hiddenCount, outputCount) {

    var self = this;

    this.inputLayer = new NeuralLayer();

    this.hiddenLayer = new NeuralLayer();

    this.outputLayer = new NeuralLayer();

    this.learningRate = 0.5;

    for(var i = 0; i < inputCount; i++)

        self.inputLayer.neurons.push(new Neuron(true));

    for(var i = 0; i < hiddenCount; i++)

        self.hiddenLayer.neurons.push(new Neuron());

    for(var i = 0; i < outputCount; i++)

        self.outputLayer.neurons.push(new Neuron());

    for (var i = 0; i < hiddenCount; i++)

        for (var j = 0; j < inputCount; j++)

            self.hiddenLayer.neurons[i].input.push({

                signal: self.inputLayer.neurons[j],

                factor: new NeuralFactor(Math.random())

            });

    for (var i = 0; i < outputCount; i++)

        for (var j = 0; j < hiddenCount; j++)

            self.outputLayer.neurons[i].input.push({

                signal: self.hiddenLayer.neurons[j],

                factor: new NeuralFactor(Math.random())

            });

    this.pulse = function() {

        self.hiddenLayer.pulse();

        self.outputLayer.pulse();

    };

    this.backPropagation = function(desiredResults) {

        for(var i = 0; i < self.outputLayer.neurons.length; i++) {

            var outputNeuron = self.outputLayer.neurons[i];

            var output = outputNeuron.output;

            outputNeuron.error = (desiredResults[i] - output) * output * (1.0 - output);

        for(var i = 0; i < self.hiddenLayer.neurons.length; i++) {

            var hiddenNeuron = self.hiddenLayer.neurons[i];

            var error = 0;

            for(var j = 0; j < self.outputLayer.neurons.length; j++) {

                var outputNeuron = self.outputLayer.neurons[j];

                error += outputNeuron.error * outputNeuron.findInput(hiddenNeuron).factor.weight * hiddenNeuron.output * (1.0 - hiddenNeuron.output);

hiddenNeuron.error = error;

        for(var j = 0; j < self.outputLayer.neurons.length; j++) {

            var outputNeuron = self.outputLayer.neurons[j];

            for(var i = 0; i < self.hiddenLayer.neurons.length; i++) {

                var hiddenNeuron = self.hiddenLayer.neurons[i];

                outputNeuron.findInput(hiddenNeuron).factor.delta += outputNeuron.error * hiddenNeuron.output;

            

            outputNeuron.bias.delta += outputNeuron.error * outputNeuron.bias.weight;

        

        for(var j = 0; j < self.hiddenLayer.neurons.length; j++) {

            var hiddenNeuron = self.hiddenLayer.neurons[j];

            for(var i = 0; i < self.inputLayer.neurons.length; i++) {

                var inputNeuron = self.inputLayer.neurons[i];

                hiddenNeuron.findInput(inputNeuron).factor.delta += hiddenNeuron.error * inputNeuron.output;

            hiddenNeuron.bias.delta += hiddenNeuron.error * hiddenNeuron.bias.weight;

    };

    this.train = function(input, desiredResults) {

        for(var i = 0; i < self.inputLayer.neurons.length; i++) {

            var neuron = self.inputLayer.neurons[i];

            neuron.output = input[i];

        self.pulse();

        self.backPropagation(desiredResults);

        self.hiddenLayer.train(self.learningRate);

        self.outputLayer.train(self.learningRate);

    };

}

Now I'm trying to learn how to resolve the XOR problem. I'm teaching it like this:

var net = new NeuralNet(2,2,1);

var testInputs = [[0,0], [0,1], [1,0], [1,1]];

var testOutputs = [[1],[0],[0],[1]];

for (var i = 0; i < 1000; i++)

    for(var j = 0; j < 4; j++)

        net.train(testInputs[j], testOutputs[j]);

function UseNet(a, b) {

    net.inputLayer.neurons[0].output = a;

    net.inputLayer.neurons[1].output = b;

    net.pulse();

    return net.outputLayer.neurons[0].output;

}

The problem is that all results that I get is close to 0.5 and pretty random, no matter what arguments I use. For example:

UseNet(0,0) => 0.5107701166677714

UseNet(0,1) => 0.4801498747476413

UseNet(1,0) => 0.5142463167153447

UseNet(1,1) => 0.4881829364416052

What can be wrong with my code?

1 Answer

0 votes
by (119k points)

In the above code, try instead of:

var testInputs = [[0,0], [0,1], [1,0], [1,1]];

var testOutputs = [[1],[0],[0],[1]];

This:

var testInputs = [[0.05,0.05], [0.05,0.95], [0.95,0.05], [0.95,0.95]];

var testOutputs = [[1],[0],[0],[1]];

or

var testInputs = [[0,0], [0,1], [1,0], [1,1]];

var testOutputs = [[0.95],[0.05],[0.05],[0.95]];

If you want to learn Artificial Intelligence, then check out this Artificial Intelligence Course by Intellipaat.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...