Model class

Definition

Represents the result of machine learning training.

A model is the result of a Azure Machine learning training Run or some other model training process outside of Azure. Regardless of how the model is produced, it can be registered in a workspace, where it is represented by a name and a version. With the Model class, you can package models for use with Docker and deploy them as an inference endpoint.

For an end-to-end tutorial showing how models are created, managed, and consumed, see Train image classification model with MNIST data and scikit-learn using Azure Machine Learning.

Model(workspace, name=None, id=None, tags=None, properties=None, version=None, run_id=None, model_framework=None)
Inheritance
builtins.object
Model

Parameters

workspace
Workspace

The workspace object containing the model to retrieve.

name
str

The name of the model to retrieve. The latest model with the specified name is returned, if it exists.

id
str

The ID of the model to retrieve. The model with the specified ID is returned, if it exists.

tags
list

An optional list of tags used to filter returned results. Results are filtered based on the provided list, searching by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

properties
list

An optional list of properties used to filter returned results. Results are filtered based on the provided list, searching by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

version
int

The model version to return. When provided along with name, the specific version of the specified named model is returned, if it exists.

run_id
str

Optional ID used to filter returned results.

model_framework
str

Optional framework name used to filter returned results. If specified, results are returned for the models matching the specified framework. See Framework for allowed values.

Remarks

The Model constructor is used to retrieve a cloud representation of a Model object associated with the specified workspace. At least the name or ID must be provided to retrieve models, but there are also other options for filtering including by tags, properties, run ID, and framework.


   from azureml.core.model import Model
   model = Model(ws, 'my_model_name')

For example, if you have a model that is stored in multiple files, you can register them as a single model in your Azure Machine Learning workspace. After registration, you can then download or deploy the registered model and receive all the files that were registered.

Registering a model creates a logical container for the one or more files that make up your model. In addition to the content of the model file itself, a registered model also stores model metadata, including model description, tags, and framework information, that is useful when managing and deploying the model in your workspace. For example, with tags you can categorize your models and apply filters when listing models in your workspace. After registration, you can then download or deploy the registered model and receive all the files and metadata that were registered.

The following sample shows how to register a model specifying tags and a description.


   from azureml.core.model import Model

   model = Model.register(model_path="sklearn_regression_model.pkl",
                          model_name="sklearn_regression_model_local_adv",
                          tags={'area': "diabetes", 'type': "regression"},
                          description="Ridge regression model to predict diabetes",
                          workspace=ws)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/deploy-to-local/register-model-deploy-local-advanced.ipynb

The following sample shows how to register a model specifying framework, input and output datasets, and resource configuration.


   from azureml.core import Model
   from azureml.core.resource_configuration import ResourceConfiguration

   model = Model.register(workspace=ws,
                          model_name='my-sklearn-model',                # Name of the registered model in your workspace.
                          model_path='./sklearn_regression_model.pkl',  # Local file to upload and register as a model.
                          model_framework=Model.Framework.SCIKITLEARN,  # Framework used to create the model.
                          model_framework_version='0.19.1',             # Version of scikit-learn used to create the model.
                          sample_input_dataset=input_dataset,
                          sample_output_dataset=output_dataset,
                          resource_configuration=ResourceConfiguration(cpu=1, memory_in_gb=0.5),
                          description='Ridge regression model to predict diabetes progression.',
                          tags={'area': 'diabetes', 'type': 'regression'})

   print('Name:', model.name)
   print('Version:', model.version)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/deploy-to-cloud/model-register-and-deploy.ipynb

Methods

add_dataset_references(datasets)

Associate the provided datasets with this Model.

add_properties(properties)

Add key value pairs to properties dictionary of this model.

add_tags(tags)

Add key value pairs to the tags dictionary of this model.

delete()

Delete this model from its associated workspace.

deploy(workspace, name, models, inference_config=None, deployment_config=None, deployment_target=None, overwrite=False)

Deploy a Webservice from zero or more model objects.

This function is similar to the deploy function of the Webservice class, but does not register the models. Use this function if you have model objects that are already registered.

deserialize(workspace, model_payload)

Convert a JSON object into a model object.

Conversion fails if the specified workspace is not the workspace the model is registered with.

download(target_dir='.', exist_ok=False, exists_ok=None)

Download model to target directory of the local file system.

get_model_path(model_name, version=None, _workspace=None)

Return path to model.

The function will search for the model in the following locations.

If version is None:

  1. Download from remote to cache
  2. Load from cache azureml-models/$MODEL_NAME/$LATEST_VERSION/
  3. ./$MODEL_NAME

If version is not None:

  1. Load from cache azureml-models/$MODEL_NAME/$LATEST_VERSION/
  2. Download from remote to cache
get_sas_urls()

Return a dictionary of key value pairs containing filenames and corresponding SAS URLs.

list(workspace, name=None, tags=None, properties=None, run_id=None, latest=False)

Retrieve a list of all models associated with the provided workspace, with optional filters.

package(workspace, models, inference_config=None, generate_dockerfile=False)

