Skip to content

Commit 8202d9f

Browse files
committed
add the model version set file
1 parent 3b5bcab commit 8202d9f

File tree

2 files changed

+300
-1
lines changed

2 files changed

+300
-1
lines changed

docs/source/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ 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
6160

6261
.. toctree::
6362
:hidden:
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
Model Version Set
2+
*****************
3+
4+
Overview
5+
________
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
14+
Quick Start
15+
___________
16+
17+
The following creates a model and model version set, and then performs some common operations on the model version set:
18+
19+
.. code-block:: python3
20+
21+
import tempfile
22+
from ads.model import SklearnModel
23+
from ads.model import ModelVersionSet
24+
from sklearn.datasets import load_iris
25+
from sklearn.linear_model import LogisticRegression
26+
from sklearn.model_selection import train_test_split
27+
28+
# Create a model version set
29+
mvs = ModelVersionSet(
30+
name = "my_test_model_version_set",
31+
description = "A test creating the model version set using ModelVersionSet")
32+
mvs.create()
33+
34+
# Create a Sklearn model
35+
iris = load_iris()
36+
X, y = iris.data, iris.target
37+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
38+
sklearn_estimator = LogisticRegression()
39+
sklearn_estimator.fit(X_train, y_train)
40+
41+
42+
# Create an SklearnModel object
43+
sklearn_model = SklearnModel(estimator=sklearn_estimator, artifact_dir=tempfile.mkdtemp())
44+
sklearn_model.prepare(inference_conda_env="dbexp_p38_cpu_v1")
45+
46+
# Save the model and add it to the model version set
47+
model_id = sklearn_model.save(
48+
display_name="Quickstart model",
49+
model_version_set=mvs,
50+
version_label="Version 1")
51+
52+
# Print a list of models in the model version set
53+
for item in ModelVersionSet.list():
54+
print(item)
55+
print("---------")
56+
57+
# Update the model version set
58+
mvs.description = "Updated description of the model version set"
59+
mvs.update()
60+
61+
# Delete the model version set and associated models
62+
# mvs.delete(delete_model=True)
63+
64+
Associate a Model
65+
_________________
66+
67+
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.
68+
69+
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.
70+
71+
There are a number of ways to associate a model with a model version set. Which approach you use depends on the workflow.
72+
73+
ModelVersionSet Object
74+
----------------------
75+
76+
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.
77+
78+
.. code-block:: python3
79+
80+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
81+
mvs.model_add(<your_model_id>, version_label="Version 1")
82+
83+
Model Serialization
84+
-------------------
85+
86+
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.
87+
88+
The ``model_version_set`` parameter accepts a model version set's OCID or name. The parameter also accepts a ``ModelVersionSet`` object.
89+
90+
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.
91+
92+
.. code-block:: python3
93+
94+
model.save(
95+
display_name='Model attached to a model version set',
96+
version_label = "Version 1",
97+
model_version_set="<model_version_set_id>"
98+
)
99+
100+
101+
Context Manager
102+
---------------
103+
104+
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.
105+
106+
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.
107+
108+
.. code-block:: python3
109+
110+
with ads.model.experiment(name="my_model_version_set", create_if_not_exists=True):
111+
# experiment 1
112+
model_1.save(
113+
display_name='Generic Model Experiment 1',
114+
version_label = "Experiment 1"
115+
)
116+
117+
# experiment 2
118+
model_2.save(
119+
display_name='Generic Model Experiment 2',
120+
version_label = "Experiment 2"
121+
)
122+
123+
# experiment 3
124+
model_3.save(
125+
display_name='Generic Model Experiment 3',
126+
version_label = "Experiment 3"
127+
)
128+
129+
Create
130+
______
131+
132+
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.
133+
134+
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.
135+
136+
137+
Parameter-based Pattern
138+
^^^^^^^^^^^^^^^^^^^^^^^
139+
140+
.. code-block:: python3
141+
142+
mvs = ModelVersionSet(
143+
compartment_id = os.environ["PROJECT_COMPARTMENT_OCID"],
144+
name = "my_model_version_set",
145+
projectId = os.environ["PROJECT_OCID"],
146+
description = "Sample model version set")
147+
mvs.create()
148+
149+
150+
Builder Pattern
151+
^^^^^^^^^^^^^^^
152+
153+
.. code-block:: python3
154+
155+
mvs = (ModelVersionSet()
156+
.with_compartment_id(os.environ["PROJECT_COMPARTMENT_OCID"])
157+
.with_project_id(os.environ["PROJECT_OCID"])
158+
.with_name("my_model_version_set")
159+
.with_description("Sample model version set"))
160+
mvs.create()
161+
162+
163+
Delete
164+
______
165+
166+
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.
167+
168+
169+
The following example deletes a model version set and its associated models.
170+
171+
.. code-block: python3
172+
173+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
174+
mvs.delete(delete_model=True)
175+
176+
177+
The ``status`` property has the following values:
178+
179+
* ``ModelVersionSet.LIFECYCLE_STATE_ACTIVE``
180+
* ``ModelVersionSet.LIFECYCLE_STATE_DELETED``
181+
* ``ModelVersionSet.LIFECYCLE_STATE_DELETING``
182+
* ``ModelVersionSet.LIFECYCLE_STATE_FAILED``
183+
184+
Download
185+
________
186+
187+
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.
188+
189+
``.from_id()``
190+
^^^^^^^^^^^^^^
191+
192+
.. code-block:: python3
193+
194+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
195+
196+
197+
``.from_name()``
198+
^^^^^^^^^^^^^^^^
199+
200+
.. code-block:: python3
201+
202+
mvs = ModelVersionSet.from_name(name="<model_version_set_name>")
203+
204+
List
205+
____
206+
207+
ModelVersionSet
208+
---------------
209+
210+
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.
211+
212+
The following example uses context manager to iterate over the collection of model version sets:
213+
214+
.. code-block:: python3
215+
216+
for model_version_set in ModelVersionSet.list():
217+
print(model_version_set)
218+
print("---------")
219+
220+
221+
Model
222+
-----
223+
224+
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.
225+
226+
.. code-block:: python3
227+
228+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
229+
models = mvs.models()
230+
231+
for dsc_model in models:
232+
print(dsc_model.display_name, dsc_model.id, dsc_model.status)
233+
234+
235+
Update
236+
______
237+
238+
ModelVersionSet Properties
239+
--------------------------
240+
241+
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.
242+
243+
The properties that you can be update are:
244+
245+
* ``compartment_id``: The OCID of the compartment that the model version set belongs to.
246+
* ``description``: A description of the models in the collection.
247+
* ``freeform_tags``: A dictionary of string values.
248+
* ``name``: Name of the model version set.
249+
* ``project_id``: The OCID of the data science project that the model version set belongs to.
250+
251+
The following demonstrates how to update these values of a model version set using the various API interfaces:
252+
253+
Parameter-based Pattern
254+
^^^^^^^^^^^^^^^^^^^^^^^
255+
256+
.. code-block:: python3
257+
258+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
259+
mvs.compartement_id = os.environ["PROJECT_COMPARTMENT_OCID"]
260+
mvs.description = "An updated description"
261+
mvs.freeform_tags = {'label_1': 'value 1', 'label_2': 'value 2'}
262+
mvs.name = "new_set_name"
263+
mvs.project_id = os.environ["PROJECT_OCID"]
264+
mvs.update()
265+
266+
Builder Pattern
267+
^^^^^^^^^^^^^^^
268+
269+
.. code-block:: python3
270+
271+
mvs = ModelVersionSet.from_id(id="<model_version_set_id>")
272+
mvs = (mvs.with_compartment_id(os.environ["PROJECT_COMPARTMENT_OCID"])
273+
.with_description("An updated description")
274+
.with_freeform_tags(label_1="value 1", label_2="value 2")
275+
.with_name("new_set_name")
276+
.with_project_id(os.environ["PROJECT_OCID"])
277+
.update())
278+
279+
280+
Version Label
281+
-------------
282+
283+
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.
284+
285+
The following example gets a registered ``Model`` object by model's OCID. Then, the object updates the version label property.
286+
287+
.. code-block:: python3
288+
289+
from ads.model import LightGBMModel
290+
291+
lgbm_model = LightGBMModel.from_id(
292+
"ocid1.datasciencemodel.oc1.xxx.xxxxx",
293+
model_file_name="model.joblib",
294+
artifact_dir="lgbm-download-test",
295+
force_overwrite=True,
296+
)
297+
298+
lgbm_model.update(version_label="MyNewVersionLabel")
299+
300+

0 commit comments

Comments
 (0)