Skip to content

Instantly share code, notes, and snippets.

@dpogorzelski
Last active December 19, 2015 02:29
Show Gist options
  • Save dpogorzelski/5883600 to your computer and use it in GitHub Desktop.
Save dpogorzelski/5883600 to your computer and use it in GitHub Desktop.
Neural network implementation based on 'Introduction to the Math of Neural Networks' book. Not complete yet.
Network = function(input, hidden, output) {
if (arguments.length !== 3) {
console.log('Check your arguments, quitting...');
process.exit();
}
this.B1 = 1.0;
this.B2 = 1.0;
for (var i = 1; i <= input; i++) {
this['I' + i] = 0;
}
for (var i = 1; i <= hidden; i++) {
this['H' + i] = {
sum: 0.0,
out: 0.0,
};
}
for (var i = 1; i <= output; i++) {
this['O' + i] = {
sum: 0.0,
out: 0.0
};
}
for (var i = 1; i <= hidden + output; i++) {
this['w' + i] = [];
}
/*for (var i = 1; i <= input + 1; i++) {
this.w1.push(Math.random() * (1 - (-1)) + (-1));
this.w2.push(Math.random() * (1 - (-1)) + (-1));
}
for (var i = 1; i <= hidden + 1; i++) {
this.w3.push(Math.random() * (1 - (-1)) + (-1));
}*/
this.w1 = [-0.07, 0.22, -0.46];
this.w2 = [0.94, 0.46, 0.10];
this.w3 = [-0.22, 0.58, 0.78];
};
Network.prototype.train = function(input, expected) {
var input1 = []
for (i = 1; i <= input.length; i++) {
this['I' + i] = input[i - 1];
input1.push(this['I' + i]);
}
input1.push(this.B1);
this.H1.sum = this.run(input1, this.w1);
this.H1.out = this.sigmoid(this.H1.sum);
this.H2.sum = this.run(input1, this.w2);
this.H2.out = this.sigmoid(this.H2.sum);
input2 = [this.H1.out, this.H2.out, this.B2];
this.O1.sum = this.run(input2, this.w3);
this.O1.out = this.sigmoid(this.O1.sum);
this.O1.delta = this.delta(this.O1.out, expected);
this.H1.delta = this.deltaHidden(this.H1.out, this.O1.delta, this.w3[0]);
this.H2.delta = this.deltaHidden(this.H2.out, this.O1.delta, this.w3[1]);
this.gradientTable = {};
this.gradientTable.H1O1 = this.gradient(this.H1.out, this.O1.delta);
this.gradientTable.H2O1 = this.gradient(this.H2.out, this.O1.delta);
this.gradientTable.B2O1 = this.gradient(this.B2, this.O1.delta);
this.gradientTable.I1H1 = this.gradient(this.I1, this.H1.delta);
this.gradientTable.I1H2 = this.gradient(this.I1, this.H2.delta);
this.gradientTable.I2H1 = this.gradient(this.I2, this.H1.delta);
this.gradientTable.I2H2 = this.gradient(this.I2, this.H2.delta);
this.gradientTable.B1H1 = this.gradient(this.B1, this.H1.delta);
this.gradientTable.B1H2 = this.gradient(this.B1, this.H2.delta);
console.log(this);
};
Network.prototype.run = function(input, w) {
var sum = 0.0;
w.forEach(function(w, index) {
sum += w * input[index];
})
return sum;
};
Network.prototype.sigmoid = function(x) {
return 1.0 / (1.0 + Math.exp(-x))
};
Network.prototype.delta = function(output, expected) {
var error = (output - expected);
var derivative = output * (1.0 - output);
var delta = -(error) * derivative;
return delta;
};
Network.prototype.deltaHidden = function(output, delta, w) {
var derivative = output * (1.0 - output);
var deltaHidden = derivative * (delta * w);
return deltaHidden;
};
Network.prototype.gradient = function(output, delta) {
return output * delta;
};
var network = new Network(2, 2, 1);
network.train([1.0, 0], 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment