TorchSpatiotemporal/tsl

Time index as inputs in forward

Closed this issue · 5 comments

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

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

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.

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:

# temporal embeddings
if n_exog_emb is not None:
n_exog_emb = ensure_list(n_exog_emb)
self.exog_embs = nn.ModuleList(
[NodeEmbedding(size, hidden_size) for size in n_exog_emb])
mlp_size += len(n_exog_emb) * hidden_size

and
if u is not None:
assert (self.exog_size is not None and u.dim() <= 3
and u.size(-1) == len(self.exog_size))
if u.dim() == 3:
assert u.size(1) == self.window
u = u[:, -1] # select only last step: b t f -> b 1 f
for u_idx, u_emb in enumerate(self.exog_embs):
t_emb = u_emb(expand=(-1, self.n_nodes, -1),
node_index=u[..., u_idx])
z.append(t_emb)

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

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)}

Thank you for your suggestions. That works very well.