Reputation:
I'm trying to understand brain.js.
This is my code; it does not work. (Explaination of what I expect it to do below)
<script src="https://cdn.rawgit.com/harthur/brain/gh-pages/brain-0.6.3.min.js">
<script>
var net = new brain.NeuralNetwork();
net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}]);
var output = net.run([1, 0]);
document.write(output[1]);
</script>
This code imports the brain.min.js code and then teaches a neural network how to do the XOR operation
I expect it to return 0.978 (or somewhere around that), but I'm staring at a blank HTML page. I hope someone who's feeling helpful points me in the right direction. Thanks!
Upvotes: 0
Views: 692
Reputation: 51
This code works in 2019:
<html>
<head>
<script src="https://unpkg.com/brain.js"></script>
</head>
<body>
<script>
const net = new brain.NeuralNetwork({hiddenLayers: [3]});
net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}]);
const output = net.run([0, 1]);
document.write(output[0])
</script>
</body>
</html>
Upvotes: 0
Reputation: 988
Arrays in JavaScript are zero based.
Therefore you have to use document.write(output[0]);
.
Maybe it would be helpfull to use a console.log or even better a debugger;
statement. This way you can inspect your variables through the JS Console.
More info on debugging can be found here.
Upvotes: 1