Created
May 13, 2016 17:44
-
-
Save sebjwallace/5ea2f8b42390221052fc0cb4249b48b5 to your computer and use it in GitHub Desktop.
Another simple 'perceptron' implementing a bias to work with both AND / OR
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
var inputs = [ | |
[0,0], | |
[1,0], | |
[0,1], | |
[1,1] | |
] | |
var outputs = [ | |
// AND | |
// 0, 0, 0, 1 | |
// OR | |
0, 1, 1, 1 | |
] | |
var weights = [ | |
0, 1 | |
] | |
var limit = 1000 | |
var count = 0 | |
function train(){ | |
for(var i in inputs){ | |
var bias = 0.5 | |
var sum = 0 | |
for(var w in weights){ | |
sum += weights[w] * (inputs[i][w] + bias) | |
} | |
var output = sum > 1 ? 1 : 0; | |
console.log('---------------------------') | |
console.log(count + ',' + i) | |
console.log('outputs ' + output + ':' + outputs[i]) | |
console.log('weights ' + weights) | |
console.log('sum ' + sum) | |
if(output == outputs[i]){ | |
console.log('correct') | |
} | |
else{ | |
console.log('---------------------------') | |
console.log('incorrect') | |
for(var w in weights){ | |
weights[w] = (Math.floor(Math.random() * 10) + 0) / 10 | |
console.log(weights[w]) | |
} | |
if(count < limit){ | |
count++ | |
train() | |
} | |
} | |
} | |
} | |
train(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment