mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.05k stars 2.5k forks source link

Notebook 10: Section 350 function for making future preds #587

Open talha-0 opened 10 months ago

talha-0 commented 10 months ago

We are not preparing the data correctly as the latest price must be at the 0th index of the list. Model's forecast are also effected by this we can see the forecast was very high from the last price this was because the 0th index has a higher value than the actual price Here are my observations: X_all[-2:] Output:

array([[47885.62525472, 50032.69313676, 49764.1320816 , 52147.82118698,
        56573.5554719 , 55715.54665129, 58102.19142623],
       [45604.61575361, 47885.62525472, 50032.69313676, 49764.1320816 ,
        52147.82118698, 56573.5554719 , 55715.54665129]])

See how the new price 45604.61575361 moved to the 0th index

SOLUTION:

new = y_all[-WINDOW_SIZE:]
new[::-1]
array([43144.47129086, 45604.61575361, 47885.62525472, 50032.69313676,
       49764.1320816 , 52147.82118698, 56573.5554719 ])

This will correct the order of windows New function:

def make_future_forecasts(
    values,
    model,
    into_future,
    window_size=WINDOW_SIZE
) -> list:
  """
  Make future forecasts into_future steps after value ends.

  Returns future forecasts as a list of floats.
  """
  # Empty list of future forecast
  future_forecast = []
  # Get the last window of prices to feed to the model
  last_window = values[-window_size:]
  last_window = last_window[::-1]

  # Make into_future number of preds altering the data which model will use for next pred
  for _ in range(into_future):
    # Predict on last window and append the pred in forecast and also in last window(model will predict on its own pred)
    future_pred = model.predict(tf.expand_dims(last_window,axis=0))
    print(f'Predicting on:\n {last_window}-> Prediction: {tf.squeeze(future_pred).numpy()}')
    future_forecast.append(tf.squeeze(future_pred).numpy())
    # Update last window
    last_window=np.append(future_pred,last_window)[:window_size]

  # Return list of forecast
  return future_forecast