sktime / pytorch-forecasting

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

QuantileLoss in DeepAR #277

Open qiweihan opened 3 years ago

qiweihan commented 3 years ago

Expected behavior

I executed code from the tutorial to work with the DeepAR model and tried to use the QuantileLoss as the loss function.

Actual behavior

However, the result was not as expected as QuantileLoss is not callable in DeepAR as it shows in the TFT model.

Code to reproduce the problem

https://colab.research.google.com/drive/1VayO_6_rvB24IzI6t1XNnvm9XUixzZXI?usp=sharing

The traceback is as below:


AssertionError Traceback (most recent call last)

in () 19 #hidden_continuous_size=8, # set to <= hidden_size 20 #output_size=7, # 7 quantiles by default ---> 21 loss=QuantileLoss(), 22 # reduce learning rate if no improvement in validation loss after x epochs 23 #reduce_on_plateau_patience=4, 4 frames /usr/local/lib/python3.6/dist-packages/pytorch_forecasting/models/deepar/__init__.py in __init__(self, cell_type, hidden_size, rnn_layers, dropout, static_categoricals, static_reals, time_varying_categoricals_encoder, time_varying_categoricals_decoder, categorical_groups, time_varying_reals_encoder, time_varying_reals_decoder, embedding_sizes, embedding_paddings, embedding_labels, x_reals, x_categoricals, n_validation_samples, n_plotting_samples, target, target_lags, loss, logging_metrics, **kwargs) 130 assert set(self.encoder_variables) - set(to_list(target)) - set(lagged_target_names) == set( 131 self.decoder_variables --> 132 ), "Encoder and decoder variables have to be the same apart from target variable" 133 for targeti in to_list(target): 134 assert ( AssertionError: Encoder and decoder variables have to be the same apart from target variable
jdb78 commented 3 years ago

DeepAR only works with DistributionLoss by design. It is not possible to use quantile regression with it.

Long story: You could potentially engineer an empirical distribution and internally work with quantile loss it requires a lot of assumptions.

StatMixedML commented 3 years ago

@jdb78 You might be interested in implementing Probabilistic Forecasting with Spline Quantile Function RNNs. This is also implemented here