Created
August 19, 2014 04:41
-
-
Save noam87/c5e2ab8dc48498a9fa91 to your computer and use it in GitHub Desktop.
shitty neural network example
This file contains hidden or 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
/** | |
* Trainins input vectors. | |
* | |
* Input used to train neural network. | |
*/ | |
// Square | |
var s_1 = [ | |
-1, -1, -1, -1, -1, | |
-1, 1, 1, 1, -1, | |
-1, 1, -1, 1, -1, | |
-1, 1, 1, 1, -1, | |
-1, -1, -1, -1, -1 | |
]; | |
// X | |
var s_2 = [ | |
1, -1, -1, -1, 1, | |
-1, 1, -1, 1, -1, | |
-1, -1, 1, -1, -1, | |
-1, 1, -1, 1, -1, | |
1, -1, -1, -1, 1 | |
]; | |
var s = [s_1, s_2]; | |
/** | |
* Target vectors. | |
* | |
* The expected output of NN. | |
*/ | |
var t_1 = -1; | |
var t_2 = 1; | |
var t = [t_1, t_2]; | |
/** | |
* Initial weight matrix (a single vector) | |
*/ | |
var weights = [ | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0 | |
]; | |
var threshold = 0; | |
function activationFunction(input, threshold) { | |
if (input >= threshold) return 1; | |
return -1; | |
} | |
/** | |
* Hebb-train the network | |
*/ | |
for (var s_i = 0; s_i < s.length; s_i++) { | |
console.log("training..."); | |
for (var i=0; i < weights.length; i++) { | |
weights[i] = weights[i] + (s[s_i][i] * t[s_i]); | |
} | |
threshold = threshold - t[s_i]; | |
console.log("New weights...", weights); | |
console.log("New threshold...", threshold); | |
} | |
console.log("Network is now trained on two inputs..."); | |
function run(input) { | |
var sum = 0; | |
for (var i=0; i < input.length; i++) { | |
sum += input[i] * weights[i]; | |
} | |
return activationFunction(sum, threshold); | |
} | |
// some inputs to try now that neural network is trained | |
// thinks it's an X | |
var tryme_1 = [ | |
-1, -1, -1, -1, 1, | |
1, 1, -1, 1, -1, | |
-1, -1, 1, -1, -1, | |
-1, 1, -1, 1, -1, | |
1, -1, -1, -1, 1 | |
]; | |
// Doesn't think it's an X | |
var tryme_2 = [ | |
1, -1, -1, -1, 1, | |
-1, 1, 1, 1, -1, | |
-1, 1, 1, 1, -1, | |
-1, 1, -1, 1, -1, | |
-1, -1, -1, -1, -1 | |
]; | |
console.log("Running on input tryme_1...", run(tryme_1), " (thinks it's an X)"); | |
console.log("Running on input tryme_2...", run(tryme_2), " (doesn't think it's an x)"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment