question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save serialized prophet model

See original GitHub issue

Hi,

I tried using the kats package with the default prophet functions to serialize the prophet model using the below code:

from kats.models.prophet import ProphetModel, ProphetParams
import json
from prophet.serialize import model_to_json, model_from_json
# create a model param instance
params = ProphetParams(seasonality_mode=args.seasonality_mode)  # additive mode gives worse results
m = ProphetModel(DATA_DF, params)
m.fit()
save_model(m, 'serialized_model.json')

def save_model(model, file_name):
    with open(file_name, 'w') as file:
        json.dump(model_to_json(model), file)

Currently, it is throwing this error:

Traceback (most recent call last):
  File "kats_example.py", line 67, in save_model
    json.dump(model_to_json(model), file)
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/prophet/serialize.py", line 56, in model_to_json
    if model.history is None:
AttributeError: 'ProphetModel' object has no attribute 'history'

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
rosalineswaycommented, Jun 1, 2022

@neelabalan that did work, thanks!

1reaction
neelabalancommented, Jun 1, 2022

did you use trained_model.model? The difference is

type(trained_model)
# kats.models.prophet.ProphetModel

type(trained_model.model)
# fbprophet.forecaster.Prophet

The above backtester example works fine for me

from kats.utils import backtesters

ALL_ERRORS = ["mape", "smape", "mae", "mase", "mse", "rmse"]
backtester_prophet = backtesters.CrossValidation(
        error_methods=ALL_ERRORS,
        data=tsd,
        params=ProphetParams(seasonality_mode='multiplicative'),
        train_percentage=50,
        test_percentage=20,
        model_class=ProphetModel,
        num_folds=10,
)
backtester_prophet.run_cv()

(
        train_data,
        test_data,
        trained_model,
        forecast_predictions,
) = backtester_prophet.results[0]

with open('serialized_model.json', 'w') as file:
    file.write(model_to_json(trained_model.model))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Additional Topics | Prophet - Meta Open Source
It is possible to save fitted Prophet models so that they can be loaded and used later. ... The json file will be...
Read more >
How to save a fbprophet forecast model using pickle such that ...
It is possible to save fitted Prophet models so that they can be loaded and used later. In Python, models should not be...
Read more >
Time Series Part 3: Forecasting with Facebook Prophet: An Intro
Saving Prophet models is a bit different than saving SARIMAX models. In Python, Prophet models should not be saved with pickle; the Stan...
Read more >
How to save Prophet models in Dataiku and use them across ...
Alternatively, you can serialize the model to json, save it locally, and then load the model within another project.
Read more >
Download button to save Facebook Prophet models (json ...
Hi guys, I'm trying to create a download button save Facebook Prophet models, a forecasting utility. FYI, here's the code to do a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found