ads.model.framework package
Submodules
ads.model.framework.automl_model module
- class ads.model.framework.automl_model.AutoMLModel(estimator: Callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Optional[Dict] = None, **kwargs)
Bases:
FrameworkSpecificModel
AutoMLModel class for estimators from AutoML framework.
- algorithm
“ensemble”, the algorithm name of the model.
- Type:
str
- artifact_dir
Artifact directory to store the files needed for deployment.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained automl estimator/model using oracle automl.
- Type:
Callable
- framework
“oracle_automl”, the framework name of the estimator.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model. Default to “model.pkl”.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> import tempfile >>> import logging >>> import warnings >>> from ads.automl.driver import AutoML >>> from ads.automl.provider import OracleAutoMLProvider >>> from ads.dataset.dataset_browser import DatasetBrowser >>> from ads.model.framework.automl_model import AutoMLModel >>> from ads.common.model_metadata import UseCaseType >>> ds = DatasetBrowser.sklearn().open("wine").set_target("target") >>> train, test = ds.train_test_split(test_size=0.1, random_state = 42)
>>> ml_engine = OracleAutoMLProvider(n_jobs=-1, loglevel=logging.ERROR) >>> oracle_automl = AutoML(train, provider=ml_engine) >>> model, baseline = oracle_automl.train( ... model_list=['LogisticRegression', 'DecisionTreeClassifier'], ... random_state = 42, ... time_budget = 500 ... )
>>> automl_model.prepare(inference_conda_env=inference_conda_env, force_overwrite=True) >>> automl_model.verify(...) >>> automl_model.save() >>> model_deployment = automl_model.deploy(wait_for_completion=False)
Initiates a AutoMLModel instance.
- Parameters:
estimator (Callable) – Any model object generated by automl framework.
artifact_dir (str) – Directory for generate artifact.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
AutoMLModel instance.
- Return type:
- Raises:
TypeError – If the input model is not an AutoML model.
- serialize_model(force_overwrite: Optional[bool] = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs: Dict)
Serialize and save AutoML model using pkl.
- Parameters:
force_overwrite ((bool, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate input schema.
- Returns:
Nothing.
- Return type:
None
ads.model.framework.lightgbm_model module
- class ads.model.framework.lightgbm_model.LightGBMModel(estimator: Callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Optional[Dict] = None, **kwargs)
Bases:
FrameworkSpecificModel
LightGBMModel class for estimators from Lightgbm framework.
- algorithm
The algorithm of the model.
- Type:
str
- artifact_dir
Artifact directory to store the files needed for deployment.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained lightgbm estimator/model using Lightgbm.
- Type:
Callable
- framework
“lightgbm”, the framework name of the model.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> import lightgbm as lgb >>> import tempfile >>> from sklearn.model_selection import train_test_split >>> from sklearn.datasets import load_iris >>> from ads.model.framework.lightgbm_model import LightGBMModel
>>> iris = load_iris() >>> X, y = iris.data, iris.target
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> train = lgb.Dataset(X_train, label=y_train) >>> param = { ... 'objective': 'multiclass', 'num_class': 3, ... } >>> lightgbm_estimator = lgb.train(param, train)
>>> lightgbm_model = LightGBMModel(estimator=lightgbm_estimator, ... artifact_dir=tempfile.mkdtemp())
>>> lightgbm_model.prepare(inference_conda_env="generalml_p37_cpu_v1", force_overwrite=True) >>> lightgbm_model.reload() >>> lightgbm_model.verify(X_test) >>> lightgbm_model.save() >>> model_deployment = lightgbm_model.deploy(wait_for_completion=False) >>> lightgbm_model.predict(X_test)
Initiates a LightGBMModel instance. This class wraps the Lightgbm model as estimator. It’s primary purpose is to hold the trained model and do serialization.
- Parameters:
estimator – any model object generated by Lightgbm framework
artifact_dir (str) – Directory for generate artifact.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
LightGBMModel instance.
- Return type:
- Raises:
TypeError – If the input model is not a Lightgbm model or not supported for serialization.:
Examples
>>> import lightgbm as lgb >>> import tempfile >>> from sklearn.model_selection import train_test_split >>> from sklearn.datasets import load_iris >>> from ads.model.framework.lightgbm_model import LightGBMModel >>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> train = lgb.Dataset(X_train, label=y_train) >>> param = { ... 'objective': 'multiclass', 'num_class': 3, ... } >>> lightgbm_estimator = lgb.train(param, train) >>> lightgbm_model = LightGBMModel(estimator=lightgbm_estimator, artifact_dir=tempfile.mkdtemp()) >>> lightgbm_model.prepare(inference_conda_env="generalml_p37_cpu_v1") >>> lightgbm_model.verify(X_test) >>> lightgbm_model.save() >>> model_deployment = lightgbm_model.deploy() >>> lightgbm_model.predict(X_test) >>> lightgbm_model.delete_deployment()
- generate_initial_types(X_sample: Any) List
Auto generate intial types.
- Parameters:
X_sample ((Any)) – Train data.
- Returns:
Initial types.
- Return type:
List
- serialize_model(as_onnx: bool = False, initial_types: Optional[List[Tuple]] = None, force_overwrite: bool = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs: Dict)
Serialize and save Lightgbm model using ONNX or model specific method.
- Parameters:
artifact_dir (str) – Directory for generate artifact.
as_onnx ((boolean, optional). Defaults to False.) – If set as True, provide initial_types or X_sample to convert into ONNX.
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
force_overwrite ((boolean, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
Nothing.
- Return type:
None
- to_onnx(initial_types: List[Tuple] = None, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs)
Produces an equivalent ONNX model of the given Lightgbm model.
- Parameters:
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
An ONNX model (type
- Return type:
ModelProto) which is equivalent to the input Lightgbm model.
ads.model.framework.pytorch_model module
- class ads.model.framework.pytorch_model.PyTorchModel(estimator: callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Dict = None, **kwargs)
Bases:
FrameworkSpecificModel
PyTorchModel class for estimators from Pytorch framework.
- algorithm
The algorithm of the model.
- Type:
str
- artifact_dir
Artifact directory to store the files needed for deployment.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained pytorch estimator/model using Pytorch.
- Type:
Callable
- framework
“pytorch”, the framework name of the model.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> torch_model = PyTorchModel(estimator=torch_estimator, ... artifact_dir=tmp_model_dir) >>> inference_conda_env = "generalml_p37_cpu_v1"
>>> torch_model.prepare(inference_conda_env=inference_conda_env, force_overwrite=True) >>> torch_model.reload() >>> torch_model.verify(...) >>> torch_model.save() >>> model_deployment = torch_model.deploy(wait_for_completion=False) >>> torch_model.predict(...)
Initiates a PyTorchModel instance.
- Parameters:
estimator (callable) – Any model object generated by pytorch framework
artifact_dir (str) – artifact directory to store the files needed for deployment.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
PyTorchModel instance.
- Return type:
- get_data_serializer(data: Union[Dict, str, List, ndarray, Series, DataFrame, torch.Tensor], data_type: str = None)
Returns serializable input data.
- Parameters:
data (Union[Dict, str, list, numpy.ndarray, pd.core.series.Series,) –
pd.core.frame.DataFrame – Data expected by the model deployment predict API.
torch.Tensor] – Data expected by the model deployment predict API.
data_type (str) – Type of the data.
- Returns:
A class containing serialized input data and original data type information.
- Return type:
InputDataSerializer
- Raises:
TypeError – if provided data type is not supported.
- serialize_model(as_onnx: bool = False, force_overwrite: bool = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, use_torch_script: bool = None, **kwargs) None
Serialize and save Pytorch model using ONNX or model specific method.
- Parameters:
as_onnx ((bool, optional). Defaults to False.) – If set as True, convert into ONNX model.
force_overwrite ((bool, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
X_sample (Union[list, tuple, pd.Series, np.ndarray, pd.DataFrame]. Defaults to None.) – A sample of input data that will be used to generate input schema and detect onnx_args.
use_torch_script ((bool, optional). Defaults to None (If the default value has not been changed, it will be set as False).) – If set as True, the model will be serialized as a TorchScript program. Check https://pytorch.org/tutorials/beginner/saving_loading_models.html#export-load-model-in-torchscript-format for more details. If set as False, it will only save the trained model’s learned parameters, and the score.py need to be modified to construct the model class instance first. Check https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-state-dict-recommended for more details.
**kwargs (optional params used to serialize pytorch model to onnx,) –
following (including the) – onnx_args: (tuple or torch.Tensor), default to None Contains model inputs such that model(onnx_args) is a valid invocation of the model. Can be structured either as: 1) ONLY A TUPLE OF ARGUMENTS; 2) A TENSOR; 3) A TUPLE OF ARGUMENTS ENDING WITH A DICTIONARY OF NAMED ARGUMENTS input_names: (List[str], optional). Names to assign to the input nodes of the graph, in order. output_names: (List[str], optional). Names to assign to the output nodes of the graph, in order. dynamic_axes: (dict, optional), default to None. Specify axes of tensors as dynamic (i.e. known only at run-time).
- Returns:
Nothing.
- Return type:
None
- to_onnx(path: str = None, onnx_args=None, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, input_names: List[str] = ['input'], output_names: List[str] = ['output'], dynamic_axes=None)
Exports the given Pytorch model into ONNX format.
- Parameters:
path (str, default to None) – Path to save the serialized model.
onnx_args ((tuple or torch.Tensor), default to None) – Contains model inputs such that model(onnx_args) is a valid invocation of the model. Can be structured either as: 1) ONLY A TUPLE OF ARGUMENTS; 2) A TENSOR; 3) A TUPLE OF ARGUMENTS ENDING WITH A DICTIONARY OF NAMED ARGUMENTS
X_sample (Union[list, tuple, pd.Series, np.ndarray, pd.DataFrame]. Defaults to None.) – A sample of input data that will be used to generate input schema and detect onnx_args.
input_names ((List[str], optional). Defaults to ["input"].) – Names to assign to the input nodes of the graph, in order.
output_names ((List[str], optional). Defaults to ["output"].) – Names to assign to the output nodes of the graph, in order.
dynamic_axes ((dict, optional). Defaults to None.) – Specify axes of tensors as dynamic (i.e. known only at run-time).
- Returns:
Nothing
- Return type:
None
- Raises:
AssertionError – if onnx module is not support by the current version of torch
ValueError – if X_sample is not provided if path is not provided
ads.model.framework.sklearn_model module
- class ads.model.framework.sklearn_model.SklearnModel(estimator: Callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Optional[Dict] = None, **kwargs)
Bases:
FrameworkSpecificModel
SklearnModel class for estimators from sklearn framework.
- algorithm
The algorithm of the model.
- Type:
str
- artifact_dir
Artifact directory to store the files needed for deployment.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained sklearn estimator/model using scikit-learn.
- Type:
Callable
- framework
“scikit-learn”, the framework name of the model.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> import tempfile >>> from sklearn.model_selection import train_test_split >>> from ads.model.framework.sklearn_model import SklearnModel >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import load_iris
>>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> sklearn_estimator = LogisticRegression() >>> sklearn_estimator.fit(X_train, y_train)
>>> sklearn_model = SklearnModel(estimator=sklearn_estimator, ... artifact_dir=tmp_model_dir)
>>> sklearn_model.prepare(inference_conda_env="generalml_p37_cpu_v1", force_overwrite=True) >>> sklearn_model.reload() >>> sklearn_model.verify(X_test) >>> sklearn_model.save() >>> model_deployment = sklearn_model.deploy(wait_for_completion=False) >>> sklearn_model.predict(X_test)
Initiates a SklearnModel instance.
- Parameters:
estimator (Callable) – Sklearn Model
artifact_dir (str) – Directory for generate artifact.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
SklearnModel instance.
- Return type:
Examples
>>> import tempfile >>> from sklearn.model_selection import train_test_split >>> from ads.model.framework.sklearn_model import SklearnModel >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.datasets import load_iris
>>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> sklearn_estimator = LogisticRegression() >>> sklearn_estimator.fit(X_train, y_train)
>>> sklearn_model = SklearnModel(estimator=sklearn_estimator, artifact_dir=tempfile.mkdtemp()) >>> sklearn_model.prepare(inference_conda_env="dataexpl_p37_cpu_v3") >>> sklearn_model.verify(X_test) >>> sklearn_model.save() >>> model_deployment = sklearn_model.deploy() >>> sklearn_model.predict(X_test) >>> sklearn_model.delete_deployment()
- generate_initial_types(X_sample: Any) List
Auto generate intial types.
- Parameters:
X_sample ((Any)) – Train data.
- Returns:
Initial types.
- Return type:
List
- static is_either_numerical_or_string_dataframe(data: DataFrame) bool
Check whether all the columns are either numerical or string for dataframe.
- serialize_model(as_onnx: Optional[bool] = False, initial_types: Optional[List[Tuple]] = None, force_overwrite: Optional[bool] = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs: Dict)
Serialize and save scikit-learn model using ONNX or model specific method.
- Parameters:
as_onnx ((bool, optional). Defaults to False.) – If set as True, provide initial_types or X_sample to convert into ONNX.
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
force_overwrite ((bool, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
Nothing.
- Return type:
None
- to_onnx(initial_types: List[Tuple] = None, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs)
Produces an equivalent ONNX model of the given scikit-learn model.
- Parameters:
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
An ONNX model (type: ModelProto) which is equivalent to the input scikit-learn model.
- Return type:
onnx.onnx_ml_pb2.ModelProto
ads.model.framework.tensorflow_model module
- class ads.model.framework.tensorflow_model.TensorFlowModel(estimator: callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Dict = None, **kwargs)
Bases:
FrameworkSpecificModel
TensorFlowModel class for estimators from Tensorflow framework.
- algorithm
The algorithm of the model.
- Type:
str
- artifact_dir
Directory for generate artifact.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained tensorflow estimator/model using Tensorflow.
- Type:
Callable
- framework
“tensorflow”, the framework name of the model.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> from ads.model.framework.tensorflow_model import TensorFlowModel >>> import tempfile >>> import tensorflow as tf
>>> mnist = tf.keras.datasets.mnist >>> (x_train, y_train), (x_test, y_test) = mnist.load_data() >>> x_train, x_test = x_train / 255.0, x_test / 255.0
>>> tf_estimator = tf.keras.models.Sequential( ... [ ... tf.keras.layers.Flatten(input_shape=(28, 28)), ... tf.keras.layers.Dense(128, activation="relu"), ... tf.keras.layers.Dropout(0.2), ... tf.keras.layers.Dense(10), ... ] ... ) >>> loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) >>> tf_estimator.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"]) >>> tf_estimator.fit(x_train, y_train, epochs=1)
>>> tf_model = TensorFlowModel(estimator=tf_estimator, ... artifact_dir=tempfile.mkdtemp()) >>> inference_conda_env = "generalml_p37_cpu_v1"
>>> tf_model.prepare(inference_conda_env="generalml_p37_cpu_v1", force_overwrite=True) >>> tf_model.verify(x_test[:1]) >>> tf_model.save() >>> model_deployment = tf_model.deploy(wait_for_completion=False) >>> tf_model.predict(x_test[:1])
Initiates a TensorFlowModel instance.
- Parameters:
estimator (callable) – Any model object generated by tensorflow framework
artifact_dir (str) – Directory for generate artifact.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
TensorFlowModel instance.
- Return type:
- get_data_serializer(data: Union[Dict, str, List, ndarray, Series, DataFrame, tf.Tensor], data_type: str = None)
Returns serializable input data.
- Parameters:
data (Union[Dict, str, list, numpy.ndarray, pd.core.series.Series,) –
pd.core.frame.DataFrame – Data expected by the model deployment predict API.
tf.Tensor] – Data expected by the model deployment predict API.
data_type (str) – Type of the data.
- Returns:
A class containing serialized input data and original data type information.
- Return type:
InputDataSerializer
- Raises:
TypeError – if provided data type is not supported.
- serialize_model(as_onnx: bool = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, force_overwrite: bool = False, **kwargs) None
Serialize and save Tensorflow model using ONNX or model specific method.
- Parameters:
as_onnx ((bool, optional). Defaults to False.) – If set as True, convert into ONNX model.
X_sample (Union[list, tuple, pd.Series, np.ndarray, pd.DataFrame]. Defaults to None.) – A sample of input data that will be used to generate input schema and detect input_signature.
force_overwrite ((bool, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
**kwargs (optional params used to serialize tensorflow model to onnx,) –
following (including the) – input_signature: a tuple or a list of tf.TensorSpec objects). default to None. Define the shape/dtype of the input so that model(input_signature) is a valid invocation of the model. opset_version: int. Defaults to None. Used for the ONNX model.
- Returns:
Nothing.
- Return type:
None
- to_onnx(path: str = None, input_signature=None, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, opset_version=None)
Exports the given Tensorflow model into ONNX format.
- Parameters:
path (str, default to None) – Path to save the serialized model.
input_signature (a tuple or a list of tf.TensorSpec objects. default to None.) – Define the shape/dtype of the input so that model(input_signature) is a valid invocation of the model.
X_sample (Union[list, tuple, pd.Series, np.ndarray, pd.DataFrame]. Defaults to None.) – A sample of input data that will be used to generate input schema and detect input_signature.
opset_version (int. Defaults to None.) – The opset to be used for the ONNX model.
- Returns:
Nothing
- Return type:
None
- Raises:
ValueError – if path is not provided
ads.model.framework.xgboost_model module
- class ads.model.framework.xgboost_model.XGBoostModel(estimator: callable, artifact_dir: str, properties: Optional[ModelProperties] = None, auth: Dict = None, **kwargs)
Bases:
FrameworkSpecificModel
XGBoostModel class for estimators from xgboost framework.
- algorithm
The algorithm of the model.
- Type:
str
- artifact_dir
Artifact directory to store the files needed for deployment.
- Type:
str
- auth
Default authentication is set using the ads.set_auth API. To override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create an authentication signer to instantiate an IdentityClient object.
- Type:
Dict
- ds_client
The data science client used by model deployment.
- Type:
DataScienceClient
- estimator
A trained xgboost estimator/model using Xgboost.
- Type:
Callable
- framework
“xgboost”, the framework name of the model.
- Type:
str
- hyperparameter
The hyperparameters of the estimator.
- Type:
dict
- metadata_custom
The model custom metadata.
- Type:
ModelCustomMetadata
- metadata_provenance
The model provenance metadata.
- Type:
ModelProvenanceMetadata
- metadata_taxonomy
The model taxonomy metadata.
- Type:
ModelTaxonomyMetadata
- model_artifact
This is built by calling prepare.
- Type:
- model_deployment
A ModelDeployment instance.
- Type:
- model_file_name
Name of the serialized model.
- Type:
str
- model_id
The model ID.
- Type:
str
- properties
ModelProperties object required to save and deploy model.
- Type:
- runtime_info
A RuntimeInfo instance.
- Type:
- schema_input
Schema describes the structure of the input data.
- Type:
Schema
- schema_output
Schema describes the structure of the output data.
- Type:
Schema
- serialize
Whether to serialize the model to pkl file by default. If False, you need to serialize the model manually, save it under artifact_dir and update the score.py manually.
- Type:
bool
- version
The framework version of the model.
- Type:
str
- delete_deployment(...)
Deletes the current model deployment.
- deploy(..., \*\*kwargs)
Deploys a model.
- from_model_artifact(uri, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from the specified folder, or zip/tar archive.
- from_model_catalog(model_id, model_file_name, artifact_dir, ..., \*\*kwargs)
Loads model from model catalog.
- introspect(...)
Runs model introspection.
- predict(data, ...)
Returns prediction of input data run against the model deployment endpoint.
- prepare(..., \*\*kwargs)
Prepare and save the score.py, serialized model and runtime.yaml file.
- reload(...)
Reloads the model artifact files: score.py and the runtime.yaml.
- save(..., \*\*kwargs)
Saves model artifacts to the model catalog.
- summary_status(...)
Gets a summary table of the current status.
- verify(data, ...)
Tests if deployment works in local environment.
Examples
>>> import xgboost as xgb >>> import tempfile >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn.datasets import load_iris >>> from ads.model.framework.xgboost_model import XGBoostModel
>>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> xgboost_estimator = xgb.XGBClassifier() >>> xgboost_estimator.fit(X_train, y_train)
>>> xgboost_model = XGBoostModel(estimator=xgboost_estimator, artifact_dir=tmp_model_dir) >>> xgboost_model.prepare(inference_conda_env="generalml_p37_cpu_v1", force_overwrite=True) >>> xgboost_model.reload() >>> xgboost_model.verify(X_test) >>> xgboost_model.save() >>> model_deployment = xgboost_model.deploy(wait_for_completion=False) >>> xgboost_model.predict(X_test)
Initiates a XGBoostModel instance. This class wraps the XGBoost model as estimator. It’s primary purpose is to hold the trained model and do serialization.
- Parameters:
estimator – XGBoostModel
artifact_dir (str) – artifact directory to store the files needed for deployment.
properties ((ModelProperties, optional). Defaults to None.) – ModelProperties object required to save and deploy model.
auth ((Dict, optional). Defaults to None.) – The default authetication is set using ads.set_auth API. If you need to override the default, use the ads.common.auth.api_keys or ads.common.auth.resource_principal to create appropriate authentication signer and kwargs required to instantiate IdentityClient object.
- Returns:
XGBoostModel instance.
- Return type:
Examples
>>> import xgboost as xgb >>> import tempfile >>> from sklearn.datasets import make_classification >>> from sklearn.model_selection import train_test_split >>> from sklearn.datasets import load_iris >>> from ads.model.framework.xgboost_model import XGBoostModel
>>> iris = load_iris() >>> X, y = iris.data, iris.target
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) >>> train = xgb.DMatrix(X_train, y_train) >>> test = xgb.DMatrix(X_test, y_test) >>> xgboost_estimator = XGBClassifier() >>> xgboost_estimator.fit(X_train, y_train) >>> xgboost_model = XGBoostModel(estimator=xgboost_estimator, artifact_dir=tempfile.mkdtemp()) >>> xgboost_model.prepare(inference_conda_env="generalml_p37_cpu_v1") >>> xgboost_model.verify(X_test) >>> xgboost_model.save() >>> model_deployment = xgboost_model.deploy() >>> xgboost_model.predict(X_test) >>> xgboost_model.delete_deployment()
- generate_initial_types(X_sample: Any) List
Auto generate intial types.
- Parameters:
X_sample ((Any)) – Train data.
- Returns:
Initial types.
- Return type:
List
- serialize_model(as_onnx: bool = False, initial_types: List[Tuple] = None, force_overwrite: bool = False, X_sample: Optional[Union[Dict, str, List, Tuple, ndarray, Series, DataFrame]] = None, **kwargs)
Serialize and save Xgboost model using ONNX or model specific method.
- Parameters:
artifact_dir (str) – Directory for generate artifact.
as_onnx ((boolean, optional). Defaults to False.) – If set as True, provide initial_types or X_sample to convert into ONNX.
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
force_overwrite ((boolean, optional). Defaults to False.) – If set as True, overwrite serialized model if exists.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
Nothing.
- Return type:
None
- to_onnx(initial_types: List[Tuple] = None, X_sample: Union[list, tuple, DataFrame, Series, ndarray] = None, **kwargs)
Produces an equivalent ONNX model of the given Xgboost model.
- Parameters:
initial_types ((List[Tuple], optional). Defaults to None.) – Each element is a tuple of a variable name and a type.
X_sample (Union[Dict, str, List, np.ndarray, pd.core.series.Series, pd.core.frame.DataFrame,]. Defaults to None.) – Contains model inputs such that model(X_sample) is a valid invocation of the model. Used to generate initial_types.
- Returns:
An ONNX model (type: ModelProto) which is equivalent to the input xgboost model.
- Return type:
onnx.onnx_ml_pb2.ModelProto