简体   繁体   中英

Brain.js - TypeError: Cannot read property 'weights' of undefined

I'm trying to forecast future sales with brain.js , but I can't figure out what I'm doing wrong.

I'm following this tutorial , and it works there.

I have the latest version of brain.js and I couldn't find other issues of this nature.

Also, feel free to suggest a better way to do the forecasting.

Error:

/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)

Code:

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

Hey its probaly a problem of brain.js i had a similar problem TypeError: Cannot read properties of undefined (reading 'rows')

In the brain.js github documentation there has section " For training with RNNTimeStep, LSTMTimeStep and GRUTimeStep ":

For "LSTMTimeStep" each training pattern can either:

  • Be an array of numbers
  • Be an array of arrays of numbers

So you can try like

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

let scaledData = data.map(normalize)

The normalize function will return only a number and scaledData will be an array of numbers.

Hopefully this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM