Model Serialization#

Once a Gloria model has been trained you may want to store it for reuse without retraining. This is useful in various scenarios, such as resuming work in a later session, transferring a model between machines, or passing it between stages of a processing pipeline. On that account, Gloria provides built-in helper methods to serialize and deserialize fitted models. A deserialized model retains all the information needed to generate forecasts based on its previous fit.

Serialize to JSON#

The main method for serializing a model is Gloria.to_json(), which converts a fitted model into a JSON-formatted string:

# Gloria model must be fitted
model.fit(df)

# Serialize the model to a JSON string
serialized = model.to_json(indent=2)

# Print the string
print(serialized[:290] + "\n...")

This will print output similar to the following:

{
  "model": "poisson",
  "timestamp_name": "ds",
  "metric_name": "y",
  "capacity_name": "",
  "n_changepoints": 2,
  "changepoint_range": 0.8,
  "seasonality_prior_scale": 3,
  "event_prior_scale": 3,
  "changepoint_prior_scale": 3,
  "dispersion_prior_scale": 3,
  "interval_width": 0.8
...

The serialized model can then be loaded back using Gloria.from_json(), which reconstructs the original model. The restored model can be used for prediction:

# Deserialize the model
model_copy = Gloria.from_json(serialized)

# Make prediction using the deserialized model
result = model_copy.predict()

# Plot results
model_copy.plot(result)

Both Gloria.to_json() and Gloria.from_json() accept an optional filepath argument. If provided, the methods will write to or read from the specified file path instead of working with strings directly.

Tip

Serialize to a Dictionary#

Gloria models can also be serialized to and restored from Python dictionaries using Gloria.to_dict() and Gloria.from_dict(). The workflow is identical to working with JSON-serialized models.

Note

Although Gloria.to_dict() and Gloria.from_dict() are exposed to users, they primarily serve as helper methods for their JSON counterparts. Consequently, non–JSON-serializable objects (such as NumPy arrays or pandas DataFrames) are not preserved in their original types within the dictionary. Instead, they are converted to serializable representations.