在无头Node JS脚本中运行Brain JS

时间:2019-11-22 13:37:53

标签: javascript node.js shell brain.js

我正在尝试在无头的Node JS脚本中运行Brain JS,我尝试通过NPM通过我的js/run.js页面要求Brain JS,但它出现以下错误:net.train is not a function

const net = require('brain.js')

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([1, 0]) // [0.987]

1 个答案:

答案 0 :(得分:0)

您可以使用这段代码,使用Brain.js文档中的配置执行您想要的操作。

const brain = require('brain.js')

// provide optional config object, defaults shown. See brain.js docs for details.
const config = {
  binaryThresh: 0.5,
  hiddenLayers: [3], // array of ints for the sizes of the hidden layers in the network
  activation: 'sigmoid', // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
  leakyReluAlpha: 0.01, // supported for activation type 'leaky-relu'
}

const net = new brain.NeuralNetwork(config);

net.train([
 { input: [0, 0], output: [0] },
 { input: [0, 1], output: [1] },
 { input: [1, 0], output: [1] },
 { input: [1, 1], output: [0] },
])

for(let input of [[0,0], [0,1], [1,0], [1,1]]) {
    console.log(`Input: [${input[0]},${input[1]}]: output: ${net.run(input)}`);
}
相关问题