Skip to content

Commit d4b564e

Browse files
authored
Merge pull request #33 from oracle/ODSC-36680/mvs_user_guide
ODSC-36680: [MVS][User Guide] Add Model Version Set user guide.
2 parents 88afde9 + 240acb1 commit d4b564e

File tree

10 files changed

+315
-0
lines changed

10 files changed

+315
-0
lines changed

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Oracle Accelerated Data Science SDK (ADS)
5757
user_guide/jobs/index
5858
user_guide/logs/logs
5959
user_guide/secrets/index
60+
user_guide/model_version_set/index
6061

6162
.. toctree::
6263
:hidden:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Associate a Model
2+
_________________
3+
4+
Model version sets are a collection of models. After a model is associated with a model version set, the model can't be associated with a different model version set. Further, the model can't be disassociated with the model version set.
5+
6+
When a model is associated with a model version set, a version label can be assigned to the set. This version is different than the model version that is maintained by the model version set.
7+
8+
There are a number of ways to associate a model with a model version set. Which approach you use depends on the workflow.
9+
10+
ModelVersionSet Object
11+
----------------------
12+
13+
For a model not associated with a model version set, use the ``.model_add()`` method on a ``ModelVersionSet`` object to associate the model with the model version set. The ``.model_add()`` requires that you provide the model OCID and optionally a version label.
14+
15+
.. code-block:: python3
16+
17+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
18+
mvs.model_add(<your_model_id>, version_label="Version 1")
19+
20+
Model Serialization
21+
-------------------
22+
23+
The :ref:`Model Serialization` classes allow a model to be associated with a model version set at the time that it is saved to the model catalog. You do this with the ``model_version_set`` parameter in the ``.save()`` method. In addition, you can add the model's version label with the ``version_label`` parameter.
24+
25+
The ``model_version_set`` parameter accepts a model version set's OCID or name. The parameter also accepts a ``ModelVersionSet`` object.
26+
27+
In the following, the ``model`` variable is a :ref:`Model Serialization` object that is to be saved to the model catalog, and at the same time associated with a model version set.
28+
29+
.. code-block:: python3
30+
31+
model.save(
32+
display_name='Model attached to a model version set',
33+
version_label = "Version 1",
34+
model_version_set="<model_version_set_id>"
35+
)
36+
37+
38+
Context Manager
39+
---------------
40+
41+
To associate several models with a model version set, use a context manager. The ``ads.model.experiment()`` method requires a ``name`` parameter. If the model catalog has a matching model version set name, the model catalog uses that model version set. If the parameter ``create_if_not_exists`` is ``True``, the ``experiment()`` method attempts to match the model version set name with name in the model catalog. If the name does not exist, the method creates a new model version set.
42+
43+
Within the context manager, you can save multiple :ref:`Model Serialization` models without specifying the ``model_version_set`` parameter because it's taken from the model context manager. The following example assumes that ``model_1``, ``model_2``, and ``model_3`` are :ref:`Model Serialization` objects. If the model version set doesn't exist in the model catalog, the example creates a model version set named ``my_model_version_set``. If the model version set exists in the model catalog, the models are saved to that model version set.
44+
45+
.. code-block:: python3
46+
47+
with ads.model.experiment(name="my_model_version_set", create_if_not_exists=True):
48+
# experiment 1
49+
model_1.save(
50+
display_name='Generic Model Experiment 1',
51+
version_label = "Experiment 1"
52+
)
53+
54+
# experiment 2
55+
model_2.save(
56+
display_name='Generic Model Experiment 2',
57+
version_label = "Experiment 2"
58+
)
59+
60+
# experiment 3
61+
model_3.save(
62+
display_name='Generic Model Experiment 3',
63+
version_label = "Experiment 3"
64+
)
65+
66+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Create
2+
______
3+
4+
The ``.create()`` method on a ``ModelVersionSet`` object creates a model version set in the model catalog. The properties of the ``ModelVersionSet`` are used to create the model version set in the model catalog.
5+
6+
The following examples create a ``ModelVersionSet``, define the properties of the model version set, and then create a model version set in the model catalog.
7+
8+
9+
Parameter-based Pattern
10+
^^^^^^^^^^^^^^^^^^^^^^^
11+
12+
.. code-block:: python3
13+
14+
mvs = ModelVersionSet(
15+
compartment_id = os.environ["PROJECT_COMPARTMENT_OCID"],
16+
name = "my_model_version_set",
17+
projectId = os.environ["PROJECT_OCID"],
18+
description = "Sample model version set")
19+
mvs.create()
20+
21+
22+
Builder Pattern
23+
^^^^^^^^^^^^^^^
24+
25+
.. code-block:: python3
26+
27+
mvs = (ModelVersionSet()
28+
.with_compartment_id(os.environ["PROJECT_COMPARTMENT_OCID"])
29+
.with_project_id(os.environ["PROJECT_OCID"])
30+
.with_name("my_model_version_set")
31+
.with_description("Sample model version set"))
32+
mvs.create()
33+
34+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Delete
2+
______
3+
4+
To delete a model version set, all the associated models must be deleted or in a terminated state. You can set the ``delete_model`` parameter to ``True`` to delete all of the models in the model version set, and then delete the model version set. The ``.delete()`` method on a ``ModelVersionSet`` object initiates an asynchronous delete operation. You can check the ``.status`` method on the ``ModelVersionSet`` object to determine the status of the delete request.
5+
6+
7+
The following example deletes a model version set and its associated models.
8+
9+
.. code-block: python3
10+
11+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
12+
mvs.delete(delete_model=True)
13+
14+
15+
The ``status`` property has the following values:
16+
17+
* ``ModelVersionSet.LIFECYCLE_STATE_ACTIVE``
18+
* ``ModelVersionSet.LIFECYCLE_STATE_DELETED``
19+
* ``ModelVersionSet.LIFECYCLE_STATE_DELETING``
20+
* ``ModelVersionSet.LIFECYCLE_STATE_FAILED``
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Download
2+
________
3+
4+
Create a ``ModelVersionSet`` object by downloading the metadata from the model catalog. The ``ModelVersionSet`` class has a ``.from_id()`` method that accepts the model version set OCID. The ``.from_name()`` method takes the name of the model version set.
5+
6+
``.from_id()``
7+
^^^^^^^^^^^^^^
8+
9+
.. code-block:: python3
10+
11+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
12+
13+
14+
``.from_name()``
15+
^^^^^^^^^^^^^^^^
16+
17+
.. code-block:: python3
18+
19+
mvs = ModelVersionSet.from_name(name="<model_version_set_name>")
20+
21+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.. _model version set:
2+
3+
###################
4+
Model Version Set
5+
###################
6+
7+
.. versionadded:: 2.7.1
8+
9+
.. toctree::
10+
:maxdepth: 1
11+
12+
overview
13+
quick_start
14+
associate_a_model
15+
create
16+
delete
17+
download
18+
list
19+
update
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
List
2+
____
3+
4+
ModelVersionSet
5+
---------------
6+
7+
The ``.list()`` method on the ``ModelVersionSet`` class takes a compartment ID and lists the model version sets in that compartment. If the compartment isn't given, then the compartment of the notebook session is used.
8+
9+
The following example uses context manager to iterate over the collection of model version sets:
10+
11+
.. code-block:: python3
12+
13+
for model_version_set in ModelVersionSet.list():
14+
print(model_version_set)
15+
print("---------")
16+
17+
18+
Model
19+
-----
20+
21+
You can get the list of models associated with a model version set by calling the ``.models()`` method on a ``ModelVersionSet`` object. A list of models that are associated with that model version set is returned. First, you must obtain a ``ModelVersionSet`` object. Use the ``.from_id()`` method if you know the model version set OCID. Alternatively, use the ``.from_name()`` method if you know the name of the model version set.
22+
23+
.. code-block:: python3
24+
25+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
26+
models = mvs.models()
27+
28+
for dsc_model in models:
29+
print(dsc_model.display_name, dsc_model.id, dsc_model.status)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Overview
2+
________
3+
4+
The normal workflow of a data scientist is to create a model and push it into production. While in production the data scientist learns what the model is doing well and what it isn't. Using this information the data scientist creates an improved model. These models are linked using model version sets. A model version set is a collection of models that are related to each other. A model version set is a way to track the relationships between models. As a container, the model version set takes a collection of models. Those models are assigned a sequential version number based on the order they are entered into the model version set.
5+
6+
In ADS the class ``ModelVersionSet`` is used to represent the model version set. An object of ``ModelVersionSet`` references a model version set in the Data Science service. The ``ModelVersionSet`` class supports two APIs: the builder pattern and the traditional parameter-based pattern. You can use either of these API frameworks interchangeably and examples for both patterns are included.
7+
8+
Use the ``.create()`` method to create a model version set in your tenancy. If the model version set already exists in the model catalog, use the ``.from_id()`` or ``.from_name()`` method to get a ``ModelVersionSet`` object based on the specified model version set. If you make changes to the metadata associated with the model version set, use the ``.update()`` method to push those changes to the model catalog. The ``.list()`` method lists all model version sets. To add an existing model to a model version set, use the ``.add_model()`` method. The ``.models()`` method lists the models in the model version set. Use the ``.delete()`` method to delete a model version set from the model catalog.
9+
10+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Quick Start
2+
___________
3+
4+
The following creates a model and model version set, and then performs some common operations on the model version set:
5+
6+
.. code-block:: python3
7+
8+
import tempfile
9+
from ads.model import SklearnModel
10+
from ads.model import ModelVersionSet
11+
from sklearn.datasets import load_iris
12+
from sklearn.linear_model import LogisticRegression
13+
from sklearn.model_selection import train_test_split
14+
15+
# Create a model version set
16+
mvs = ModelVersionSet(
17+
name = "my_test_model_version_set",
18+
description = "A test creating the model version set using ModelVersionSet")
19+
mvs.create()
20+
21+
# Create a Sklearn model
22+
iris = load_iris()
23+
X, y = iris.data, iris.target
24+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
25+
sklearn_estimator = LogisticRegression()
26+
sklearn_estimator.fit(X_train, y_train)
27+
28+
29+
# Create an SklearnModel object
30+
sklearn_model = SklearnModel(estimator=sklearn_estimator, artifact_dir=tempfile.mkdtemp())
31+
sklearn_model.prepare(inference_conda_env="dbexp_p38_cpu_v1")
32+
33+
# Save the model and add it to the model version set
34+
model_id = sklearn_model.save(
35+
display_name="Quickstart model",
36+
model_version_set=mvs,
37+
version_label="Version 1")
38+
39+
# Print a list of models in the model version set
40+
for item in ModelVersionSet.list():
41+
print(item)
42+
print("---------")
43+
44+
# Update the model version set
45+
mvs.description = "Updated description of the model version set"
46+
mvs.update()
47+
48+
# Delete the model version set and associated models
49+
# mvs.delete(delete_model=True)
50+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Update
2+
______
3+
4+
ModelVersionSet Properties
5+
--------------------------
6+
7+
The ``ModelVersionSet`` object has a number of properties that you can be update. When the properties in a ``ModelVersionSet`` object are updated, the model version set in the model catalog are not automatically updated. You must call the ``.update()`` method to commit the changes.
8+
9+
The properties that you can be update are:
10+
11+
* ``compartment_id``: The OCID of the compartment that the model version set belongs to.
12+
* ``description``: A description of the models in the collection.
13+
* ``freeform_tags``: A dictionary of string values.
14+
* ``name``: Name of the model version set.
15+
* ``project_id``: The OCID of the data science project that the model version set belongs to.
16+
17+
The following demonstrates how to update these values of a model version set using the various API interfaces:
18+
19+
Parameter-based Pattern
20+
^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
.. code-block:: python3
23+
24+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
25+
mvs.compartement_id = os.environ["PROJECT_COMPARTMENT_OCID"]
26+
mvs.description = "An updated description"
27+
mvs.freeform_tags = {'label_1': 'value 1', 'label_2': 'value 2'}
28+
mvs.name = "new_set_name"
29+
mvs.project_id = os.environ["PROJECT_OCID"]
30+
mvs.update()
31+
32+
Builder Pattern
33+
^^^^^^^^^^^^^^^
34+
35+
.. code-block:: python3
36+
37+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
38+
mvs = (mvs.with_compartment_id(os.environ["PROJECT_COMPARTMENT_OCID"])
39+
.with_description("An updated description")
40+
.with_freeform_tags(label_1="value 1", label_2="value 2")
41+
.with_name("new_set_name")
42+
.with_project_id(os.environ["PROJECT_OCID"])
43+
.update())
44+
45+
46+
Version Label
47+
-------------
48+
49+
The version label is associated with the model, and not the model version set. To change the version label, you must have a ``Model`` object. Then, you can change the ``version_label`` for the registered model.
50+
51+
The following example gets a registered ``Model`` object by model's OCID. Then, the object updates the version label property.
52+
53+
.. code-block:: python3
54+
55+
from ads.model import LightGBMModel
56+
57+
lgbm_model = LightGBMModel.from_id(
58+
"ocid1.datasciencemodel.oc1.xxx.xxxxx",
59+
model_file_name="model.joblib",
60+
artifact_dir="lgbm-download-test",
61+
force_overwrite=True,
62+
)
63+
64+
lgbm_model.update(version_label="MyNewVersionLabel")

0 commit comments

Comments
 (0)