Creating custom models with dependencies

Sometimes just custom models by themselves are not enough and you need to add other data necessary for defining your model behavior.

Verta supports adding artifacts to custom models and this tutorial shows you how to add dependencies to your model.

Class model definition

A Class Model must implement this interface:

class MyModel(object):
    def __init__(self, artifacts):

    def predict(self, data):

__init__() takes an argument called artifacts. This is argument a dictionary of artifact keys to files or directories for those artifacts. As an example:

artifacts == {
    'tensorflow_saved_model': "/deployment/artifacts/saved_model",
    'nearest_neighbor_index': "/deployment/artifacts/index.ann",

The locations where the artifacts are saved at the time that the model object is initialized are automatically handled for you.

This dictionary can be used in conjunction with open(), json.load(), and machine learning frameworks to load data from files. For example, to load a TensorFlow model, you could do:

def __init__(self, artifacts):
    self.session = tf.Session()
        artifacts['tensorflow_saved_model'],  # logged artifact

The keys and values of this dictionary map directly to the arguments used to log such dependencies.

Logging model dependencies

Any class in the Verta platform that has a log_artifact() method, such as an ExperimentRun, supports logging dependencies for their associated models. The method signature is generally:

run.log_artifact("user-defined-name", "location/on/disk/to/artifact")

Please check the documentation for the particular class for more details.

Once you have logged the artifacts into the system, you can log your model and explicitly state the dependency like

    MyModel,  # class, not instance
    artifacts=["tensorflow_saved_model", "nearest_neighbor_index"],  # logged artifact keys

The same can be done using a RegisteredModelVersion:

model_version.log_artifact("user-defined-name", "location/on/disk/to/artifact")
    MyModel,  # class, not instance
    artifacts=["tensorflow_saved_model", "nearest_neighbor_index"],  # logged artifact keys

By doing this, whenever an object of MyModel is instantiated by Verta, the artifacts will be available for consumption and passed to the constructor automatically.

Local testing

For local testing, you can use the equivalent method fetch_artifacts() to fetch the artifacts and initialize the model.

artifacts = run.fetch_artifacts(["tensorflow_saved_model", "nearest_neighbor_index"])
model = Model(artifacts=artifacts)

Last updated