pytorch-forecasting icon indicating copy to clipboard operation
pytorch-forecasting copied to clipboard

AttributeError: 'CompositeMetric' object has no attribute 'rescale_parameters'

Open AdolHong opened this issue 3 years ago • 4 comments
trafficstars

  • PyTorch-Forecasting version: 0.10.1
  • PyTorch version: 1.11.0+cu113
  • Python version: 3.7.13
  • Operating System: colab

Expected behavior

I executed code ... in order to ... and expected to get result ... i want to calculate custom loss metric, for example: rmse() + mae(), but i met error AttributeError: 'CompositeMetric' object has no attribute 'rescale_parameters'

orig_data = [[1, 'id1', 1.0],
 [2, 'id1', 1.0],
 [3, 'id1', 2.0],
 [4, 'id1', 2.0],
 [5, 'id1', 3.0],
 [6, 'id1', 3.0],
 [7, 'id1', 4.0],
 [8, 'id1', 4.0],
 [9, 'id1', 5.0],
 [10, 'id1', 5.0],
 [11, 'id1', 6.0],
 [12, 'id1', 6.0],
]

df = pd.DataFrame(orig_data, columns = ['time_idx', 'id', 'value'])
df['id'] = df['id'].astype(str).astype("category")
df['value'] = df['value'].astype(float)


training = TimeSeriesDataSet(
    df,
    time_idx="time_idx",
    target="value",
    group_ids=["id"],
    min_encoder_length=2,  # allow encoder lengths from 0 to max_prediction_length
    max_encoder_length=2,
    min_prediction_length=5,
    max_prediction_length=5,
    static_categoricals=["id"],
    time_varying_known_reals=["time_idx"],
    time_varying_unknown_reals=['value'],
    allow_missing_timesteps=True
)

trainer = pl.Trainer(
    max_epochs=30,
    gpus=0,
    weights_summary="top",
    gradient_clip_val=0.1,
    limit_train_batches=30,  # coment in for training, running valiation every 30 batches
    # fast_dev_run=True,  # comment in to check that networkor dataset has no serious bugs
    callbacks=[lr_logger, early_stop_callback],
    logger=logger,
)

tft = TemporalFusionTransformer.from_dataset(
    training,
    learning_rate=0.00003,
    hidden_size=16,
    attention_head_size=1,
    dropout=0.1,
    hidden_continuous_size=8,
    output_size=1,  # 7 quantiles by default
    loss=RMSE() + RMSE(),
    log_interval=10,  # uncomment for learning rate finder and otherwise, e.g. to 10 for logging every 10 batches
    reduce_on_plateau_patience=4,
)


colab notebook: https://colab.research.google.com/drive/1wOMp88ud4EfF-MIljktJOcHOyrL1tRc5?usp=sharing

could you give me some help?

AdolHong avatar Jun 15 '22 15:06 AdolHong

I changed line 473 of pytorch_forecasting/metrics/base_metrics.py and fixed the bug, see below

line473: # class CompositeMetric(LightningMetric):
updated : class CompositeMetric(Metric):

AdolHong avatar Jun 15 '22 15:06 AdolHong

Nice catch and workaround! I was facing the same error and was thinking of monkeypatching, but simple inheritance change solved the issue, thank you!

fnavruzov avatar Jun 16 '22 21:06 fnavruzov

@AdolHong Thanks for sharing your solution to this problem!

TaroKawa avatar Jun 25 '22 12:06 TaroKawa