Create a model package in the form of a Docker image or Dockerfile build context.

profile(workspace, profile_name, models, inference_config, input_data)

Profiles this model to get resource configuration recommendations.

register(workspace, model_path, model_name, tags=None, properties=None, description=None, datasets=None, model_framework=None, model_framework_version=None, child_paths=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Register a model with the provided workspace.

remove_tags(tags)

Remove the specified keys from tags dictionary of this model.

serialize()

Convert this Model into a json serialized dictionary.

update(tags=None, description=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Perform an in-place update of the model.

Existing values of specified parameters are replaced.

update_tags_properties(add_tags=None, remove_tags=None, add_properties=None)

Perform an update of the tags and properties of the model.

add_dataset_references(datasets)

Associate the provided datasets with this Model.

add_dataset_references(datasets)

Parameters

datasets
list[tuple({str : (Dataset or DatasetSnapshot)})]

A list of tuples representing a pairing of dataset purpose to Dataset object.

add_properties(properties)

Add key value pairs to properties dictionary of this model.

add_properties(properties)

Parameters

properties
dict({str : str})

The dictionary of properties to add.

add_tags(tags)

Add key value pairs to the tags dictionary of this model.

add_tags(tags)

Parameters

tags
dict({str : str})

The dictionary of tags to add.

Exceptions

delete()

Delete this model from its associated workspace.

delete()

deploy(workspace, name, models, inference_config=None, deployment_config=None, deployment_target=None, overwrite=False)

Deploy a Webservice from zero or more model objects.

This function is similar to the deploy function of the Webservice class, but does not register the models. Use this function if you have model objects that are already registered.

deploy(workspace, name, models, inference_config=None, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace

A Workspace object to associate the Webservice with.

name
str

The name to give the deployed service. Must be unique to the workspace, only consist of lowercase letters, numbers, or dashes, start with a letter, and be between 3 and 32 characters long.

models
list[Model]

A list of model objects. Can be an empty list.

inference_config
InferenceConfig

An InferenceConfig object used to determine required model properties.

default value: None
deployment_config
WebserviceDeploymentConfiguration

A WebserviceDeploymentConfiguration used to configure the webservice. If one is not provided, an empty configuration object will be used based on the desired target.

default value: None
deployment_target
ComputeTarget

A ComputeTarget to deploy the Webservice to. As Azure Container Instances has no associated ComputeTarget, leave this parameter as None to deploy to Azure Container Instances.

default value: None
overwrite
bool

Indicates whether to overwrite the existing service if a service with the specified name already exists.

default value: False

Returns

A Webservice object corresponding to the deployed webservice.

Return type

Exceptions

deserialize(workspace, model_payload)

Convert a JSON object into a model object.

Conversion fails if the specified workspace is not the workspace the model is registered with.

deserialize(workspace, model_payload)

Parameters

workspace
Workspace

The workspace object the model is registered with.

model_payload
dict

A JSON object to convert to a Model object.

Returns

The Model representation of the provided JSON object.

Return type

download(target_dir='.', exist_ok=False, exists_ok=None)

Download model to target directory of the local file system.

download(target_dir='.', exist_ok=False, exists_ok=None)

Parameters

target_dir
str

The path to a directory in which to download the model. Defaults to "."

default value: .
exist_ok
bool

Indicates whether to replace downloaded dir/files if they exist. Defaults to False.

default value: False
exists_ok
bool

DEPRECATED. Use exist_ok.

default value: None

Returns

The path to file or folder of the model.

Return type

str

get_model_path(model_name, version=None, _workspace=None)

Return path to model.

The function will search for the model in the following locations.

If version is None:

  1. Download from remote to cache
  2. Load from cache azureml-models/$MODEL_NAME/$LATEST_VERSION/
  3. ./$MODEL_NAME

If version is not None:

  1. Load from cache azureml-models/$MODEL_NAME/$LATEST_VERSION/
  2. Download from remote to cache
get_model_path(model_name, version=None, _workspace=None)

Parameters

model_name
str

The name of the model to retrieve.

version
int

The version of the model to retrieve. Defaults to the latest version.

default value: None
_workspace
Workspace

The workspace to retrieve a model from. Can't use remotely.

default value: None

Returns

The path on disk to the model.

Return type

str

Exceptions

get_sas_urls()

Return a dictionary of key value pairs containing filenames and corresponding SAS URLs.

get_sas_urls()

Returns

Dictionary of K,V pairs containing filenames and corresponding SAS URLs

Return type

list(workspace, name=None, tags=None, properties=None, run_id=None, latest=False)

Retrieve a list of all models associated with the provided workspace, with optional filters.

list(workspace, name=None, tags=None, properties=None, run_id=None, latest=False)

Parameters

workspace
Workspace

The workspace object to retrieve models from.

name
str

If provided, will only return models with the specified name, if any.

default value: None
tags
list

Will filter based on the provided list, by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

default value: None
properties
list

Will filter based on the provided list, by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

default value: None
run_id
str

Will filter based on the provided run ID.

default value: None
latest
bool

If true, will only return models with the latest version.

default value: False

Returns

A list of models, optionally filtered.

Return type

Exceptions

package(workspace, models, inference_config=None, generate_dockerfile=False)

Create a model package in the form of a Docker image or Dockerfile build context.

package(workspace, models, inference_config=None, generate_dockerfile=False)

Parameters

workspace
Workspace

The workspace in which to create the package.

models
list[Model]

A list of Model objects to include in the package. Can be an empty list.

inference_config
InferenceConfig

An InferenceConfig object to configure the operation of the models. This must include an Environment object.

default value: None
generate_dockerfile
bool

Whether to create a Dockerfile that can be run locally instead of building an image.

default value: False

Returns

A ModelPackage object.

Return type

profile(workspace, profile_name, models, inference_config, input_data)

Profiles this model to get resource configuration recommendations.

profile(workspace, profile_name, models, inference_config, input_data)

Parameters

workspace
Workspace

A Workspace object in which to profile the model

profile_name
str

The name of the profiling run

models
list[Model]

A list of model objects. Can be an empty list.

inference_config
InferenceConfig

An InferenceConfig object used to determine required model properties.

input_data
str

The input data for profiling

Return type

register(workspace, model_path, model_name, tags=None, properties=None, description=None, datasets=None, model_framework=None, model_framework_version=None, child_paths=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Register a model with the provided workspace.

register(workspace, model_path, model_name, tags=None, properties=None, description=None, datasets=None, model_framework=None, model_framework_version=None, child_paths=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Parameters

workspace
Workspace

The workspace to register the model with.

model_path
str

The path on the local file system where the model assets are located. This can be a direct pointer to a single file or folder. If pointing to a folder, the child_paths parameter can be used to specify individual files to bundle together as the Model object, as opposed to using the entire contents of the folder.

model_name
str

The name to register the model with.

tags
dict({str : str})

An optional dictionary of key value tags to assign to the model.

default value: None
properties
dict({str : str})

An optional dictionary of key value properties to assign to the model. These properties can't be changed after model creation, however new key value pairs can be added.

default value: None
description
str

A text description of the model.

default value: None
datasets
list[(str, AbstractDataset)]

A list of tuples where the first element describes the dataset-model relationship and the second element is the dataset.

default value: None
model_framework
str

The framework of the registered model. Using the system-supported constants from the Framework class allows for simplified deployment for some popular frameworks.

default value: None
model_framework_version
str

The framework version of the registered model.

default value: None
child_paths
list[str]

If provided in conjunction with a model_path to a folder, only the specified files will be bundled into the Model object.

default value: None
sample_input_dataset
AbstractDataset

Sample input dataset for the registered model.

default value: None
sample_output_dataset
AbstractDataset

Sample output dataset for the registered model.

default value: None
resource_configuration
ResourceConfiguration

A resource configuration to run the registered model.

default value: None

Returns

The registered model object.

Return type

Remarks

In addition to the content of the model file itself, a registered model also stores model metadata, including model description, tags, and framework information, that is useful when managing and deploying the model in your workspace. For example, with tags you can categorize your models and apply filters when listing models in your workspace.

The following sample shows how to register a model specifying tags and a description.


   from azureml.core.model import Model

   model = Model.register(model_path="sklearn_regression_model.pkl",
                          model_name="sklearn_regression_model_local_adv",
                          tags={'area': "diabetes", 'type': "regression"},
                          description="Ridge regression model to predict diabetes",
                          workspace=ws)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/deploy-to-local/register-model-deploy-local-advanced.ipynb

remove_tags(tags)

Remove the specified keys from tags dictionary of this model.

remove_tags(tags)

Parameters

tags
list[str]

The list of keys to remove

serialize()

Convert this Model into a json serialized dictionary.

serialize()

Returns

The json representation of this Model

Return type

update(tags=None, description=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Perform an in-place update of the model.

Existing values of specified parameters are replaced.

update(tags=None, description=None, sample_input_dataset=None, sample_output_dataset=None, resource_configuration=None)

Parameters

tags
dict({str : str})

A dictionary of tags to update the model with. These tags replace existing tags for the model.

default value: None
description
str

The new description to use for the model. This name replaces the existing name.

default value: None
sample_input_dataset
AbstractDataset

The sample input dataset to use for the registered model. This sample input dataset replaces the existing dataset.

default value: None
sample_output_dataset
AbstractDataset

The sample output dataset to use for the registered model. This sample output dataset replaces the existing dataset.

default value: None
resource_configuration
ResourceConfiguration

The resource configuration to use to run the registered model.

default value: None

Exceptions

update_tags_properties(add_tags=None, remove_tags=None, add_properties=None)

Perform an update of the tags and properties of the model.

update_tags_properties(add_tags=None, remove_tags=None, add_properties=None)

Parameters

add_tags
dict({str : str})

A dictionary of tags to add.

default value: None
remove_tags
list[str]

A list of tag names to remove.

default value: None
add_properties
dict({str : str})

A dictionary of properties to add.

default value: None

Exceptions