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

Add the trainer back to the forecaster after saving model to file #1416

Merged
merged 1 commit into from Sep 11, 2023

Conversation

ziqin8
Copy link
Contributor

@ziqin8 ziqin8 commented Sep 7, 2023

🔬 Background

  • Why is this change needed? Is there a related issue or a new feature to be added?

After save, forecaster.predict won't work as the trainer is removed.

To reproduce:

import pandas as pd
from neuralprophet import NeuralProphet, set_log_level, df_utils
from neuralprophet import save as npsave

data_location = "https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/yosemite_temps.csv"
df = pd.read_csv(data_location)

# first model
m = NeuralProphet()
_ = m.fit(df, epochs=2, learning_rate=0.01, progress=None)
npsave(m, "test.np")
forecast = m.predict(df.iloc[-2:])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[110], line 12
     10 _ = m.fit(df, epochs=2, learning_rate=0.01, progress=None)
     11 npsave(m, "test.np")
---> 12 forecast = m.predict(df.iloc[-2:])

File ~/mambaforge/envs/neuralprophet/lib/python3.8/site-packages/neuralprophet/forecaster.py:1049, in NeuralProphet.predict(self, df, decompose, raw)
   1047 forecast = pd.DataFrame()
   1048 for df_name, df_i in df.groupby("ID"):
-> 1049     dates, predicted, components = self._predict_raw(
   1050         df_i, df_name, include_components=decompose, prediction_frequency=self.prediction_frequency
   1051     )
   1052     df_i = df_utils.drop_missing_from_df(
   1053         df_i, self.config_missing.drop_missing, self.predict_steps, self.n_lags
   1054     )
   1055     if raw:

File ~/mambaforge/envs/neuralprophet/lib/python3.8/site-packages/neuralprophet/forecaster.py:2746, in NeuralProphet._predict_raw(self, df, df_name, include_components, prediction_frequency)
   2744     self.model.set_covar_weights(self.model.get_covar_weights())
   2745 # Compute the predictions and components (if requested)
-> 2746 result = self.trainer.predict(self.model, loader)
   2747 # Extract the prediction and components
   2748 predicted, component_vectors = zip(*result)

AttributeError: 'NoneType' object has no attribute 'predict'

🔮 Key changes

  • Explain the main changes introduced by this pull request for the reviewer.

Add the trainer back to the forecaster after torch.save

📋 Review Checklist

  • I have performed a self-review of my own code.
  • I have commented my code, added docstrings and data types to function definitions.
  • I have added pytests to check whether my feature / fix works.

Please make sure to follow our best practices in the Contributing guidelines.

@codecov
Copy link

codecov bot commented Sep 7, 2023

Codecov Report

Merging #1416 (6a4a070) into main (1c6094d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1416   +/-   ##
=======================================
  Coverage   89.87%   89.88%           
=======================================
  Files          38       38           
  Lines        5066     5071    +5     
=======================================
+ Hits         4553     4558    +5     
  Misses        513      513           
Files Changed Coverage Δ
neuralprophet/utils.py 79.65% <100.00%> (+0.30%) ⬆️

📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@leoniewgnr leoniewgnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice! Thanks @c3-ziqin for this change!

@leoniewgnr leoniewgnr added the status: ready PR is ready to be merged label Sep 11, 2023
@leoniewgnr leoniewgnr merged commit 56d9910 into ourownstory:main Sep 11, 2023
12 of 13 checks passed
@github-actions
Copy link

Model Benchmark

Benchmark Metric main current diff
YosemiteTemps MAE_val 1.34899 1.34899 0.0%
YosemiteTemps RMSE_val 2.00817 2.00817 0.0%
YosemiteTemps Loss_val 0.00078 0.00078 0.0%
YosemiteTemps MAE 1.32133 1.32133 0.0%
YosemiteTemps RMSE 2.13713 2.13713 0.0%
YosemiteTemps Loss 0.00064 0.00064 0.0%
YosemiteTemps time 57.5074 60.93 5.95% ⚠️
PeytonManning MAE_val 0.58162 0.58162 0.0%
PeytonManning RMSE_val 0.72218 0.72218 0.0%
PeytonManning Loss_val 0.01239 0.01239 0.0%
PeytonManning MAE 0.41671 0.41671 0.0%
PeytonManning RMSE 0.55961 0.55961 0.0%
PeytonManning Loss 0.00612 0.00612 0.0%
PeytonManning time 12.3769 12.71 2.69%
AirPassengers MAE_val 13.0627 13.0627 0.0%
AirPassengers RMSE_val 15.9453 15.9453 0.0%
AirPassengers Loss_val 0.00131 0.00131 0.0%
AirPassengers MAE 9.88153 9.88153 0.0%
AirPassengers RMSE 11.7354 11.7354 0.0%
AirPassengers Loss 0.00052 0.00052 0.0%
AirPassengers time 5.16755 5.36 3.72% ⚠️
Model training plots

Model Training

PeytonManning

YosemiteTemps

AirPassengers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready PR is ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants