For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Serialization

This document explains how to serialize build-in models in Concrete ML.

Introduction

Serialization allows you to dump a fitted and compiled model into a JSON string or file. You can then load the estimator back using the JSON object.

Saving Models

All built-in models provide the following methods:

  • dumps: Dumps the model as a string.

  • dump: Dumps the model into a file.

For example, a logistic regression model can be dumped in a string as follows:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

from concrete.ml.sklearn import LogisticRegression

# Create the data for classification:
X, y = make_classification()

# Retrieve train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

# Instantiate, train and compile the model
model = LogisticRegression()
model.fit(X_train, y_train)
model.compile(X_train)

# Run the inference in FHE
y_pred_fhe = model.predict(X_test, fhe="execute")

# Dump the model in a string
dumped_model_str = model.dumps()

Similarly, it can be dumped into a file:

Alternatively, Concrete ML provides two equivalent global functions:

Loading Models

You can load a built-in model using the following functions:

  • loads: Loads the model from a string.

  • load: Loads the model from a file.

The same logistic regression model can be loaded as follows:

Last updated

Was this helpful?