diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a68470899..dfde763ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: @@ -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: diff --git a/darts/models/forecasting/pl_forecasting_module.py b/darts/models/forecasting/pl_forecasting_module.py index 79c1902a81..565031c85b 100644 --- a/darts/models/forecasting/pl_forecasting_module.py +++ b/darts/models/forecasting/pl_forecasting_module.py @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/darts/models/forecasting/tcn_model.py b/darts/models/forecasting/tcn_model.py index 076fd939df..91a9180425 100644 --- a/darts/models/forecasting/tcn_model.py +++ b/darts/models/forecasting/tcn_model.py @@ -4,7 +4,7 @@ """ import math -from typing import Optional, Sequence, Tuple +from typing import Optional, Tuple import torch import torch.nn as nn @@ -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__) @@ -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 @@ -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 @@ -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) @@ -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 @@ -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): @@ -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, - ) diff --git a/darts/models/forecasting/torch_forecasting_model.py b/darts/models/forecasting/torch_forecasting_model.py index 8312808689..18617615e3 100644 --- a/darts/models/forecasting/torch_forecasting_model.py +++ b/darts/models/forecasting/torch_forecasting_model.py @@ -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: """ diff --git a/docs/source/conf.py b/docs/source/conf.py index dd4017a787..45ea057f1a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -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", }