Nixtla / neuralforecast

Scalable and user friendly neural :brain: forecasting algorithms.
https://nixtlaverse.nixtla.io/neuralforecast
Apache License 2.0
2.98k stars 342 forks source link

"sequence-to-value" for customised shifted horizon forecasting #903

Closed noahvand closed 5 months ago

noahvand commented 7 months ago

What happened + What you expected to happen

Hi, I'm a beginner on deep learning time-series forcasting and really appreicate about the work on neuralforcast which provide the strong function and comprehensive state of art models for beginner to start with.

Now, I'm tryting to do a sequence-to-value forecasting, where I have hourly based time sequence data. I take input of 24 histroic hour as the historic window, and take single value output of 24th prediction only as the target to forecast.

I noticed there is existing function provided in neuralforecast, which we can define input_size=24 and h=1 (horizon), however this set-up only provides the 1th hour prediction. If I used input_size=24 and h=24 (horizon), then the first 23 hour step is the information I do not need.

Do we have any function support for the described forecasting task now? or we need to do some code modfication on source code? any relevant information is appreciated. Many thanks indeed for help in advance.

Versions / Dependencies

neuralforecast 1.6.4 Python 3.10.12; Windows 10;

Reproduction script

from neuralforecast import NeuralForecast from neuralforecast.models import MLP, NBEATS, NBEATSx, NHITS from neuralforecast.losses.pytorch import MSE, RMSE

horizon = 1 # should be 1 here or ?

Try different hyperparmeters to improve accuracy.

models = [ MLP(h=horizon, input_size = 24*7, batch_size = 16, max_steps=300, scaler_type='minmax', loss= MSE(), valid_loss=MSE(), # early stopping val_check_steps=10, # early stopping early_stop_patience_steps = 5 ),

      NHITS(h=horizon,
           input_size = 24*7,
           batch_size = 16,
           max_steps=300,
           scaler_type='minmax',
           loss= MSE(),
           valid_loss=MSE(), # early stopping
           val_check_steps=10, # early stopping
           early_stop_patience_steps = 5
      ),

      NBEATS(h=horizon,
           input_size = 24*7,
           stack_types=['identity'],
           batch_size = 16,
           max_steps=300,
           scaler_type='minmax',
           loss= MSE(),
           valid_loss=MSE(), # early stopping
           val_check_steps=10, # early stopping
           early_stop_patience_steps = 5
      ),

      NBEATSx(h=horizon,
           input_size = 24*7,
           batch_size = 16,
           stack_types=['identity'],
           max_steps=300,
           scaler_type='minmax',
           loss= MSE(),
           valid_loss=MSE(), # early stopping
           val_check_steps=10, # early stopping
           early_stop_patience_steps = 5
      )
      ]

nf = NeuralForecast(models=models, freq='h') Y_hat_df = nf.cross_validation(df=Y_df, val_size = 299,test_size=299, n_windows=None)

Issue Severity

High: It blocks me from completing my task.

elephaint commented 5 months ago

Hi,

Neuralforecast (at this moment) doesn't support only forecasting the n th value in the horizon at the moment. However, you can achieve this by (i) selecting the n th item in the forecast (as you also state above), and (ii) by adding a horizon weight such that only the n th forecast will be considered in the loss function, such that the model will only give importance to the forecast period of interest.

Example for a horizon of 12 where you are only interested in forecasting for the last day:

horizon_weight = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
loss= MSE(horizon_weight = horizon_weight)
model = NHITS(h=12,
              loss=loss,
              ....
              )

Does this answer your question?

noahvand commented 5 months ago

thanks so much for help. The suggested solution should work!