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

make tcn loss only compute on output chunk #2006

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
- 🚀🚀 Optimized `historical_forecasts()` for pre-trained `TorchForecastingModel` running up to 20 times faster than before!. [#2013](https://github.com/unit8co/darts/pull/2013) by [Dennis Bader](https://github.com/dennisbader).
- Added callback `darts.utils.callbacks.TFMProgressBar` to customize at which model stages to display the progress bar. [#2020](https://github.com/unit8co/darts/pull/2020) by [Dennis Bader](https://github.com/dennisbader).
- Improvements to documentation:
- Adapted the example notebooks to properly apply data transformers and avoid look-ahead bias. [#2020](https://github.com/unit8co/darts/pull/2020) by [Samriddhi Singh](https://github.com/SimTheGreat).
- Adapted the example notebooks to properly apply data transformers and avoid look-ahead bias. [#2020](https://github.com/unit8co/darts/pull/2020) by [Samriddhi Singh](https://github.com/SimTheGreat).
- Improvements to Regression Models:
- `XGBModel` now leverages XGBoost's native Quantile Regression support that was released in version 2.0.0 for improved probabilistic forecasts. [#2051](https://github.com/unit8co/darts/pull/2051) by [Dennis Bader](https://github.com/dennisbader).
- Other improvements:
Expand All @@ -24,6 +24,7 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
**Fixed**
- Fixed a bug when calling optimized `historical_forecasts()` for a `RegressionModel` trained with unequal component-specific lags. [#2040](https://github.com/unit8co/darts/pull/2040) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed a bug when using encoders with `RegressionModel` and series with a non-evenly spaced frequency (e.g. Month Begin). This raised an error during lagged data creation when trying to divide a pd.Timedelta by the ambiguous frequency. [#2034](https://github.com/unit8co/darts/pull/2034) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed an issue where `TCNModel` training included the last (input_chunk_length - output_chunk_length) target points in the loss computation. [#2006](https://github.com/unit8co/darts/pull/2006) by [Dennis Bader](https://github.com/dennisbader).

### For developers of the library:

Expand Down
21 changes: 4 additions & 17 deletions darts/models/forecasting/pl_forecasting_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,6 @@ def __init__(
self.pred_n_jobs: Optional[int] = None
self.predict_likelihood_parameters: Optional[bool] = None

@property
def first_prediction_index(self) -> int:
"""
Returns the index of the first predicted within the output of self.model.
"""
return 0

@abstractmethod
def forward(self, *args, **kwargs) -> Any:
super().forward(*args, **kwargs)
Expand Down Expand Up @@ -600,9 +593,7 @@ def _get_batch_prediction(
dim=dim_component,
)

out = self._produce_predict_output(x=(input_past, static_covariates))[
:, self.first_prediction_index :, :
]
out = self._produce_predict_output(x=(input_past, static_covariates))

batch_prediction = [out[:, :roll_size, :]]
prediction_length = roll_size
Expand Down Expand Up @@ -649,9 +640,7 @@ def _get_batch_prediction(
] = future_past_covariates[:, left_past:right_past, :]

# take only last part of the output sequence where needed
out = self._produce_predict_output(x=(input_past, static_covariates))[
:, self.first_prediction_index :, :
]
out = self._produce_predict_output(x=(input_past, static_covariates))

batch_prediction.append(out)
prediction_length += self.output_chunk_length
Expand Down Expand Up @@ -783,9 +772,7 @@ def _get_batch_prediction(
)
)

out = self._produce_predict_output(x=(input_past, input_future, input_static))[
:, self.first_prediction_index :, :
]
out = self._produce_predict_output(x=(input_past, input_future, input_static))

batch_prediction = [out[:, :roll_size, :]]
prediction_length = roll_size
Expand Down Expand Up @@ -853,7 +840,7 @@ def _get_batch_prediction(
# take only last part of the output sequence where needed
out = self._produce_predict_output(
x=(input_past, input_future, input_static)
)[:, self.first_prediction_index :, :]
)

batch_prediction.append(out)
prediction_length += self.output_chunk_length
Expand Down
39 changes: 4 additions & 35 deletions darts/models/forecasting/tcn_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import math
from typing import Optional, Sequence, Tuple
from typing import Optional, Tuple

import torch
import torch.nn as nn
Expand All @@ -16,8 +16,6 @@
io_processor,
)
from darts.models.forecasting.torch_forecasting_model import PastCovariatesTorchModel
from darts.timeseries import TimeSeries
from darts.utils.data import PastCovariatesShiftedDataset
from darts.utils.torch import MonteCarloDropout

logger = get_logger(__name__)
Expand Down Expand Up @@ -139,14 +137,12 @@ def __init__(
weight_norm: bool,
target_size: int,
nr_params: int,
target_length: int,
dropout: float,
**kwargs
):

"""PyTorch module implementing a dilated TCN module used in `TCNModel`.


Parameters
----------
input_size
Expand All @@ -155,8 +151,6 @@ def __init__(
The dimensionality of the output time series.
nr_params
The number of parameters of the likelihood (or 1 if no likelihood is used).
target_length
Number of time steps the torch module will predict into the future at once.
kernel_size
The size of every kernel in a convolutional layer.
num_filters
Expand All @@ -179,10 +173,8 @@ def __init__(

Outputs
-------
y of shape `(batch_size, input_chunk_length, target_size, nr_params)`
Tensor containing the predictions of the next 'output_chunk_length' points in the last
'output_chunk_length' entries of the tensor. The entries before contain the data points
leading up to the first prediction, all in chronological order.
y of shape `(batch_size, output_chunk_length, target_size, nr_params)`
Tensor containing the predictions of the next 'output_chunk_length' points.
"""

super().__init__(**kwargs)
Expand All @@ -191,7 +183,6 @@ def __init__(
self.input_size = input_size
self.n_filters = num_filters
self.kernel_size = kernel_size
self.target_length = target_length
self.target_size = target_size
self.nr_params = nr_params
self.dilation_base = dilation_base
Expand Down Expand Up @@ -249,11 +240,7 @@ def forward(self, x_in: Tuple):
batch_size, self.input_chunk_length, self.target_size, self.nr_params
)

return x

@property
def first_prediction_index(self) -> int:
return -self.output_chunk_length
return x[:, -self.output_chunk_length :, :, :]


class TCNModel(PastCovariatesTorchModel):
Expand Down Expand Up @@ -511,25 +498,7 @@ def _create_model(self, train_sample: Tuple[torch.Tensor]) -> torch.nn.Module:
num_filters=self.num_filters,
num_layers=self.num_layers,
dilation_base=self.dilation_base,
target_length=self.output_chunk_length,
dropout=self.dropout,
weight_norm=self.weight_norm,
**self.pl_module_params,
)

def _build_train_dataset(
self,
target: Sequence[TimeSeries],
past_covariates: Optional[Sequence[TimeSeries]],
future_covariates: Optional[Sequence[TimeSeries]],
max_samples_per_ts: Optional[int],
) -> PastCovariatesShiftedDataset:

return PastCovariatesShiftedDataset(
target_series=target,
covariates=past_covariates,
length=self.input_chunk_length,
shift=self.output_chunk_length,
max_samples_per_ts=max_samples_per_ts,
use_static_covariates=self.uses_static_covariates,
)
7 changes: 0 additions & 7 deletions darts/models/forecasting/torch_forecasting_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,13 +1505,6 @@ def predict_from_dataset(
# flatten and return
return [ts for batch in predictions for ts in batch]

@property
def first_prediction_index(self) -> int:
"""
Returns the index of the first predicted within the output of self.model.
"""
return 0

@property
def min_train_series_length(self) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
+ "PastCovariatesTorchModel,FutureCovariatesTorchModel,DualCovariatesTorchModel,MixedCovariatesTorchModel,"
+ "SplitCovariatesTorchModel,TorchParametricProbabilisticForecastingModel,"
+ "min_train_series_length,"
+ "untrained_model,first_prediction_index,future_covariate_series,past_covariate_series,"
+ "untrained_model,future_covariate_series,past_covariate_series,"
+ "initialize_encoders,register_datapipe_as_function,register_function,functions,"
+ "SplitTimeSeriesSequence,randint,AnomalyModel",
}
Expand Down