Last active
July 22, 2016 16:19
-
-
Save rukshn/d4923e23d80697d2444d077eb1673e68 to your computer and use it in GitHub Desktop.
Simple perceptron just as I wrote it in node JS.
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
// Sigmoid function | |
function S(num){ | |
var y = 1 / (1 + Math.exp(num)); // y = 1/(1+e^x) | |
return(y) | |
} | |
// Training function | |
function process(arr) { | |
var w = [] | |
var bias = 0.5 | |
var neta = 0.5 | |
var epoch = 2500 | |
var temp = "" | |
w[0] = Math.random() | |
w[1] = Math.random() | |
w[2] = Math.random() | |
for (var ip = 0; ip < epoch; ip++) { | |
for (var i = 0; i < arr.length; i++) { | |
var x = arr[i].x | |
var y = arr[i].y | |
var tar = arr[i].tar | |
var out = S(x*w[0] + y*w[1] + bias*w[2]) | |
var diff = tar-out | |
var E = 0.5 * Math.pow(diff,2) | |
var delta = (out-tar)*(out*(1-out)) | |
w[0] = w[0]+(neta*delta*x) | |
w[1] = w[1]+(neta*delta*y) | |
w[2] = w[2]+(neta*delta*bias) | |
var medium = (ip*4+i) + "," + E + "\n" | |
temp = temp + medium | |
} | |
if (isNaN(E)) { | |
break | |
} | |
// | |
// if (E < 0.00001) { | |
// console.log('true'); | |
// console.log(E); | |
// console.log(w); | |
// break | |
// } | |
// else { | |
// console.log(E); | |
// console.log('false'); | |
// } | |
} | |
var fs = require('fs'); | |
fs.writeFile('error.csv', temp, function (err) { | |
if (err) | |
return console.log(err); | |
console.log(w); | |
}); | |
} | |
// Testing function | |
function run(arr) { | |
// OR weights | |
// var wx = -6.145714143824315 | |
// var wy = -6.14594481839442 | |
// var wbias = 5.393654370020651 | |
// AND weights | |
// var wx = -4.6549735181828 | |
// var wy = -4.654060764413737 | |
// var wbias = 14.031695517058715 | |
//xor weights | |
var wx = 0.0796488512108033 | |
var wy = 0.0637190809265625 | |
var wbias = -0.07964885084210521 | |
var bias = 0.5 | |
for (var i = 0; i < arr.length; i++) { | |
var x = arr[i].x | |
var y = arr[i].y | |
var tar = arr[i].tar | |
var out = S(x*wx + y*wy + bias*wbias) | |
console.log('Target - ' + tar); | |
console.log('Output - ' + out); | |
} | |
} | |
var arr = [ | |
{ | |
"x" : 0, "y": 0, "tar": 0 | |
}, | |
{ | |
"x" : 0, "y": 1, "tar": 1 | |
}, | |
{ | |
"x" : 1, "y": 0, "tar": 1 | |
}, | |
{ | |
"x" : 1, "y": 1, "tar": 0 | |
} | |
] | |
// process(arr) | |
// run(arr) | |
//read the detailed blog post : https://justruky.blogspot.com/2016/07/from-biological-neuron-to-perceptron.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment