得到不需要的brain.js输出

时间:2019-05-02 18:54:20

标签: node.js machine-learning neural-network brain.js

我正在尝试学习brain.js。我已经编写了一个代码来输入文本并获得一个数字作为输出。但我总是得到NaN作为输出。

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"));

1 个答案:

答案 0 :(得分:0)

您的输出很好,但是您正在使用不兼容的方式来训练brain.NeuralNetwork和输入令牌(字符串)。您需要以某种方式输入数字。一种方法是使用属性为数字的对象。这将起作用:

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 }));

工作示例:https://jsfiddle.net/robertleeplummerjr/xz06ghfp/3/