title: Time Series Forecasting in Tensorflow js
body: <p>I'm new in this field. </p>
  
  <p>I made this code, but it doesn't work well, because I only see a medium price, but not a real forecasting. </p>
  
  <p>I created a 3d tensor with some previous open,high,low and close prices, with a 5 time steps, and i need to forecast the next close number.</p>
  
  <p>Example of input:
  open high low close  for 5 timesteps for 75 samples </p>
  
  <pre><code>[/*samples size (75)*/
  [/*timestep1*/
  /* open, high, low, close*/
  [1905,1906,1903,1904]
  [1904,1905,1904,1906]
  [1906,1907,1904,1907]
  [1907,1908,1902,1905]
  [1905,1906,1904,1904]
  ],
  [/*timestep2*/
  [1904,1905,1904,1906]
  [1906,1907,1904,1907]
  [1907,1908,1902,1905]
  [1905,1906,1904,1904]
  [1904,1906,1902,**1903**]
  ],
  
  </code></pre>
  
  <p>The output simply is the close values from the 6 timesteps to the nexts steps</p>
  
  <p>Example:</p>
  
  <pre><code>/*input*/
  [/*timestep1*/
  /* open, high, low, close*/
  
  [1905,1906,1903,1904]
  [1904,1905,1904,1906]
  [1906,1907,1904,1907]
  [1907,1908,1902,1905]
  [1905,1906,1904,1904]
  ]
  
  /*output*/
  1903 (timestep2 last close) , ...
  
  </code></pre>
  
  <p>What is wrong?</p>
  
  <pre><code>    /* global tf, tfvis */
  
  async function getData() {
  
  // Import from CSV
      const dataSet = tf.data.csv('http://localhost:8888/ts2/eurusd2.csv');
  
  // Extract x and y values to plot
      const pointsDataSet = dataSet.map(record =&gt; ({
              /*date: record["\&lt;DTYYYYMMDD&gt;"]+record["\&lt;TIME&gt;"],*/
              open: record["\&lt;OPEN\&gt;"] * 10000,
              high: record["\&lt;HIGH\&gt;"] * 10000,
              low: record["\&lt;LOW\&gt;"] * 10000,
              close: record["\&lt;CLOSE\&gt;"] * 10000
  
          }));
  
      const points = await pointsDataSet.toArray();
  
      return points;
  }
  
  
  
  function preparaDatiInput(data, time_steps) {
      //es 5 time steps
      //il 6 è la previsione
      if (data.length &gt; time_steps) {
  
          let arr = new Array();
  
          for (let i = 0; i &lt; data.length - time_steps; i++) {
  
              arr.push(data.slice(i, i + 5).map(d =&gt; {
                  return [d.open, d.high, d.low, d.close];
              }));
  
          }
  
          return arr;
      } else
      {
          return false;
      }
  
  }
  
  function preparaDatiOutput(data, time_steps) {
  
      /* l'output è sempre 1*/
      if (data.length &gt; time_steps) {
  
          let arr = new Array();
  
          for (let i = time_steps; i &lt; data.length; i++) {
  
              arr.push(data[i].close);
  
          }
  
          return arr;
  
      } else
      {
          return false;
      }
  }
  
  async function train_data(data) {
  
      const size = 75;
      const time_steps = 5;
  
      const input = preparaDatiInput(data.slice(0, size), time_steps);
      const output = preparaDatiOutput(data.slice(0, size), time_steps);
  
      const testing = preparaDatiInput(data.slice(size), time_steps);
      const risultatiTesting = preparaDatiOutput(data.slice(size), time_steps);
  
      /* primo campo per tensori 3d */
      const trainingData = tf.tensor3d(input, [input.length, input[0].length, input[0][0].length]);
      const outputData = tf.tensor1d(output);
  
  
      const testingData = tf.tensor3d(testing, [testing.length, testing[0].length, testing[0][0].length]);
  
  
      const trainingDataMax = trainingData.max();
      const trainingDataMin = trainingData.min();
  
      const testingDataMax = testingData.min();
      const testingDataMin = testingData.max();
  
      const  outputDataMax = outputData.min();
      const outputDataMin = outputData.max();
  
      const normalizedTrainingData = trainingData.sub(trainingDataMin).div(trainingDataMax.sub(trainingDataMin));
      const normalizedTestingData = testingData.sub(testingDataMin).div(testingDataMax.sub(testingDataMin));
      const normalizedOutputData = outputData.sub(outputDataMin).div(outputDataMax.sub(outputDataMin));
  
      const model = tf.sequential();
      /* time_steps, features */
      model.add(tf.layers.lstm({units: 20, inputShape: [5, 4], returnSequences: false}));
      /* 1 output */
      model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));
  
      model.summary();
  
      const sgdoptimizer = tf.train.adam(0.03);
  
      model.compile({
          optimizer: sgdoptimizer,
          loss: tf.losses.meanSquaredError,
          metrics: ["mse"]
      });
  
  
      console.log('......Loss History.......');
      for (let i = 0; i &lt; 10; i++) {
          let res = await model.fit(normalizedTrainingData, normalizedOutputData, {epochs: 10});
          console.log(`Iteration ${i}: ${res.history.loss[0]}`);
      }
  
      console.log('....Model Prediction .....');
  
  
      const preds = model.predict(normalizedTestingData);
  
      const unNormPreds = preds
              .mul(outputDataMax.sub(outputDataMin))
              .add(outputDataMin).dataSync();
  
      console.log(unNormPreds);
  
      const risultati_veri = risultatiTesting.map((d, i) =&gt; {
          return {
              x: i, y: d
          };
      });
      const previsioni = Array.from(unNormPreds).map((d, i) =&gt; {
          return {
              x: i, y: d
          };
      });
  
      tfvis.render.linechart(
              {name: 'Model Predictions vs Original Data'},
              {values: [risultati_veri, previsioni], series: ['original', 'predicted']},
              {
                  xLabel: 'contatore',
                  yLabel: 'prezzo',
                  height: 300,
                  zoomToFit: true
              }
      );
  
  }
  
  async function main() {
      const data = await getData();
      await train_data(data);
  
  }
  
  main();
  
  </code></pre>
  
  <p>eurusd2.csv example:</p>
  
  <pre><code>&lt;TICKER&gt;,&lt;DTYYYYMMDD&gt;,&lt;TIME&gt;,&lt;OPEN&gt;,&lt;HIGH&gt;,&lt;LOW&gt;,&lt;CLOSE&gt;,&lt;VOL&gt;
  
  EURUSD,20010102,230100,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,230200,0.9506,0.9506,0.9505,0.9505,4
  
  EURUSD,20010102,230300,0.9505,0.9507,0.9505,0.9506,4
  
  EURUSD,20010102,230400,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010102,230500,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010102,230600,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010102,230700,0.9505,0.9507,0.9505,0.9507,4
  
  EURUSD,20010102,230800,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,230900,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231000,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231100,0.9507,0.9507,0.9506,0.9507,4
  
  EURUSD,20010102,231200,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231300,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231400,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231500,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,231600,0.9507,0.9507,0.9506,0.9506,4
  
  EURUSD,20010102,232000,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,232100,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,232200,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,232300,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,232400,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,233000,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,233100,0.9508,0.9508,0.9508,0.9508,4
  
  EURUSD,20010102,233500,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,233600,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,233700,0.9507,0.9508,0.9507,0.9508,4
  
  EURUSD,20010102,233800,0.9509,0.9509,0.9509,0.9509,4
  
  EURUSD,20010102,233900,0.9509,0.9509,0.9509,0.9509,4
  
  EURUSD,20010102,234000,0.9509,0.9509,0.9509,0.9509,4
  
  EURUSD,20010102,234100,0.9508,0.9508,0.9508,0.9508,4
  
  EURUSD,20010102,234400,0.9508,0.9508,0.9508,0.9508,4
  
  EURUSD,20010102,234500,0.9508,0.9508,0.9508,0.9508,4
  
  EURUSD,20010102,234700,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,234900,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235000,0.9507,0.9508,0.9506,0.9506,4
  
  EURUSD,20010102,235100,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010102,235200,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010102,235300,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235400,0.9507,0.9507,0.9506,0.9506,4
  
  EURUSD,20010102,235500,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235600,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235700,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235800,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010102,235900,0.9507,0.9507,0.9506,0.9506,4
  
  EURUSD,20010103,000000,0.9506,0.9507,0.9506,0.9507,4
  
  EURUSD,20010103,000100,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010103,000200,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010103,000300,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010103,000400,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010103,000500,0.9507,0.9507,0.9506,0.9507,4
  
  EURUSD,20010103,000600,0.9507,0.9507,0.9506,0.9506,4
  
  EURUSD,20010103,000700,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,000800,0.9507,0.9507,0.9506,0.9506,4
  
  EURUSD,20010103,000900,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001100,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001200,0.9506,0.9506,0.9505,0.9506,4
  
  EURUSD,20010103,001300,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001400,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001500,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001700,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001800,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,001900,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,002000,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,002100,0.9506,0.9506,0.9506,0.9506,4
  
  EURUSD,20010103,002200,0.9506,0.9507,0.9506,0.9507,4
  
  EURUSD,20010103,002300,0.9507,0.9507,0.9507,0.9507,4
  
  EURUSD,20010103,002400,0.9508,0.9508,0.9507,0.9507,4
  
  EURUSD,20010103,002500,0.9508,0.9510,0.9508,0.9510,4
  
  EURUSD,20010103,002600,0.9510,0.9510,0.9509,0.9509,4
  
  EURUSD,20010103,002700,0.9509,0.9509,0.9509,0.9509,4
  
  EURUSD,20010103,002800,0.9509,0.9509,0.9509,0.9509,4
  
  EURUSD,20010103,002900,0.9508,0.9508,0.9507,0.9507,4
  
  EURUSD,20010103,003000,0.9508,0.9508,0.9507,0.9507,4
  
  EURUSD,20010103,003100,0.9507,0.9507,0.9507,0.9507,4
  </code></pre>