Uncaught (in promise) Error: Input 0 is incompatible with layer lstm_LSTM1: expected ndim=3, found ndim=2

133 views Asked by At

Here's the code:

var options = {
     task: "regression",
     debug: true,
     inputs: ["date"],
     outputs: ["price"],
     optimizer: "adam",
     loss: "meanSquaredError",
     layers: [
          {
               type: 'dense',
               units: 1,
               inputShape: [1],
               activation: 'tanh',
               useBias: true,
          },
          {
               type: 'lstm',
               units: 1,
               inputShape: [1,1],
               activation: 'tanh',
               useBias: true,
               return_sequences: true,
          },
          {
               type: 'dense',
               units: 1,
               inputShape: [1],
               activation: 'tanh',
               useBias: false,
          },
     ],
};
     

var nn = ml5.neuralNetwork(options);
setData();

async function getData(){
     var data = await fetch("https://raw.githubusercontent.com/cryptnotehq/filestorage/main/apple_stock.json");
     data = await data.json();
     var cleaned = await data.map( (entry) => {  
          var date = entry.Date.split("-");
          date = new Date(date[0],date[1],date[2]).getTime();
          var result = {
               "date": date,
               "price": entry.High,
          };
          return result;
     }).filter( result => (result.date != "" || result.date != undefined) && (result.price != "" || result.price != undefined) );
     return cleaned;
}

async function setData() {
     var obj = await getData();
     obj.forEach(item => {
          var input = { "date": parseInt(item.date) };
          var output = { "price": parseInt(item.price) };
          nn.addData(input, output);
     });
     
     nn.normalizeData();
     
     train();
}

function train() {
     var trainingOptions = {
          epochs: 256,
          batchSize: 1024,
     };
     
     nn.train(trainingOptions, predict);
     console.log(nn.data);
}

function predict(){
     nn.predict([ parseInt(new Date(2020,10,17).getTime()) ]).then((result) => {
          console.log(result);
     });
     
     //nn.save();
}

The code can also be seen in this Fiddle. The error can be viewed in the Developer Console in any browser.

I expect the code to run. I tried to change the inputShape of the lstm layer and the first "dense" layer. When I change the inputShape of the first layer to [10048,1] and the lstm layer to [1,1,1] I get this error: Uncaught (in promise) Error: Error when checking : expected dense_Dense1_input to have 3 dimension(s), but got array with shape [1,1]

Here's the second approach in this Fiddle.

I don't know what else I could do, I've run out of ideas.

0

There are 0 answers