Skip to content

Commit

Permalink
Merge branch 'master' into feature/pl_auto_batch_size
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisbader authored Apr 12, 2024
2 parents 35d1d58 + c3a6112 commit eecfd0f
Show file tree
Hide file tree
Showing 6 changed files with 1,572 additions and 734 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
- Moved functions `retain_period_common_to_all()`, `series2seq()`, `seq2series()`, `get_single_series()` from `darts.utils.utils` to `darts.utils.ts_utils`.
- Improvements to `ForecastingModel`: [#2269](https://github.com/unit8co/darts/pull/2269) by [Felix Divo](https://github.com/felixdivo).
- Renamed the private `_is_probabilistic` property to a public `supports_probabilistic_prediction`.
- Improvements to `RegressionModel`: [#2320](https://github.com/unit8co/darts/pull/2320) by [Felix Divo](https://github.com/felixdivo).
- Added a progress bar when performing optimized historical forecasts (`retrain=False` and no autoregression) to display the series-level progress.
- Improvements to `DataTransformer`: [#2267](https://github.com/unit8co/darts/pull/2267) by [Alicja Krzeminska-Sciga](https://github.com/alicjakrzeminska).
- `InvertibleDataTransformer` now supports parallelized inverse transformation for `series` being a list of lists of `TimeSeries` (`Sequence[Sequence[TimeSeries]]`). This `series` type represents for example the output from `historical_forecasts()` when using multiple series.
- New method `TorchForecastingModel.scale_batch_size()` that helps to find batch size automatically. [#2318](https://github.com/unit8co/darts/pull/2318) by [Bohdan Bilonoh](https://github.com/BohdanBilonoh)

**Fixed**
- Fixed a bug in `quantile_loss`, where the loss was computed on all samples rather than only on the predicted quantiles. [#2284](https://github.com/unit8co/darts/pull/2284) by [Dennis Bader](https://github.com/dennisbader).
- Fixed type hint warning "Unexpected argument" when calling `historical_forecasts()` caused by the `_with_sanity_checks` decorator. The type hinting is now properly configured to expect any input arguments and return the output type of the method for which the sanity checks are performed for. [#2286](https://github.com/unit8co/darts/pull/2286) by [Dennis Bader](https://github.com/dennisbader).
- Fixed the order of the features when using component-wise lags so that they are grouped by values, then by components (before, were grouped by components, then by values). [#2272](https://github.com/unit8co/darts/pull/2272) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed a segmentation fault that some users were facing when importing a `LightGBMModel`. [#2304](https://github.com/unit8co/darts/pull/2304) by [Dennis Bader](https://github.com/dennisbader).
- Fixed a bug when using a dropout with a `TorchForecasting` and pytorch lightning versions >= 2.2.0, where the dropout was not properly activated during training. [#2312](https://github.com/unit8co/darts/pull/2312) by [Dennis Bader](https://github.com/dennisbader).

Expand Down
96 changes: 20 additions & 76 deletions darts/models/forecasting/regression_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from darts.models.forecasting.forecasting_model import GlobalForecastingModel
from darts.timeseries import TimeSeries
from darts.utils.data.tabularization import (
add_static_covariates_to_lagged_data,
_create_lagged_data_autoregression,
create_lagged_component_names,
create_lagged_training_data,
)
Expand Down Expand Up @@ -1019,83 +1019,25 @@ def predict(
last_step_shift = t_pred - (n - step)
t_pred = n - step

np_X = []
# retrieve target lags
if "target" in self.lags:
if predictions:
series_matrix = np.concatenate(
[series_matrix, predictions[-1]], axis=1
)
# component-wise lags
if "target" in self.component_lags:
tmp_X = [
series_matrix[
:,
[lag - (shift + last_step_shift) for lag in comp_lags],
comp_i,
]
for comp_i, (comp, comp_lags) in enumerate(
self.component_lags["target"].items()
)
]
# values are grouped by component
np_X.append(
np.concatenate(tmp_X, axis=1).reshape(
len(series) * num_samples, -1
)
)
else:
# values are grouped by lags
np_X.append(
series_matrix[
:,
[
lag - (shift + last_step_shift)
for lag in self.lags["target"]
],
].reshape(len(series) * num_samples, -1)
)
# retrieve covariate lags, enforce order (dict only preserves insertion order for python 3.6+)
for cov_type in ["past", "future"]:
if cov_type in covariate_matrices:
# component-wise lags
if cov_type in self.component_lags:
tmp_X = [
covariate_matrices[cov_type][
:,
np.array(comp_lags) - self.lags[cov_type][0] + t_pred,
comp_i,
]
for comp_i, (comp, comp_lags) in enumerate(
self.component_lags[cov_type].items()
)
]
np_X.append(
np.concatenate(tmp_X, axis=1).reshape(
len(series) * num_samples, -1
)
)
else:
np_X.append(
covariate_matrices[cov_type][
:, relative_cov_lags[cov_type] + t_pred
].reshape(len(series) * num_samples, -1)
)

# concatenate retrieved lags
X = np.concatenate(np_X, axis=1)
# Need to split up `X` into three equally-sized sub-blocks
# corresponding to each timeseries in `series`, so that
# static covariates can be added to each block; valid since
# each block contains same number of observations:
X_blocks = np.split(X, len(series), axis=0)
X_blocks, _ = add_static_covariates_to_lagged_data(
X_blocks,
series,
# concatenate previous iteration forecasts
if "target" in self.lags and predictions:
series_matrix = np.concatenate([series_matrix, predictions[-1]], axis=1)

# extract and concatenate lags from target and covariates series
X = _create_lagged_data_autoregression(
target_series=series,
t_pred=t_pred,
shift=shift,
last_step_shift=last_step_shift,
series_matrix=series_matrix,
covariate_matrices=covariate_matrices,
lags=self.lags,
component_lags=self.component_lags,
relative_cov_lags=relative_cov_lags,
num_samples=num_samples,
uses_static_covariates=self.uses_static_covariates,
last_shape=self._static_covariates_shape,
last_static_covariates_shape=self._static_covariates_shape,
)
X = np.concatenate(X_blocks, axis=0)

# X has shape (n_series * n_samples, n_regression_features)
prediction = self._predict_and_sample(
Expand Down Expand Up @@ -1257,6 +1199,7 @@ def _optimized_historical_forecasts(
stride=stride,
overlap_end=overlap_end,
show_warnings=show_warnings,
verbose=verbose,
predict_likelihood_parameters=predict_likelihood_parameters,
**kwargs,
)
Expand All @@ -1273,6 +1216,7 @@ def _optimized_historical_forecasts(
stride=stride,
overlap_end=overlap_end,
show_warnings=show_warnings,
verbose=verbose,
predict_likelihood_parameters=predict_likelihood_parameters,
**kwargs,
)
Expand Down
Loading

0 comments on commit eecfd0f

Please sign in to comment.