Created
May 20, 2016 00:09
-
-
Save wtfaremyinitials/d5eca0f33549a8a64e95c63115d39ce0 to your computer and use it in GitHub Desktop.
Addition with neural networks
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 brain = require('brain.js') | |
var net = new brain.NeuralNetwork(); | |
console.log('Generating training data...'); | |
var data = []; | |
for(var i=0; i<100000; i++) { | |
var obj = {}; | |
obj.input = []; | |
obj.input[0] = Math.random(); | |
obj.input[1] = Math.random(); | |
obj.output = []; | |
obj.output[0] = obj.input[0] + obj.input[1]; | |
if(obj.output[0] > 1) continue; | |
data.push(obj); | |
} | |
console.log('Done.'); | |
console.log('Training...'); | |
var meta = net.train(data); | |
console.log('Done. (' + meta.iterations + ' iterations)'); | |
function round(a) { | |
var b = 2; | |
var num = (Math.round(a * Math.pow(10, b))); | |
num += ""; | |
while(num.length < b) | |
num = "0" + num | |
return num; | |
} | |
function attempt(a, b) { | |
if(a + b > 1) { a /= 2; b /= 2; } | |
var res = net.run([a, b]); | |
console.log(round(a) + ' + ' + round(b) + ' = ' + round( a + b) + ' \u2248 ' + round(res)); | |
} | |
for(var i=0; i<10; i++) | |
attempt(Math.random(), Math.random()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment