ibm-granite / granite-tsfm

Foundation Models for Time Series
Apache License 2.0
285 stars 152 forks source link

Question: How do I invert the scaling of predictions when using the models for inference? #48

Closed JamieToddUK closed 1 month ago

JamieToddUK commented 1 month ago

I am using a TimeSeriesPreprocessor with scaling = True for a ForecastDFDataset, and the PatchTSMixer and PatchTST models use scaling = "std".

After training, when I inspect the model's predictions using:

predictions = trainer.predict(dataset)
pred_values = predictions.predictions[0][..., :num_targets]
true_values = predictions.label_ids[..., :num_targets]

The predicted and true values for the target columns are still normalised. How can I revert this scaling to analyse the predicted values I want?

Setting scaling = False for the TimeSeriesPreprocessor did not work, neither did setting scaling = False for the models.

Thanks in advance.

wgifford commented 1 month ago

Similar to https://github.com/ibm-granite/granite-tsfm/issues/46

# define train and test data
train_data = # pandas dataframe
test_data = # pandas dataframe

# define a preprocessor
tsp = TimeSeriesPreprocessor(
        timestamp_column=...
        scaling=True,
    )

# train the preprocessor
tsp.train(train_data)

# define a forecasting pipeline
forecast_pipeline = TimeSeriesForecastingPipeline(
    model=model,
    timestamp_column=...
    feature_extractor=tsp, # note, needed if we want to inverse scale internally
    inverse_scale_outputs=True,
)

# forecast using the pipeline on the scaled test data
forecasts = forecast_pipeline(tsp.preprocess(test_data))

You can find a similar example in the tests: https://github.com/ibm-granite/granite-tsfm/blob/main/tests/toolkit/test_time_series_forecasting_pipeline.py#L113

JamieToddUK commented 1 month ago

@wgifford Thank you for the help and quick response!