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

Bugfix, weights shape during length randomization #1318

Merged

Conversation

bohdan-safoniuk
Copy link
Contributor

Description

This PR fixes RuntimeError during calculation of weighted MultiHorizonMetric loss due to losses and weights shape mismatch.

Setup

training_dataset = TimeSeriesDataSet(
    dataset.loc[dataset["date"] <= cutoff_],
    time_idx="time_idx",
    target="target",
    weight="weight",
    group_ids=["group_id_1", "group_id_2", "group_id_3"],
    min_encoder_length=max_encoder_length // 2,
    max_encoder_length=max_encoder_length,
    min_prediction_length=1,
    max_prediction_length=max_prediction_length,
    static_categoricals=static_categoricals,
    time_varying_known_reals=time_varying_known_reals,
    time_varying_known_categoricals=time_varying_known_categoricals,
    time_varying_unknown_reals=time_varying_unknown_reals,
    target_normalizer=target_normalizer,
    categorical_encoders=categorical_encoders,
    scalers=scalers,
    add_relative_time_idx=True,
    add_encoder_length=True,
    randomize_length=(0.1, 0.15),
    allow_missing_timesteps=False,
)

tft = TemporalFusionTransformer.from_dataset(
    training_dataset,
    learning_rate=0.0001,
    hidden_size=256,
    lstm_layers=2,
    attention_head_size=4,
    dropout=0.5,
    hidden_continuous_size=128,
    output_size=7,
    loss=QuantileLoss(),
    reduce_on_plateau_patience=4,
)

Error

File /opt/conda/envs/forecaster-env/lib/python3.9/site-packages/pytorch-forecasting/pytorch_forecsting/metrics/base_metrics.py in MultiHorizonMetric.update(self, y_pred, target)
     786   if weight is not None:
---> 787       losses = losses * unsqueeze_like(weight, losses)
     789   self._update_losses_and_lengths(losses, lengths)

RuntimeError: The size of tensor a (12) must match the size of tensor b (22) at non-singleton dimension 1

Issues

Possible linked issues (they have exact same error, but different setup)

@jdb78 jdb78 force-pushed the bigfix/timeseriesdataset-weights-shape branch from 3fbf9ed to 4149e9a Compare September 10, 2023 21:11
@codecov-commenter
Copy link

codecov-commenter commented Sep 10, 2023

Codecov Report

Patch coverage: 50.00% and project coverage change: -0.02% ⚠️

Comparison is base (9995d0a) 90.13% compared to head (4149e9a) 90.11%.
Report is 2 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1318      +/-   ##
==========================================
- Coverage   90.13%   90.11%   -0.02%     
==========================================
  Files          30       30              
  Lines        4712     4714       +2     
==========================================
+ Hits         4247     4248       +1     
- Misses        465      466       +1     
Flag Coverage Δ
cpu 90.11% <50.00%> (-0.02%) ⬇️
pytest 90.11% <50.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
pytorch_forecasting/data/timeseries.py 93.19% <50.00%> (-0.13%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jdb78 jdb78 merged commit 66ca7ea into sktime:master Sep 10, 2023
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants