|
8 | 8 |
|
9 | 9 | The ``GenericModel`` class in ADS provides an efficient way to serialize almost any model class. This section demonstrates how to use the ``GenericModel`` class to prepare model artifacts, verify models, save models to the model catalog, deploy models, and perform predictions on model deployment endpoints.
|
10 | 10 |
|
11 |
| -The ``GenericModel`` class works with any unsupported model framework that has a ``.predict()`` method. For the most common model classes such as scikit-learn, XGBoost, LightGBM, TensorFlow, and PyTorch, and AutoML, we recommend that you use the ADS provided, framework-specific serializations models. For example, for a scikit-learn model, use SKLearnmodel. For other models, use the ``GenericModel`` class. |
| 11 | +The ``GenericModel`` class works with any unsupported model framework that has a ``.predict()`` method. For the most common model classes such as scikit-learn, XGBoost, LightGBM, TensorFlow, and PyTorch, we recommend that you use the ADS provided, framework-specific serializations models. For example, for a scikit-learn model, use SKLearnmodel. For other models, use the ``GenericModel`` class. |
12 | 12 |
|
13 | 13 | .. include:: _template/overview.rst
|
14 | 14 |
|
@@ -190,83 +190,46 @@ By default, the ``GenericModel`` serializes to a pickle file. The following exam
|
190 | 190 | catboost_model.delete_deployment(wait_for_completion=True)
|
191 | 191 | catboost_model.delete() # delete the model
|
192 | 192 |
|
193 |
| -You can also use the shortcut ``.prepare_save_deploy()`` instead of calling ``.prepare()``, ``.save()`` and ``.deploy()`` seperately. |
194 |
| - |
195 |
| -.. code-block:: python3 |
196 |
| -
|
197 |
| - import tempfile |
198 |
| - from ads.catalog.model import ModelCatalog |
199 |
| - from ads.model.generic_model import GenericModel |
200 |
| -
|
201 |
| - class Toy: |
202 |
| - def predict(self, x): |
203 |
| - return x ** 2 |
204 |
| - estimator = Toy() |
205 |
| -
|
206 |
| - model = GenericModel(estimator=estimator) |
207 |
| - model.summary_status() |
208 |
| - # If you are running the code inside a notebook session and using a service pack, `inference_conda_env` can be omitted. |
209 |
| - model.prepare_save_deploy(inference_conda_env="dataexpl_p37_cpu_v3") |
210 |
| - model.verify(2) |
211 |
| - model.predict(2) |
212 |
| - model.delete_deployment(wait_for_completion=True) |
213 |
| - ModelCatalog(compartment_id=os.environ['NB_SESSION_COMPARTMENT_OCID']).delete_model(model.model_id) |
214 |
| -
|
215 | 193 |
|
216 | 194 | Example -- Save Your Own Model
|
217 | 195 | ==============================
|
218 | 196 |
|
219 | 197 | By default, the ``serialize`` in ``GenericModel`` class is True, and it will serialize the model using cloudpickle. However, you can set ``serialize=False`` to disable it. And serialize the model on your own. You just need to copy the serialized model into the ``.artifact_dir``. This example shows step by step how you can do that.
|
220 |
| -The example is illustrated using an AutoMLx model. |
| 198 | +The example is illustrated using a Sklearn model. |
221 | 199 |
|
222 | 200 | .. code-block:: python3
|
223 | 201 |
|
224 |
| - import automl |
225 |
| - import ads |
226 |
| - from automl import init |
227 |
| - from sklearn.datasets import fetch_openml |
228 |
| - from sklearn.model_selection import train_test_split |
| 202 | + import tempfile |
| 203 | + from ads import set_auth |
229 | 204 | from ads.model import GenericModel
|
| 205 | + from sklearn.datasets import load_iris |
| 206 | + from sklearn.linear_model import LogisticRegression |
| 207 | + from sklearn.model_selection import train_test_split |
230 | 208 |
|
231 |
| - dataset = fetch_openml(name='adult', as_frame=True) |
232 |
| - df, y = dataset.data, dataset.target |
233 |
| -
|
234 |
| - # Several of the columns are incorrectly labeled as category type in the original dataset |
235 |
| - numeric_columns = ['age', 'capitalgain', 'capitalloss', 'hoursperweek'] |
236 |
| - for col in df.columns: |
237 |
| - if col in numeric_columns: |
238 |
| - df[col] = df[col].astype(int) |
239 |
| - |
240 |
| -
|
241 |
| - X_train, X_test, y_train, y_test = train_test_split(df, |
242 |
| - y.map({'>50K': 1, '<=50K': 0}).astype(int), |
243 |
| - train_size=0.7, |
244 |
| - random_state=0) |
245 |
| -
|
246 |
| - X_train.shape, X_test.shape |
247 |
| -
|
248 |
| - # create a AutoMLx model |
249 |
| - init(engine='local') |
| 209 | + set_auth(auth="resource_principal") |
250 | 210 |
|
251 |
| - est = automl.Pipeline(task='classification') |
252 |
| - est.fit(X_train, y_train) |
| 211 | + # Load dataset and Prepare train and test split |
| 212 | + iris = load_iris() |
| 213 | + X, y = iris.data, iris.target |
| 214 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) |
253 | 215 |
|
254 |
| - # Authentication |
255 |
| - ads.set_auth(auth="resource_principal") |
| 216 | + # Train a LogisticRegression model |
| 217 | + sklearn_estimator = LogisticRegression() |
| 218 | + sklearn_estimator.fit(X_train, y_train) |
256 | 219 |
|
257 | 220 | # Serialize your model. You can choose your own way to serialize your model.
|
258 | 221 | import cloudpickle
|
259 | 222 | with open("./model.pkl", "wb") as f:
|
260 |
| - cloudpickle.dump(est, f) |
| 223 | + cloudpickle.dump(sklearn_estimator, f) |
261 | 224 |
|
262 |
| - model = GenericModel(est, artifact_dir = "model_artifact_folder", serialize=False) |
263 |
| - model.prepare(inference_conda_env="automlx_p38_cpu_v1",force_overwrite=True, model_file_name="model.pkl", X_sample=X_test) |
| 225 | + model = GenericModel(sklearn_estimator, artifact_dir = "model_artifact_folder", serialize=False) |
| 226 | + model.prepare(inference_conda_env="generalml_p38_cpu_v1",force_overwrite=True, model_file_name="model.pkl", X_sample=X_test) |
264 | 227 |
|
265 |
| -Now copy the model.pkl file and paste into the ``model_artifact_folder`` folder. And open the score.py in the ``model_artifact_folder`` folder and add implement the ``load_model`` function. You can also edit ``pre_inference`` and ``post_inference`` function. Below is an example implementation of the score.py. |
| 228 | +Now copy the model.pkl file and paste into the ``model_artifact_folder`` folder. And open the score.py in the ``model_artifact_folder`` folder to add implementation of the ``load_model`` function. You can also add your preprocessing steps in ``pre_inference`` function and postprocessing steps in ``post_inference`` function. Below is an example implementation of the score.py. |
266 | 229 | Replace your score.py with the code below.
|
267 | 230 |
|
268 | 231 | .. code-block:: python3
|
269 |
| - :emphasize-lines: 28, 29, 30, 31, 122 |
| 232 | + :emphasize-lines: 28, 29, 30, 31, 123 |
270 | 233 |
|
271 | 234 | # score.py 1.0 generated by ADS 2.8.2 on 20230301_065458
|
272 | 235 | import os
|
@@ -414,16 +377,38 @@ Replace your score.py with the code below.
|
414 | 377 | )
|
415 | 378 | return {'prediction': yhat}
|
416 | 379 |
|
417 |
| -Save the score.py and now call verify to check if it works locally. |
| 380 | +Save the score.py and now call ``.verify()`` to check if it works locally. |
418 | 381 |
|
419 | 382 | .. code-block:: python3
|
420 | 383 |
|
421 |
| - model.verify(X_test.iloc[:2], auto_serialize_data=True) |
| 384 | + model.verify(X_test[:2], auto_serialize_data=True) |
422 | 385 |
|
423 | 386 | After verify run successfully, you can save the model to model catalog, deploy and call predict to invoke the endpoint.
|
424 | 387 |
|
425 | 388 | .. code-block:: python3
|
426 | 389 |
|
427 |
| - model_id = model.save(display_name='Demo AutoMLModel model') |
428 |
| - deploy = model.deploy(display_name='Demo AutoMLModel deployment') |
429 |
| - model.predict(X_test.iloc[:2].to_json()) |
| 390 | + model_id = model.save(display_name='Demo Sklearn model') |
| 391 | + deploy = model.deploy(display_name='Demo Sklearn deployment') |
| 392 | + model.predict(X_test[:2].tolist()) |
| 393 | +
|
| 394 | +You can also use the shortcut ``.prepare_save_deploy()`` instead of calling ``.prepare()``, ``.save()`` and ``.deploy()`` seperately. |
| 395 | + |
| 396 | +.. code-block:: python3 |
| 397 | +
|
| 398 | + import tempfile |
| 399 | + from ads.catalog.model import ModelCatalog |
| 400 | + from ads.model.generic_model import GenericModel |
| 401 | +
|
| 402 | + class Toy: |
| 403 | + def predict(self, x): |
| 404 | + return x ** 2 |
| 405 | + estimator = Toy() |
| 406 | +
|
| 407 | + model = GenericModel(estimator=estimator) |
| 408 | + model.summary_status() |
| 409 | + # If you are running the code inside a notebook session and using a service pack, `inference_conda_env` can be omitted. |
| 410 | + model.prepare_save_deploy(inference_conda_env="dataexpl_p37_cpu_v3") |
| 411 | + model.verify(2) |
| 412 | + model.predict(2) |
| 413 | + model.delete_deployment(wait_for_completion=True) |
| 414 | + model.delete() |
0 commit comments