prophet
prophet copied to clipboard
refitting already fitted model
I have declared and fitted a model with the following code: model= Prophet(weekly_seasonality=False, daily_seasonality=True, changepoint_prior_scale=0.5)
and i add 4 regressor model.add_regressor('regressor1') model.add_regressor('regressor2') model.add_regressor('regressor3') model.add_regressor('regressor4')
i follow rules in "Updating fitted models" section at link: https://facebook.github.io/prophet/docs/additional_topics.html#flat-trend-and-custom-trends
But sometimes i try to use this function to refit a new model the following two error pops up:
- mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=delta; position=0; dims declared=(3); dims found=(25)
- invalid index to scalar variable - this happens in function stan_init
Can you help me to understand what are the problems?
The code in the docs will work if you keep the same number of parameters. By adding 4 regressors, you're adding 4 extra parameters, so if you'll need to pass initial values for these 4 parameters as well.
In the def stan_init(m) function, res['beta'] picks up the coefficients of the existing seasonality/holiday/regressor features. You'll need to use np.hstack to add 4 additional columns to this array. The initial values can all be zeroes, i.e.:
...
res['beta'] = np.hstack(m.params['beta'][0], np.zeros(4))
return res
Thanks for the response.
I don't understand the response, i try to explain how my software works.
pr= Prophet(weekly_seasonality=False, daily_seasonality=True, changepoint_prior_scale=0.5) pr.add_regressor('regressor1') pr.add_regressor('regressor2') pr.add_regressor('regressor3') pr.add_regressor('regressor4')
model = pr.fit(dataframe)
dataframe contains 6 columns: ds: timestamp y: data to predict regressor1 regressor2 regressor3 regressor4
_ @tcuongd My model contains in beta data for regressors, is it correct?_
I save my model in json format: with open(path, 'w') as fout: json.dump(model_to_json(model), fout) # Save model
where path is the system operative path where i want to save my json file.
When new data will come:
with open(path, 'r') as fin: old_model = model_from_json(json.load(fin)) # Load model, same path as before
New prophet instance with the same parameters then old_model: prophet = Prophet( growth=old_model.growth, n_changepoints=old_model.n_changepoints, changepoint_range=old_model.changepoint_range, yearly_seasonality=old_model.yearly_seasonality, weekly_seasonality=old_model.weekly_seasonality, daily_seasonality=old_model.daily_seasonality, holidays=old_model.holidays, seasonality_mode=old_model.seasonality_mode, seasonality_prior_scale=old_model.seasonality_prior_scale, holidays_prior_scale=old_model.holidays_prior_scale, changepoint_prior_scale=old_model.changepoint_prior_scale, mcmc_samples=old_model.mcmc_samples, interval_width=old_model.interval_width, uncertainty_samples=old_model.uncertainty_samples, stan_backend=old_model.stan_backend )
Add regressors: prophet .add_regressor('regressor1') prophet .add_regressor('regressor2') prophet .add_regressor('regressor3') prophet .add_regressor('regressor4')
Create new model starting from previous one: new_model = prophet.fit(dataframe, init=self.__stan_init(old_model)) # Adding new data starting from old model m1
dataframe contains 6 columns: ds: timestamp y: data to predict regressor1 regressor2 regressor3 regressor4
I save my model in json format over the old json model: with open(path, 'w') as fout: json.dump(model_to_json(new_model ), fout) # Save model
my questions are:
- old_model was created using 4 regressors, the same was done for new_model. Why columns of "Beta" params of old_model and new_model should have difference size?
- Is my approach correct?
Nobody can explain me something?
@tcuongd?
Nobody can explain me something?
I will when I solve it
You need to load the alibi model state using model_from_json and initialize it. Then you can based the model state to stan_init func to extract the params.
`def load_alibi_model_state(json_state): """ Converts string model state into model itself Args: json_state (str): Model state as string
Returns:
model (Alibi-Detect Model): Alibi-Detect[Prophet] Model
"""
state = model_from_json(json_state)
state_dict = {'model': state, 'cap': None}
return init_od_prophet(state_dict)`
`def stan_init(m) -> Dict: """ Retrieve parameters from a trained model. Extracts necessary parameters for stan init: See: https://facebook.github.io/prophet/docs/additional_topics.html
Args:
m (Alibi-Detect[Prophet] Model): A trained model of the Fitted
Alibi-Detect[Prophet] Class
Returns:
res (dict): A Dictionary containing retrieved parameters of m.
"""
res = {}
for pname in ['k', 'm', 'sigma_obs']:
res[pname] = m.model.params[pname][0][0]
for pname in ['delta', 'beta']:
res[pname] = m.model.params[pname][0]
return res`
p = OutlierProphet(**get_model_params(fitted_model_state.model))
op.model.fit(pdf, init=stan_init(fitted_model_state))
Play with it and and see if it works for you
Hey i have the same error when i fit my model.
mismatch in dimension declared and found in context; processing stage=parameter initialization; variable name=delta; position=0; dims declared=(16); dims found=(11)
Is there a way to know what causes the error ? For example : dims declared=(16); ------> is there a way to know from the input data how we decide this dims is 16? since some of my model works some not and i want to know what kind of input data causes the issue, for example, the new input data i use to update the model is not long enough for whole seasonal or what
my save model params are :
{'k': 5.301105415174269e-18,
'm': -6.433904274365651e-20,
'sigma_obs': 1.5685016108618157e-11,
'delta': array([-0.06820517, 0.30048138, -0.06612237, -0.0062083 , -0.00912226,
0.05885513, -0.01655667, -0.00121885, -0.17013535, -0.12073692,
-0.04170187]),
'beta': array([-0.66049279, 2.73312236, -1.51306781, 2.74887553, -0.40755755,
1.96982752, 5.11581133, -8.18825939, -0.18914887, -0.15139349,
-2.95247916, 3.2990841 , -4.53187238, 2.57813818, -4.80676602,
3.05610153, 2.61037924, 8.35775864, 0.60808887, 0.28357102,
2.87760787])}
my train data size is 22 weeks, and i need to update the model with a 7 weeks new data . i forecast weekly data.
and when i train the model i get this warning :
2022-08-19 13:37:01,527 - prophet - INFO - n_changepoints greater than number of observations. Using 11.