javascript Brain.js -类型错误:无法读取未定义的属性“weights”

57hvy0tb  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(141)

我试图用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]));
gz5pxeao

gz5pxeao1#

嘿,这可能是brainiderjs的问题,我也有类似的问题TypeError: Cannot read properties of undefined (reading 'rows')

ubof19bj

ubof19bj2#

在brain.js github文档中,有一节“使用RNNTimeStep、LSTMTimeStep和GRUTimeStep进行培训”:
对于**“LSTMTimeStep”**,每个训练模式可以:

  • 为数字数组
  • 是数字数组的数组

所以你可以试试

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

let scaledData = data.map(normalize)

normalize函数将只返回一个数字,scaledData将是一个数字数组。
希望这能有所帮助。

相关问题