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



我希望在无头节点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]

你可以试一试这段代码,它应该做你想做的事,使用大脑.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)}`);
}

最新更新