Save and load the model object

After executing the model run, we recommend saving the model object for future use. This helps you avoid repetitive model runs, and saves time and computational resources. After the model object is saved, the object can be loaded at a later stage to continue the analysis or visualizations without having to re-run the model.

Using Python pickle

Save the model object

Run the following command to save the model object:

file_path = f'{PATH}/{FILENAME}.pkl'
model.save_mmm(mmm, file_path)

Where:

  • PATH is the path to the file location.
  • FILENAME is the name of the file. It must have a PKL extension.

Load the model object

Run the following command to load the saved model:

file_path = f'{PATH}/{FILENAME}.pkl'
mmm = model.load_mmm(file_path)

Where:

  • PATH is the path to the file location.
  • FILENAME is the name of the file. It must have a PKL extension.

Use Meridian serde package

The Meridian serde package provides support for serializing and deserializing Meridian model objects into Protocol Buffers (protobuf) files. The format - binary or text - is determined by the file extension specified in the filename.

Formats include:

  • Binary Protobuf (.binpb) - Saved in compact protobuf wire format. Ideal for binary transmission and storage.
  • Text Protobuf (.txtpb or .textproto) - Saved in human-readable text representation. Ideal for debugging.

Save the model object

Run the following command to save the model object:

from meridian.schema.serde import meridian_serde

mmm = model.Meridian(input_data=input_data, model_spec=model_spec)
meridian_serde.save_meridian(mmm, "model.binpb") # or "model.txtpb"

Load the model object

Run the following command to load the model object:

from meridian.schema.serde import meridian_serde

mmm = meridian_serde.load_meridian("model.binpb") # or "model.txtpb"