sktime / pytorch-forecasting

Time series forecasting with PyTorch
https://pytorch-forecasting.readthedocs.io/
MIT License
3.99k stars 631 forks source link

KeyError when plotting predictions by variable for TFT with added lags #1299

Open jzicker opened 1 year ago

jzicker commented 1 year ago

Expected behavior

I added lags to my timeseriesdataset and added the variables to time_varying_unknown training = TimeSeriesDataSet( data[lambda x: x.weeks_from_start <= training_cutoff], time_idx="weeks_from_start", target="covid_trend", lags={"covid_trend":[1,2,3,4]}, group_ids=["metro_id","region"], min_encoder_length=max_encoder_length, max_encoder_length=max_encoder_length, min_prediction_length=1, max_prediction_length=max_prediction_length, static_categoricals=["region","metro_id",'IECC Climate Zone', 'IECC Moisture Regime'], time_varying_known_categoricals=["quarter",'month', "week_num","year"], static_reals = ["population","population_density","income", "white_pcnt", "asian_pcnt","latino_pcnt","african_american_pcnt", "native_american_pcnt","dim_1","dim_2"], time_varying_known_reals=["weeks_from_start"], time_varying_unknown_reals=["covid_trend", "covid_trend_lagged_by_1", "covid_trend_lagged_by_2", "covid_trend_lagged_by_3", "covid_trend_lagged_by_4", "fever_trend", "cough_trend", "sore_throat_trend", "trouble_breathing_trend", "taste_or_smell_trend", "vomiting_trend","diarrhea_trend","headache_trend", ], target_normalizer=GroupNormalizer( groups=["metro_id","region"], transformation="softplus" ), # we normalize by group add_relative_time_idx=True

add_target_scales=True

# allow_missing_timesteps=True

)

Analysis by variable

predictions = best_tft.predict(val_dataloader, return_x=True) predictions_vs_actuals = best_tft.calculate_prediction_actual_by_variable(predictions.x, predictions.output) best_tft.plot_prediction_actual_by_variable(predictions_vs_actuals)

Actual behavior

and got this error which resulted in not producing all of the variable plots KeyError Traceback (most recent call last) in <cell line: 4>() 2 predictions = best_tft.predict(val_dataloader, return_x=True) 3 predictions_vs_actuals = best_tft.calculate_prediction_actual_by_variable(predictions.x, predictions.output) ----> 4 best_tft.plot_prediction_actual_by_variable(predictions_vs_actuals)

2 frames /usr/local/lib/python3.10/dist-packages/pytorch_forecasting/models/base_model.py in plot_prediction_actual_by_variable(self, data, name, ax, log_scale) 1853 """ 1854 if name is None: # run recursion for figures -> 1855 figs = {name: self.plot_prediction_actual_by_variable(data, name) for name in data["support"].keys()} 1856 return figs 1857 else:

/usr/local/lib/python3.10/dist-packages/pytorch_forecasting/models/base_model.py in (.0) 1853 """ 1854 if name is None: # run recursion for figures -> 1855 figs = {name: self.plot_prediction_actual_by_variable(data, name) for name in data["support"].keys()} 1856 return figs 1857 else:

/usr/local/lib/python3.10/dist-packages/pytorch_forecasting/models/base_model.py in plot_prediction_actual_by_variable(self, data, name, ax, log_scale) 1900 scaler = self.output_transformer 1901 else: -> 1902 scaler = self.dataset_parameters["scalers"][name] 1903 x = np.linspace(-data["std"], data["std"], bins) 1904 # reversing normalization for group normalizer is not possible without sample level information

KeyError: 'covid_trend_lagged_by_1'

Trudie commented 4 months ago

you have to add your lag in x_real what I did for DeepAR is :

model_params = {
    "cell_type": 
    "hidden_size": 20, 
    "rnn_layers": 3, 
    "dropout":.05, 
    "learning_rate": 0.005,
    'loss': BetaDistributionLoss(),
    "target_lags": {'sales':{'monthly':4,'quarterly':12,'yearly':52}},
    'x_reals':['sales', 'monthly','quarterly','yearly']
}

DeepAR.from_dataset(training, **model_params)