Nixtla / neuralforecast

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

Neural Forecast: Tweedie Distribution Loss Broken (NBEATSx) #1181

Open jrodenberg0 opened 1 month ago

jrodenberg0 commented 1 month ago

What happened + What you expected to happen

Attempting to train NBEATsx with NeuralForecast and Tweedie loss and encounter error claiming that "an output must be a tensor, not a tuple")

Versions / Dependencies

neuralforecast 1.7.5

pytorch-forecasting 1.1.1 pytorch-lightning 2.4.0

Reproducible example

from neuralforecast import NeuralForecast

from neuralforecast.models import NHITS, NBEATSx

from neuralforecast.losses.pytorch import DistributionLoss,Tweedie, MSE,MAE,RMSE

import torch

horizon = 13

rho_ = torch.tensor([1.5])#.to('cuda:0') # attempted to take rho to tensor to solve bug, did not work.

cuda = torch.device('cuda')

rho_ = rho.to(cuda)

tweedie_loss = DistributionLoss(distribution='Tweedie',rho=1.2,validate_args=False,return_params=True)

define models here

models = [NBEATSx(h=horizon,

              input_size = 52,

              #max_steps=200,

             # scaler_type='robust',

              loss= DistributionLoss(distribution='Tweedie',rho=1.2,validate_args=False)

,

              activation='ReLU',

            val_check_steps=10,

              stat_exog_list = ['Client','Warehouse','Product'],

              enable_model_summary=False,

              enable_checkpointing=False,

              logger=False

              #date_features=['week','month']
              ),

  # NHITS(h=horizon,

  #       loss=MAE(),

  #       input_size=104,

  #             stat_exog_list = ['Client','Warehouse','Product'],

  #       )
           ]

nf = NeuralForecast( models=models, freq='W-MON' )

nf_df = df.select('unique_id','Client','Warehouse','Product','ds','y').to_pandas()

nf_df['y'] = nf_df['y'].fillna(0)

dl_cv = nf.cross_validation(df=nf_df,

            id_col='unique_id',

            time_col='ds',

                 n_windows=5,

                 step_size=horizon,

                        static_df = nf_df[['unique_id','Client','Warehouse','Product']])

Issue Severity

High: It blocks me from completing my task.

elephaint commented 1 month ago

Hey, thanks for reporting. I think this issue is in the wrong repo (should be in NeuralForecast).

To answer though, the Tweedie loss is a bit finicky. I think the error is caused by the return_params=True - you could try setting that to False. We've just included a fix on the Tweedie loss on the main branch, but I think that's unrelated to this issue. You could try cloning the main branch of NF and rerunning the script with return_params=False, that should definitely work.

Let me know if that works for you.

jrodenbergrheem commented 1 week ago

Hi @elephaint, i appreciate you reaching out. Unfortunately after specifying return params== True or False I get the same error.

elephaint commented 1 hour ago

Did you update to the latest version of Neuralforecast? I can't reproduce the issue.