Train Explain Model On Amlcompute And Deploy
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
![]()
Train and explain models remotely via Azure Machine Learning Compute and deploy model and scoring explainer
This notebook illustrates how to use the Azure Machine Learning Interpretability SDK to train and explain a classification model remotely on an Azure Machine Leanrning Compute Target (AMLCompute), and use Azure Container Instances (ACI) for deploying your model and its corresponding scoring explainer as a web service.
Problem: IBM employee attrition classification with scikit-learn (train a model and run an explainer remotely via AMLCompute, and deploy model and its corresponding explainer.)
Table of Contents
- Introduction
- Setup
- Run model explainer locally at training time
- Apply feature transformations
- Train a binary classification model
- Explain the model on raw features
- Generate global explanations
- Generate local explanations
- Visualize results
- Deploy model and scoring explainer
- Next steps
Introduction
This notebook showcases how to train and explain a classification model remotely via Azure Machine Learning Compute (AMLCompute), download the calculated explanations locally for visualization and inspection, and deploy the final model and its corresponding explainer to Azure Container Instances (ACI). It demonstrates the API calls that you need to make to submit a run for training and explaining a model to AMLCompute, download the compute explanations remotely, and visualizing the global and local explanations via a visualization dashboard that provides an interactive way of discovering patterns in model predictions and downloaded explanations, and using Azure Machine Learning MLOps capabilities to deploy your model and its corresponding explainer.
We will showcase one of the tabular data explainers: TabularExplainer (SHAP) and follow these steps:
- Develop a machine learning script in Python which involves the training script and the explanation script.
- Create and configure a compute target.
- Submit the scripts to the configured compute target to run in that environment. During training, the scripts can read from or write to datastore. And the records of execution (e.g., model, metrics, prediction explanations) are saved as runs in the workspace and grouped under experiments.
- Query the experiment for logged metrics and explanations from the current and past runs. Use the interpretability toolkit’s visualization dashboard to visualize predictions and their explanation. If the metrics and explanations don't indicate a desired outcome, loop back to step 1 and iterate on your scripts.
- After a satisfactory run is found, create a scoring explainer and register the persisted model and its corresponding explainer in the model registry.
- Develop a scoring script.
- Create an image and register it in the image registry.
- Deploy the image as a web service in Azure.
![]() |
|---|
Setup
Make sure you go through the configuration notebook first if you haven't.
Initialize a Workspace
Initialize a workspace object from persisted configuration
Explain
Create An Experiment: Experiment is a logical container in an Azure ML Workspace. It hosts run records which can include run metrics and output artifacts from your experiments.
Introduction to AmlCompute
Azure Machine Learning Compute is managed compute infrastructure that allows the user to easily create single to multi-node compute of the appropriate VM Family. It is created within your workspace region and is a resource that can be used by other users in your workspace. It autoscales by default to the max_nodes, when a job is submitted, and executes in a containerized environment packaging the dependencies as specified by the user.
Since it is managed compute, job scheduling and cluster management are handled internally by Azure Machine Learning service.
For more information on Azure Machine Learning Compute, please read this article
If you are an existing BatchAI customer who is migrating to Azure Machine Learning, please read this article
Note: As with other Azure services, there are limits on certain resources (for eg. AmlCompute quota) associated with the Azure Machine Learning service. Please read this article on the default limits and how to request more quota.
The training script run_explainer.py is already created for you. Let's have a look.
Submit an AmlCompute run
First lets check which VM families are available in your region. Azure is a regional service and some specialized SKUs (especially GPUs) are only available in certain regions. Since AmlCompute is created in the region of your workspace, we will use the supported_vms () function to see if the VM family we want to use ('STANDARD_D2_V2') is supported.
You can also pass a different region to check availability and then re-create your workspace in that region through the configuration notebook
Create project directory
Create a directory that will contain all the necessary code from your local machine that you will need access to on the remote resource. This includes the training script, and any additional files your training script depends on
Provision a compute target
Note that if you have an AzureML Data Scientist role, you will not have permission to create compute resources. Talk to your workspace or IT admin to create the compute targets described in this section, if they do not already exist.
You can provision an AmlCompute resource by simply defining two parameters thanks to smart defaults. By default it autoscales from 0 nodes and provisions dedicated VMs to run your job in a container. This is useful when you want to continously re-use the same target, debug it between jobs or simply share the resource with other users of your workspace.
vm_size: VM family of the nodes provisioned by AmlCompute. Simply choose from the supported_vmsizes() abovemax_nodes: Maximum nodes to autoscale to while running a job on AmlCompute
Configure & Run
Note: if you need to cancel a run, you can follow these instructions.
Download Model Explanation, Model, and Data
Visualize
Visualize the explanations
Deploy
Deploy Model and ScoringExplainer
Next
Learn about other use cases of the explain package on a:
- Training time: regression problem
- Training time: binary classification problem
- Training time: multiclass classification problem
- Explain models with engineered features:
- Save model explanations via Azure Machine Learning Run History
- Run explainers remotely on Azure Machine Learning Compute (AMLCompute)
- Inferencing time: deploy a locally-trained model and explainer
- Inferencing time: deploy a locally-trained keras model and explainer
