2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alex
  • 163
  • 2
  • 15
  • How do people know that the question has been edited? – Alex Oct 18 '20 at 12:19
  • 1
    After the first edit, the question goes into the [Review Queues](https://stackoverflow.com/help/privileges/access-review-queues), one of which is dedicated to post-closure reopens. The queue is operated by volunteers, and they make human judgements about whether something should be reopened. – halfer Oct 18 '20 at 12:22
  • 2
    We see the question move back to the top of the queue. I came here because I saw it a few minutes ago there. Voting to reopen, and it is now open ;-) – trincot Oct 18 '20 at 12:22

0 Answers0