Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Bugfix when no exogenous variable is passed to TFT #1667

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ def forward(self, x: Dict[str, torch.Tensor], context: torch.Tensor = None):

outputs = var_outputs * sparse_weights
outputs = outputs.sum(dim=-1)
else: # for one input, do not perform variable selection but just encoding
elif self.num_inputs == 1:
# for one input, do not perform variable selection but just encoding
name = next(iter(self.single_variable_grns.keys()))
variable_embedding = x[name]
if name in self.prescalers:
Expand All @@ -346,6 +347,12 @@ def forward(self, x: Dict[str, torch.Tensor], context: torch.Tensor = None):
sparse_weights = torch.ones(outputs.size(0), outputs.size(1), 1, 1, device=outputs.device) #
else: # ndim == 2 -> batch size, hidden size, n_variables
sparse_weights = torch.ones(outputs.size(0), 1, 1, device=outputs.device)
else: # for no input
outputs = torch.zeros(context.size(), device=context.device)
if outputs.ndim == 3: # -> batch size, time, hidden size, n_variables
sparse_weights = torch.zeros(outputs.size(0), outputs.size(1), 1, 0, device=outputs.device)
else: # ndim == 2 -> batch size, hidden size, n_variables
sparse_weights = torch.zeros(outputs.size(0), 1, 0, device=outputs.device)
return outputs, sparse_weights


Expand Down
48 changes: 48 additions & 0 deletions tests/test_models/test_temporal_fusion_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lightning.pytorch.callbacks import EarlyStopping
from lightning.pytorch.loggers import TensorBoardLogger
import numpy as np
import pandas as pd
import pytest
import torch

Expand Down Expand Up @@ -421,3 +422,50 @@ def test_hyperparameter_optimization_integration(dataloaders_with_covariates, tm
)
finally:
shutil.rmtree(tmp_path, ignore_errors=True)


def test_no_exogenous_variable():
data = pd.DataFrame(
{
"target": np.ones(1600),
"group_id": np.repeat(np.arange(16), 100),
"time_idx": np.tile(np.arange(100), 16),
}
)
training_dataset = TimeSeriesDataSet(
data=data,
time_idx="time_idx",
target="target",
group_ids=["group_id"],
max_encoder_length=10,
max_prediction_length=5,
time_varying_unknown_reals=["target"],
time_varying_known_reals=[],
)
validation_dataset = TimeSeriesDataSet.from_dataset(training_dataset, data, stop_randomization=True, predict=True)
training_data_loader = training_dataset.to_dataloader(train=True, batch_size=8, num_workers=0)
validation_data_loader = validation_dataset.to_dataloader(train=False, batch_size=8, num_workers=0)
forecaster = TemporalFusionTransformer.from_dataset(
training_dataset,
log_interval=1,
)
from lightning.pytorch import Trainer

trainer = Trainer(
max_epochs=2,
limit_train_batches=8,
limit_val_batches=8,
)
trainer.fit(
forecaster,
train_dataloaders=training_data_loader,
val_dataloaders=validation_data_loader,
)
best_model_path = trainer.checkpoint_callback.best_model_path
best_model = TemporalFusionTransformer.load_from_checkpoint(best_model_path)
best_model.predict(
validation_data_loader,
return_x=True,
return_y=True,
return_index=True,
)
Loading