andrDev
andrDev

Reputation: 23

getting unwanted brain.js output

I am trying to learn brain.js. I have written a code to input a text and get a number as an output. but I always get NaN as output.

var brain = require('brain.js')
var net = new brain.NeuralNetwork();

net.train([
  {input: "", output:[0]},
  {input: "Jack", output:[1]},
  {input: "Tim", output: [0]},
  {input: "James", output: [0]},
  {input: "JOHN", output: [0]},
  {input: "cathy", output: [0]},
  {input: "Boom", output: [0]},
]);

console.log("Jack = "+net.run("Jack"));
console.log("JOHN = "+net.run("JOHN"));
console.log("cathy = "+net.run("cathy"));

Upvotes: 0

Views: 116

Answers (1)

Robert Plummer
Robert Plummer

Reputation: 652

Your output is fine, but you are using an incompatible means of training brain.NeuralNetwork with input tokens (strings). You need to feed in numbers somehow. A way to do this is with objects who's properties are numbers. This will work:

var brain = require('brain.js')
var net = new brain.NeuralNetwork();

net.train([
  {input: { "": 1 }, output:[0]},
  {input: { "Jack": 1 }, output:[1]},
  {input: { "Tim": 1 }, output: [0]},
  {input: { "James": 1 }, output: [0]},
  {input: { "JOHN": 1 }, output: [0]},
  {input: { "cathy": 1 }, output: [0]},
  {input: { "Boom": 1 }, output: [0]},
]);

console.log("Jack = "+net.run({ "Jack": 1 }));
console.log("JOHN = "+net.run({ "JOHN": 1 }));
console.log("cathy = "+net.run({ "cathy": 1 }));

Working example: https://jsfiddle.net/robertleeplummerjr/xz06ghfp/3/

Upvotes: 1

Related Questions