Created
November 3, 2014 17:55
-
-
Save DanBrink91/0883a81c7d5db7de707e to your computer and use it in GitHub Desktop.
Most Simple Neural Network-Like Thing, no backprop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Train data | |
var xor_data = [ | |
[[0, 0], [0]], | |
[[0, 1], [1]], | |
[[1, 0], [1]], | |
[[1, 1], [0]], | |
]; | |
// helper functions | |
function randomRange(count) { | |
var range = []; | |
for(var i = count; i--;){ | |
range.push(Math.random()); | |
} | |
return range; | |
} | |
function randomMatrix(width, height){ | |
var matrix = []; | |
for(var y = height; y--;) { | |
matrix.push(randomRange(width)); | |
} | |
return matrix; | |
} | |
function Net(in_count, hidden_count, out_count) { | |
this.in_layer = new Array(in_count); | |
this.hidden_layer = new Array(hidden_count); | |
this.out_layer = new Array(out_count); | |
// weights | |
this.in_to_hidden = randomMatrix(in_count, hidden_count); | |
this.hidden_to_out = randomMatrix(hidden_count, out_count); | |
} | |
Net.prototype.run = function(input) { | |
if(this.in_layer.length != input.length){ | |
console.log("Input length doesn't match Net input length"); | |
return; | |
} | |
for(var i = this.in_layer.length; i--;) { | |
this.in_layer[i] = input[i]; | |
} | |
// Input -> Hidden | |
for(var i = this.hidden_layer.length; i--;) { | |
var sum = 0.0; | |
for(var j = this.in_layer.length; j--;) { | |
sum += this.in_layer[j] * this.in_to_hidden[i][j]; | |
} | |
this.hidden_layer[i] = sum; | |
} | |
// Hidden -> Output | |
for(var i = this.out_layer.length; i--;) { | |
var sum = 0.0; | |
for(var j = this.hidden_layer.length; j--;) { | |
sum += this.hidden_layer[j] * this.hidden_to_out[i][j]; | |
} | |
this.out_layer[i] = sum; | |
} | |
return this.out_layer; | |
}; | |
var net = new Net(2, 3, 1); | |
console.log(net.run(xor_data[3][0])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment