 {"metrics": {"accuracy": accuracy}}

return modelFor further depth, there is an advanced metadata logging guide that goes more into detail about logging metadata in ZenML.

Additionally, there is a lot more to learn about artifacts within ZenML. Please read the dedicated data management guide for more information.

Code example

This section combines all the code from this section into one simple script that you can use easily:

from typing import Optional, Tuple

from typing_extensions import Annotated

import numpy as np

from sklearn.base import ClassifierMixin

from sklearn.datasets import load_digits

from sklearn.svm import SVC

from zenml import ArtifactConfig, pipeline, step, log_artifact_metadata

from zenml import save_artifact, load_artifact

from zenml.client import Client

@step

def versioned_data_loader_step() -> (

Annotated[

Tuple[np.ndarray, np.ndarray],

ArtifactConfig(

name="my_dataset",

tags=["digits", "computer vision", "classification"],

),

):

"""Loads the digits dataset as a tuple of flattened numpy arrays."""

digits = load_digits()

return (digits.images.reshape((len(digits.images), -1)), digits.target)

@step

def model_finetuner_step(

model: ClassifierMixin, dataset: Tuple[np.ndarray, np.ndarray]

) -> Annotated[

ClassifierMixin,

ArtifactConfig(name="my_model", is_model_artifact=True, tags=["SVC", "trained"]),

]:

"""Finetunes a given model on a given dataset."""

model.fit(dataset[0], dataset[1])

accuracy = model.score(dataset[0], dataset[1])

log_artifact_metadata(metadata={"accuracy": float(accuracy)})

return model

@pipeline

def model_finetuning_pipeline(

dataset_version: Optional[str] = None,

model_version: Optional[str] = None,

):

client = Client()

# Either load a previous version of "my_dataset" or create a new one

if dataset_version:

dataset = client.get_artifact_version(

name_id_or_prefix="my_dataset", version=dataset_version

else:

dataset = versioned_data_loader_step()

# Load the model to finetune