Created
August 13, 2013 14:41
-
-
Save mmalecki/6221816 to your computer and use it in GitHub Desktop.
I taught a neural network to recognize a cross
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 nn = require('nn'); | |
var correct = [ | |
[ | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 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, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 1, | |
0, 0, 0, 0, 0, 0, 0, 1 | |
], | |
[ | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
1, 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, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 1, 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, 0, 0, 0, 1, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 1, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0 | |
], | |
[ | |
0, 0, 0, 0, 0, 0, 0, 1, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 1, 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, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 1, 1, 1, 1, 0, 0, | |
0, 0, 0, 1, 1, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0 | |
], | |
]; | |
var incorrect = [ | |
[ | |
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, 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, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0 | |
], | |
[ | |
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, 1, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, | |
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0, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0 | |
] | |
]; | |
var n = nn({ | |
layers: [ 12, 6 ], | |
iterations: 20000 | |
}); | |
var trainData = []; | |
correct.forEach(function (item) { | |
trainData.push({ input: item, output: [1] }); | |
}); | |
incorrect.forEach(function (item) { | |
trainData.push({ input: item, output: [0] }); | |
}); | |
console.time('training'); | |
n.train(trainData); | |
console.timeEnd('training'); | |
test.forEach(function (item) { | |
item.forEach(function (val, i) { | |
if (i % 8 === 0) { | |
console.log(); | |
} | |
process.stdout.write(val ? '\u25A0' : ' '); | |
}); | |
console.time('send'); | |
console.dir(n.send(item)); | |
console.timeEnd('send'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment