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

Use observation mask as feature in DeepAR #2892

Open
wants to merge 4 commits into
base: dev
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
54 changes: 49 additions & 5 deletions src/gluonts/torch/model/deepar/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def __init__(
)
else:
self.scaler = NOPScaler(dim=-1, keepdim=True)
self.rnn_input_size = len(self.lags_seq) + self._number_of_features
self.rnn_input_size = (
2 * len(self.lags_seq)
) + self._number_of_features
self.rnn = nn.LSTM(
input_size=self.rnn_input_size,
hidden_size=hidden_size,
Expand Down Expand Up @@ -216,23 +218,41 @@ def prepare_rnn_input(
past_observed_values: torch.Tensor,
future_time_feat: torch.Tensor,
future_target: Optional[torch.Tensor] = None,
future_observed_values: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor,]:
context = past_target[..., -self.context_length :]
observed_context = past_observed_values[..., -self.context_length :]

input, _, scale = self.scaler(context, observed_context)
observed_input = observed_context
future_length = future_time_feat.shape[-2]
if future_length > 1:
assert future_target is not None
assert (
future_target is not None
and future_observed_values is not None
)
input = torch.cat(
(input, future_target[..., : future_length - 1] / scale),
dim=-1,
)
observed_input = torch.cat(
(
observed_input,
future_observed_values[..., : future_length - 1],
),
dim=-1,
)
prior_input = past_target[..., : -self.context_length] / scale
observed_prior_input = past_observed_values[
..., : -self.context_length
]

lags = lagged_sequence_values(
self.lags_seq, prior_input, input, dim=-1
)
observed_lags = lagged_sequence_values(
self.lags_seq, observed_prior_input, observed_input, dim=-1
)

time_feat = torch.cat(
(
Expand All @@ -252,8 +272,8 @@ def prepare_rnn_input(
)

features = torch.cat((expanded_static_feat, time_feat), dim=-1)

return torch.cat((lags, features), dim=-1), scale, static_feat
rnn_input = torch.cat((lags, observed_lags, features), dim=-1)
return (rnn_input, scale, static_feat)

def unroll_lagged_rnn(
self,
Expand All @@ -264,6 +284,7 @@ def unroll_lagged_rnn(
past_observed_values: torch.Tensor,
future_time_feat: torch.Tensor,
future_target: Optional[torch.Tensor] = None,
future_observed_values: Optional[torch.Tensor] = None,
) -> Tuple[
Tuple[torch.Tensor, ...],
torch.Tensor,
Expand Down Expand Up @@ -297,6 +318,9 @@ def unroll_lagged_rnn(
future_target
(Optional) tensor of future target values,
shape: ``(batch_size, prediction_length)``.
future_observed_values
(Optional) tensor of future observed values indicators,
shape: ``(batch_size, prediction_length)``.

Returns
-------
Expand All @@ -316,6 +340,7 @@ def unroll_lagged_rnn(
past_observed_values,
future_time_feat,
future_target,
future_observed_values,
)

output, new_state = self.rnn(rnn_input)
Expand Down Expand Up @@ -409,6 +434,9 @@ def forward(
past_target.repeat_interleave(repeats=num_parallel_samples, dim=0)
/ repeated_scale
)
repeated_past_observed_values = past_observed_values.repeat_interleave(
repeats=num_parallel_samples, dim=0
)
repeated_time_feat = future_time_feat.repeat_interleave(
repeats=num_parallel_samples, dim=0
)
Expand Down Expand Up @@ -436,13 +464,28 @@ def forward(
next_lags = lagged_sequence_values(
self.lags_seq, repeated_past_target, scaled_next_sample, dim=-1
)
rnn_input = torch.cat((next_lags, next_features), dim=-1)
next_observed_lags = lagged_sequence_values(
self.lags_seq,
repeated_past_observed_values,
torch.ones_like(scaled_next_sample),
dim=-1,
)
rnn_input = torch.cat(
(next_lags, next_observed_lags, next_features), dim=-1
)

output, repeated_state = self.rnn(rnn_input, repeated_state)

repeated_past_target = torch.cat(
(repeated_past_target, scaled_next_sample), dim=1
)
repeated_past_observed_values = torch.cat(
(
repeated_past_observed_values,
torch.ones_like(scaled_next_sample),
),
dim=1,
)

params = self.param_proj(output)
distr = self.output_distribution(params, scale=repeated_scale)
Expand Down Expand Up @@ -524,6 +567,7 @@ def loss(
past_observed_values,
future_time_feat,
future_target_reshaped,
future_observed_reshaped,
)

if future_only:
Expand Down
2 changes: 2 additions & 0 deletions src/gluonts/torch/model/mqf2/lightning_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def _compute_loss(self, batch: Dict[str, torch.Tensor]) -> torch.Tensor:
future_time_feat = batch["future_time_feat"]
future_target = batch["future_target"]
past_observed_values = batch["past_observed_values"]
future_observed_values = batch["future_observed_values"]

picnn = self.model.picnn

Expand All @@ -107,6 +108,7 @@ def _compute_loss(self, batch: Dict[str, torch.Tensor]) -> torch.Tensor:
past_observed_values,
future_time_feat,
future_target,
future_observed_values,
)

hidden_state = hidden_state[:, : self.model.context_length]
Expand Down
6 changes: 6 additions & 0 deletions test/torch/model/test_deepar_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_deepar_modules(
past_observed_values,
future_time_feat,
future_target,
future_observed_values,
)

assert scale.shape == (batch_size, 1)
Expand Down Expand Up @@ -231,6 +232,11 @@ def test_rnn_input(
dtype=torch.float32,
).view(1, prediction_length)

batch["future_observed_values"] = torch.ones(
(1, prediction_length),
dtype=torch.float32,
)

rnn_input, scale, _ = model.prepare_rnn_input(**batch)

assert (scale == 1.0).all()
Expand Down
1 change: 1 addition & 0 deletions test/torch/model/test_mqf2_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_mqf2_modules(
past_observed_values,
future_time_feat,
future_target,
future_observed_values,
)

hidden_state = hidden_state[:, :context_length]
Expand Down