sktime / pytorch-forecasting

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

plot_prediction_actual_by_variable with None scalers #1107

Open romanzes637 opened 2 years ago

romanzes637 commented 2 years ago

Expected behavior

I executed code like in the tutorial Demand forecasting with the Temporal Fusion Transformer -> Actuals vs predictions by variables

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

with None-scalers for several variables

dataset = TimeSeriesDataSet(
    ...
    scalers: {"some_variable": None, ...},
    ...
)

Actual behavior

However, result was

Traceback (most recent call last):
  File "/home/bra/miniconda3/envs/ccpred/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/bra/miniconda3/envs/ccpred/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/bra/ccpred/src/ccpred/__main__.py", line 19, in <module>
    c(**kwargs)
  File "/home/bra/ccpred/src/ccpred/test.py", line 91, in test
    plots = m.plot_prediction_actual_by_variable(variables)
  File "/home/bra/miniconda3/envs/ccpred/lib/python3.9/site-packages/pytorch_forecasting/models/base_model.py", line 1666, in plot_prediction_actual_by_variable
    figs = {name: self.plot_prediction_actual_by_variable(data, name) for name in data["support"].keys()}
  File "/home/bra/miniconda3/envs/ccpred/lib/python3.9/site-packages/pytorch_forecasting/models/base_model.py", line 1666, in <dictcomp>
    figs = {name: self.plot_prediction_actual_by_variable(data, name) for name in data["support"].keys()}
  File "/home/bra/miniconda3/envs/ccpred/lib/python3.9/site-packages/pytorch_forecasting/models/base_model.py", line 1718, in plot_prediction_actual_by_variable
    x = scaler.inverse_transform(x.reshape(-1, 1)).reshape(-1)
AttributeError: 'NoneType' object has no attribute 'inverse_transform'

I worked around this issue by adding "None-Check" in base_model.py Does not work:

if not isinstance(scaler, (GroupNormalizer, EncoderNormalizer)):
    x = scaler.inverse_transform(x.reshape(-1, 1)).reshape(-1)
    ax.set_xlabel(f"Normalized {name}")

Works:

if not isinstance(scaler, (GroupNormalizer, EncoderNormalizer)):
    if scaler is not None:
        x = scaler.inverse_transform(x.reshape(-1, 1)).reshape(-1)
    ax.set_xlabel(f"Normalized {name}")

Maybe I am doing something wrong?

sairamtvv commented 2 years ago

Added it in local code. Facing same issue

Nephdz commented 1 year ago

Same thing for me. Fixed by adding in the extra check. @jdb78