TorchSpatiotemporal / tsl

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

Add future covariates #33

Closed antonelse closed 7 months ago

antonelse commented 7 months ago

Hi everyone :) I am having difficulty understanding whether it is possible and if so how to include future covariates, such as weather forecasts. I would need to include this information in the training, referring to the time horizon of the target

What I am doing is something like this:

` dataset = AirQuality(small=True, impute_nans=True)

df = dataset.dataframe()

fake_past_cov_df = pd.DataFrame(np.random.randn(df.shape), columns=df.columns, index=df.index) fake_fut_cov_df = pd.DataFrame(np.random.randn(df.shape), columns=df.columns, index=df.index) day_sin_cos = dataset.datetime_encoded('day').values weekdays = dataset.datetime_onehot('weekday').values

torch_dataset = SpatioTemporalDataset(target=dataset.dataframe(), mask=dataset.mask, horizon=12, window=12, stride=1, precision=16, name='AQI')

torch_dataset.add_covariate(name='fake_past_cov', value=fake_past_cov_df, add_to_input_map=True, synch_mode=SynchMode.WINDOW, pattern='tnf') torch_dataset.add_covariate(name='fake_fut_cov', value=fake_fut_cov_df, add_to_input_map=False, synch_mode=SynchMode.HORIZON, pattern='tnf') torch_dataset.add_exogenous(name='global_u', value=np.concatenate([day_sin_cos, weekdays], axis=-1), add_to_input_map=True, synch_mode=SynchMode.WINDOW) `

But when training for example an AGCRNModel, i receive this: Sanity Checking DataLoader 0: 0%| | 0/2 [00:00<?, ?it/s]Arguments ['fake_past_cov'] are filtered out. Only args ['u', 'x'] are forwarded to the model (AGCRNModel). Anyone knows how to overcome this problem, for both past and future covariates ? Unfortunately, I couldn't find anything about it in the documentation.

Any help is appreciated ;) Thanks

steve3nto commented 7 months ago

Hello, The problem here is that the input_map needs to match the args in the the forward method of the model you are using. This is how the forward method of AGCRNModel is defined:

def forward(self, x: Tensor, u: OptTensor = None) -> Tensor: 

If you want to use AGCRNModel without modifications, you should have only x and u in your input_map . A better approach would be to define your own custom model with more arguments in the forward method, to handle more covariates separately.

antonelse commented 7 months ago

Ahh, I got it :) I thought this feature was already implemented under the hood. I apologize for the trivial question :)

Thanks a lot.