Deploying a scikit-learn model

As mention in the deploying models guide, deploying models via Verta Inference is a two step process: (1) first create an endpoint, and (2) update the endpoint with a model.

This tutorial explains how Verta Inference can be used to deploy a scikit-learn model.

1. Create an endpoint

Users can create an endpoint using Client.create_endpoint() as follows:

census_endpoint = client.create_endpoint(path="/census")

2. Updating the endpoint with a RMV

As discussed in the Catalog Overview, there are multiple of ways to create an RMV for a scikit-learn model.

First, given a scikit-learn model object, users can use the sklearn convenience functions to create a Verta Standard Model.

from verta.environment import Python

model_version_v1 = registered_model.create_standard_model_from_sklearn(
    model,
    environment=Python(requirements=["scikit-learn"]),
    name="v1",
)

Alternatively, a scikit-learn model can be used as an artifact in a model that extends VertaModelBase.


import cloudpickle
with open("model.pkl", "wb") as f:
    cloudpickle.dump(model, f)

from verta.registry import VertaModelBase

class CensusIncomeClassifier(VertaModelBase):
    def __init__(self, artifacts):
        self.model = cloudpickle.load(open(artifacts["serialized_model"], "rb"))

    def predict(self, batch_input):
        results = []
        for one_input in batch_input:
            results.append(self.model.predict(one_input))
        return results

model_version_v2 = registered_model.create_standard_model(
    model_cls=CensusIncomeClassifier,
    environment=Python(requirements=["scikit-learn"]),
    artifacts=artifacts_dict,
    name="v2"
)

Regardless of how a Registered Model Version has been created, the endpoint defined above can now be upated and we can make predictions against it.

census_endpoint = client.get_or_create_endpoint("census-model")
census_endpoint.update(model_version_v1, wait=True)
deployed_model = census_endpoint.get_deployed_model()
deployed_model.predict(X_test.values.tolist()[:5])

The full code for this tutorial can be found here.

Last updated