Virtualitics Predict has internal support for saving trained models. Depending on your application, you may want to use a trained model in other Steps within the Flow used to train the model, or other Flows developed in the future.
You are able to store and load your model by using the Model
and StoreInterface
functions within Virtualitics Predict.
Storing a Model Asset
The code below is an example of how to store a Model Asset:
from predict_backend.ml.model import Model
from predict_backend.store.store_interface import StoreInterface
class SaveModel(Step):
def run(self, flow_metadata):
# Retrieve StoreInterface object
store_interface = StoreInterface(**flow_metadata)
# Store model as an asset by calling the model function.
# The concepts of 'label' and 'alias' are described below per parameter.
model_asset = Model(model=model,
label="model_x", # label - analogous to a bucket/folder
alias="trained_10202022" # alias - the version of the saved model
)
# Use the "store_interface" object to save your model asset.
store_interface.save_asset(model_asset)
Loading a Model Asset and Using the Trained Model
The code below is an example of how to load a Model Asset and use the trained Model:
from predict_backend.store.store_interface import StoreInterface
class ModelPredict(Step):
def run(self, flow_metadata):
store_interface = StoreInterface(**flow_metadata)
# Use StoreInterface "get_model" attribute to retrieve model.
# Must use identical label and alias names to retrieve the correct model.
# Alternatively, if "alias" parameter is not used, the most recent version of the model will be retrieved.
model = store_interface.get_model(label="model_x",alias="trained_10202022")
# Model assets are a wrapper for their underlying object, so you can call functions,
# set/get attributes directly on the Model object!
results = model.predict(data)
What to Expect
When you have saved your model as a Model Asset, your model is accessible from any Flow within Virtualitics Predict.
When you load your Model Asset as a model, you may use your model per the functions associated with your model.
Previous Article |
Next Article |