Skip to content

Instantly share code, notes, and snippets.

@sebjwallace
Created May 13, 2016 16:44
Show Gist options
  • Save sebjwallace/ef46291a297db8dacf45c57fd791d9f7 to your computer and use it in GitHub Desktop.
Save sebjwallace/ef46291a297db8dacf45c57fd791d9f7 to your computer and use it in GitHub Desktop.
One of the most simplest 'perception' training the 'AND' operator
var inputs = [
[0,0],
[1,0],
[0,1],
[1,1]
]
var outputs = [
0, 0, 0, 1
]
var weights = [
0, 1
]
var limit = 1000
var count = 0
function train(){
for(var i in inputs){
var sum = 0
for(var w in weights){
sum += weights[w] * inputs[i][w]
}
var output = sum > 1 ? 1 : 0;
console.log('---------------------------')
console.log(i + ', ' + output + ' : ' + outputs[i])
console.log('weights ' + weights)
if(output == outputs[i]){
console.log('correct')
}
else{
console.log('---------------------------')
console.log('mutation')
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