The Model class represents a namespace for organizing artifacts, metadata, and other resources related to a machine learning model.
Signature
Model(
name: str ,
license : Optional[ str ] = None ,
description: Optional[ str ] = None ,
audience: Optional[ str ] = None ,
use_cases: Optional[ str ] = None ,
limitations: Optional[ str ] = None ,
trade_offs: Optional[ str ] = None ,
ethics: Optional[ str ] = None ,
tags: Optional[List[ str ]] = None ,
version: Optional[Union[ModelStages, int , str ]] = None ,
save_models_to_registry: bool = True ,
)
Parameters
The license under which the model is created.
The description of the model.
The target audience of the model.
The use cases of the model.
The known limitations of the model.
The tradeoffs of the model.
The ethical implications of the model.
Tags associated with the model.
version
Union[ModelStages, int, str]
The version name, version number, or stage. If skipped, a new version will be created. Supports placeholders like {date} and {time}, plus custom placeholders passed as substitutions.
Whether to save all ModelArtifacts to Model Registry if available in the active stack.
Properties
The unique identifier of the model version.
The unique identifier of the model.
The version number of this model version.
The current stage of this model version (e.g., staging, production).
Methods
load_artifact
def load_artifact (
name : str ,
version : Optional[ str ] = None
) -> Any
Load an artifact linked to this model version.
Parameters:
name (str): Name of the artifact to load
version (Optional[str]): Version of the artifact to load
Returns: The loaded artifact
get_artifact
def get_artifact (
name : str ,
version : Optional[ str ] = None
) -> Optional[ArtifactVersionResponse]
Get the artifact response linked to this model version.
set_stage
def set_stage (
stage : Union[ str , ModelStages],
force : bool = False
) -> None
Set this model version to a specific stage.
Parameters:
stage: The target stage (e.g., “staging”, “production”)
force: Whether to force archiving of the current version in the target stage
def log_metadata (
metadata : Dict[ str , MetadataType]
) -> None
Log metadata for this model version.
Parameters:
metadata: Dictionary of metadata key-value pairs
Examples
Basic Model Usage
from zenml import Model, pipeline, step
model = Model(
name = "iris_classifier" ,
version = "1.0.0" ,
description = "Iris species classifier" ,
tags = [ "classification" , "iris" ]
)
@pipeline ( model = model)
def training_pipeline ():
# Pipeline steps
pass
Using Model Stages
from zenml import Model, ModelStages
# Load production model
production_model = Model(
name = "iris_classifier" ,
version = ModelStages. PRODUCTION
)
# Load specific version
specific_model = Model(
name = "iris_classifier" ,
version = "1.2.0"
)
Loading Artifacts from Model
from zenml import Model
model = Model(
name = "iris_classifier" ,
version = "production"
)
# Load a specific artifact
training_data = model.load_artifact( "training_data" )
model_weights = model.load_artifact( "model" , version = "3" )
from zenml import step, Model
@step ( model = Model( name = "my_model" ))
def train_model ():
# Training code
accuracy = 0.95
from zenml import get_step_context
context = get_step_context()
context.model.log_metadata({
"accuracy" : accuracy,
"framework" : "sklearn" ,
"dataset_size" : 1000
})
from zenml import Model, ModelStages
model = Model(
name = "iris_classifier" ,
version = "1.5.0"
)
# Promote to staging
model.set_stage(ModelStages. STAGING )
# Promote to production (force replace current production model)
model.set_stage(ModelStages. PRODUCTION , force = True )
@pipeline Configure model in pipelines
@step Configure model in steps
log_model_metadata Log model metadata
link_artifact_to_model Link artifacts to models