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.

Saving a model after a warm start

See original GitHub issue

I am using the below code to fit a model using a warm start. which calls the below function to write the model to file. I am able to save the initial model without any issues. However, when I come to try and save a model after a warm start (that is essentially two models that have been combined) I get the below error:

Object of type ndarray is not JSON serializable from the json.dump(model_to_json(model) code.

Can anyone help, this is quite urgent. Thank you!

` m_current = read_model_from_cloud(userId, containerId, tagId)

    m = Prophet.Prophet(daily_seasonality='auto',
                        yearly_seasonality='auto',
                        weekly_seasonality='auto',
                        interval_width=interval_width,
                        changepoint_range=0.8)
    if m_current is not None:
        m = m.fit(df, init=stan_init(m_current))
    else:
        m = m.fit(df)
    upload_model_to_cloud(m,
                          userId,
                          containerId,
                          tagId,
                          lastRun)`

with open(model_name, 'w') as fout: json.dump(model_to_json(model), fout)

The issue seems to be with the model_to_json function

Screen Shot 2020-10-01 at 9 58 04 pm

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
blethamcommented, Mar 3, 2021

This is fixed in 29f1417.

test code:

from fbprophet.serialize import model_to_json, model_from_json
from fbprophet import Prophet
import pandas as pd


def stan_init(m):
    res = {}
    for pname in ['k', 'm', 'sigma_obs']:
        res[pname] = m.params[pname][0][0]
    for pname in ['delta', 'beta']:
        res[pname] = m.params[pname][0]
    return res

df = pd.read_csv('../examples/example_wp_log_peyton_manning.csv')
df1 = df.loc[df['ds'] < '2016-01-19', :]  # All data except the last day
m1 = Prophet().fit(df1)
m2 = Prophet().fit(df, init=stan_init(m1)) 

mj1 = model_to_json(m1)  # works
mj2 = model_to_json(m2)  # failed before, now works

1reaction
Fasturtle1commented, Dec 7, 2020

it should be the case that any numpy arrays are being converted to lists before serialization.

Using the debugger, I found two fields “delta” and “beta” which remain in the ndarray format. Convert them to a list format manually before calling the “model_to_json” method.

model.fit_kwargs['init']['delta'] = model.fit_kwargs['init']['delta'].tolist()
model.fit_kwargs['init']['beta'] = model.fit_kwargs['init']['beta'].tolist()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load models | TensorFlow Core
Model progress can be saved during and after training. This means a model can resume where it left off and avoid long training...
Read more >
Warm starting for efficient deep learning resource utilization
In this post, we discuss how warm-starting can save computational resources and improve generalizability when training deep learning models.
Read more >
Keras: How to save model and continue training?
read()) # open the model you just saved (same as your last train) with a different name old_model.load_weights('yours.h5') # the model ...
Read more >
How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >
Save solution and warm start - Julia Discourse
I am using JuMP and CPLEX to solve an optimization problem. How can I save the best solution found after a certain time...
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