TorchSpatiotemporal / tsl

tsl: a PyTorch library for processing spatiotemporal data.
https://torch-spatiotemporal.readthedocs.io/
MIT License
259 stars 24 forks source link

Time index as inputs in forward #48

Open Viet1004 opened 21 hours ago

Viet1004 commented 21 hours ago

Hello. I'm doing load forecasting, and some algorithms suggest using day and hour indexes as input variables for models since periodicity is quite essential.

I must change the method __getitem__ in the SpatioTemporalDataset class since the return should include the day and hour index.

Do you have any other suggestions for solving this problem? If not, would you be open to including this feature?

Thank you very much.

Best regards,

Viet

andreacini commented 21 hours ago

Hi. You can include any covariate you want in a batch. Checkout, eg, the traffic forecasting example at line 108.

Viet1004 commented 14 hours ago

Hi. Thank you a lot for your suggestion. I intend to use time index to learn the temporal representation similar to Node Embedding (lookup table). Would like to test that and come back if there is any problem.

marshka commented 36 minutes ago

Hi Viet, in our implementation of the STID model we do something similar to what you're looking for. I suggest you check out lines: https://github.com/TorchSpatiotemporal/tsl/blob/f9e5081d198cc4e568a41b0ba472fce0f603c61b/tsl/nn/models/temporal/stid_model.py#L62-L67 and https://github.com/TorchSpatiotemporal/tsl/blob/f9e5081d198cc4e568a41b0ba472fce0f603c61b/tsl/nn/models/temporal/stid_model.py#L130-L139

In this case, u is the one-hot encoding of the temporal variables.

marshka commented 25 minutes ago

I apologize, actually an element in u is a scalar, i.e., the index of the corresponding temporal element. Here's an example code to obtain it directly from a tsl.datasets.DatetimeDataset with the method datetime_idx:

  # encode time of the day and use it as exogenous variable
  day_enc = dataset.datetime_idx('weekday').values
  slot_enc = dataset.datetime_idx(['hour', 'minute']).values
  slot_enc[:, 0] *= (slot_enc[:, 1].max() + 1)
  slot_enc = slot_enc.sum(1, keepdims=True)
  covariates = {'u': np.concatenate([day_enc, slot_enc], -1)}