Brain.js-TypeError:无法读取未定义的属性“权重”

时间:2020-02-13 13:25:56

标签: javascript machine-learning neural-network forecasting

我正在尝试使用brain.js来预测未来的销售量,但是我无法弄清楚自己做错了什么。

我正在关注this tutorial,并且可以在其中运行。

我拥有brain.js的最新版本,但找不到其他这种性质的问题。

此外,请随时提出一种更好的预测方法。

错误:

/node_modules/brain.js/src/recurrent/rnn-time-step.js:279
    const result = [lastOutput.weights[0]];
                               ^
TypeError: Cannot read property 'weights' of undefined
    at LSTMTimeStep.forecastNumbers (/node_modules/brain.js/src/recurrent/rnn-time-step.js:279:32)
    at LSTMTimeStep.runObject (/node_modules/brain.js/src/recurrent/rnn-time-step.js:293:14)
    at LSTMTimeStep.run (/node_modules/brain.js/src/recurrent/rnn-time-step.js:104:21)
    at Object.<anonymous> (/forecast.js:123:17)

代码:

const brain = require("brain.js");

let data = [
    { sales: 0 },
    { sales: 0 },
    { sales: 0 },
    { sales: 0 },
    { sales: 0 },
    { sales: 0 },
    { sales: 92 },
    { sales: 759 },
    { sales: 3691 },
    { sales: 4039 },
    { sales: 2257 },
    { sales: 1736 },
    { sales: 3979 },
    { sales: 3170 },
    { sales: 6092 },
    { sales: 7839 },
    { sales: 5764 },
    { sales: 5512 },
    { sales: 5494 },
    { sales: 7458 },
    { sales: 3721 },
    { sales: 8512 },
    { sales: 1089 },
    { sales: 7462 },
    { sales: 710 },
    { sales: 4534 },
    { sales: 6224 },
    { sales: 7610 },
    { sales: 3976 },
    { sales: 6243 },
    { sales: 1532 },
    { sales: 2204 },
    { sales: 801 },
    { sales: 1575 },
    { sales: 2144 },
    { sales: 3679 },
];

let max = Math.max(...data.map(o => o.sales));
let min = Math.min(...data.map(o => o.sales));

function normalize(step) {
    return { sales: (step.sales - min) / (max - min) };
}

let scaledData = data.map(normalize)

let trainingData = [
    scaledData.slice(0, 12),
    scaledData.slice(12, 24),
    scaledData.slice(24, 36),
]

const net = new brain.recurrent.LSTMTimeStep({
    inputSize: 1,
    hiddenLayers: [1], 
    outputSize: 1
});

net.train(trainingData, {
    iterations: 200,
    learningRate: 0.005,
    errorTresh: 0.02
})

console.log(net.run(trainingData[0]));

0 个答案:

没有答案