Azure Machine Learning (Azure ML) is a cloud-based service that allows users to build, train, and deploy machine learning models. This post provides a comprehensive introduction to the core concepts of Azure ML and guides users through the process of running a training job on a scalable compute resource, deploying the model as an endpoint, and testing the deployment.
Overview:
This post is an introduction to some of the most used features of the Azure Machine Learning service.
In it, we will explore and learn how to create, register and deploy a model. This post will help you become familiar with the core concepts of Azure Machine Learning and their most common usage.
You’ll learn how to run a training job on a scalable compute resource, then deploy it, and finally test the deployment.
You’ll create a training script to handle the data preparation, train and register a model. Once you train the model, you’ll deploy it as an endpoint, then call the endpoint for inferencing.
Let’s get started with a summary of the Demo.
Demo (Summary):
Demo Summary:
- Create Training Script: This involves writing a script that defines the model training process, including data loading, model architecture, loss function, and optimization algorithm. The script is typically written in a programming language supported by the machine learning framework you’re using.
- Create Command: This step involves defining a command that will execute the training script. The command specifies the script to run and may include additional parameters such as the path to the dataset and hyperparameters for the model training.
- Submit the Command to Run the Training Script: After creating the command, you submit it to a compute resource, such as a local machine, cloud-based virtual machine, or a managed cluster. This initiates the training process.
- View Training Results: Once the training is complete, you can view the results, which typically include the model’s performance metrics such as accuracy, loss, and validation results. These results help evaluate the effectiveness of the model.
- Deploy an Endpoint: Deploying an endpoint involves setting up a web service that allows for model inference. This is often done through cloud services that provide an API endpoint to which you can send data for predictions.
- Deploy Model to Endpoint: This step involves uploading the trained model to the deployed endpoint so that it can process incoming prediction requests.
- Test Deployment: Finally, you test the deployed model by sending sample data to the endpoint and verifying that it returns the expected predictions.
These steps are part of a typical machine-learning workflow and may vary slightly depending on the tools and platforms you’re using.
Refer below image for the ML lifecycle overview.
Prerequisites:
Prerequisites:
- Set Up Workspace: Users need an Azure ML workspace, which is the central place for all artifacts created when using Azure ML. https://ml.azure.com
- Create Compute Resource: A compute cluster is created for scalable resources.
- Open or create a notebook in your workspace
Or, open tutorials/get-started-notebooks/quickstart.ipynb from the Samples section of studio. Then select Clone to add the notebook to your Files.
- Set your kernel
Demo (Step-by-step)
Create workspace.
Before we deep dive into the code, you need a way to reference your workspace. The workspace is the top-level resource for Azure Machine Learning, providing a centralized place to work with all the artifacts you create when you use Azure Machine Learning.
You’ll create ml_client for a handle to the workspace. You’ll then use ml_client to manage resources and jobs.
Python
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
# authenticate
credential = DefaultAzureCredential()
SUBSCRIPTION="<SUBSCRIPTION_ID>"
RESOURCE_GROUP="<RESOURCE_GROUP>"
WS_NAME="<AML_WORKSPACE_NAME>"
# Get a handle to the workspace
ml_client = MLClient(
credential=credential,
subscription_id=SUBSCRIPTION,
resource_group_name=RESOURCE_GROUP,
workspace_name=WS_NAME,
)Create a Compute Instance:
1. Create training script
Let’s start by creating the training script – the main.py Python file.
This script handles the data preparation.
First, create a source folder for the script:
Python
import os
train_src_dir = "./src"
os.makedirs(train_src_dir, exist_ok=True)This script handles the preprocessing of the data, splitting it into test and train data. It then consumes this data to train a tree-based model and return the output model.
MLFlow will be used to log the parameters and metrics during our pipeline run.
The cell below uses IPython magic to write the training script into the directory you just created.
Python
%%writefile {train_src_dir}/main.py
import os
import argparse
import pandas as pd
import mlflow
import mlflow.sklearn
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
def main():
"""Main function of the script."""
# input and output arguments
parser = argparse.ArgumentParser()
parser.add_argument("--data", type=str, help="path to input data")
parser.add_argument("--test_train_ratio", type=float, required=False, default=0.25)
parser.add_argument("--n_estimators", required=False, default=100, type=int)
parser.add_argument("--learning_rate", required=False, default=0.1, type=float)
parser.add_argument("--registered_model_name", type=str, help="model name")
args = parser.parse_args()
# Start Logging
mlflow.start_run()
# enable autologging
mlflow.sklearn.autolog()
###################
#<prepare the data>
###################
print(" ".join(f"{k}={v}" for k, v in vars(args).items()))
print("input data:", args.data)
credit_df = pd.read_csv(args.data, header=1, index_col=0)
mlflow.log_metric("num_samples", credit_df.shape[0])
mlflow.log_metric("num_features", credit_df.shape[1] - 1)
train_df, test_df = train_test_split(
credit_df,
test_size=args.test_train_ratio,
)
####################
#</prepare the data>
####################
##################
#<train the model>
##################
# Extracting the label column
y_train = train_df.pop("default payment next month")
# convert the dataframe values to array
X_train = train_df.values
# Extracting the label column
y_test = test_df.pop("default payment next month")
# convert the dataframe values to array
X_test = test_df.values
print(f"Training with data of shape {X_train.shape}")
clf = GradientBoostingClassifier(
n_estimators=args.n_estimators, learning_rate=args.learning_rate
)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))
###################
#</train the model>
###################
##########################
#<save and register model>
##########################
# Registering the model to the workspace
print("Registering the model via MLFlow")
mlflow.sklearn.log_model(
sk_model=clf,
registered_model_name=args.registered_model_name,
artifact_path=args.registered_model_name,
)
# Saving the model to a file
mlflow.sklearn.save_model(
sk_model=clf,
path=os.path.join(args.registered_model_name, "trained_model"),
)
###########################
#</save and register model>
###########################
# Stop Logging
mlflow.end_run()
if __name__ == "__main__":
main()
As you can see in this script, once the model is trained, the model file is saved and registered to the workspace. Now you can use the registered model in inferencing endpoints.
You might need to select Refresh to see the new folder and script in your Files.
2. Create/Configure Command
Now that you have a script that can perform the desired tasks, and a compute cluster to run the script, you’ll use a general-purpose command that can run command line actions. This command line action can directly call system commands or run a script.
Here, you’ll create input variables to specify the input data, split ratio, learning rate, and registered model name.
Python
from azure.ai.ml import command
from azure.ai.ml import Input
registered_model_name = "credit_defaults_model"
job = command(
inputs=dict(
data=Input(
type="uri_file",
path="https://azuremlexamples.blob.core.windows.net/datasets/credit_card/default_of_credit_card_clients.csv",
),
test_train_ratio=0.2,
learning_rate=0.25,
registered_model_name=registered_model_name,
),
code="./src/", # location of source code
command="python main.py --data ${{inputs.data}} --test_train_ratio ${{inputs.test_train_ratio}} --learning_rate ${{inputs.learning_rate}} --registered_model_name ${{inputs.registered_model_name}}",
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest",
display_name="credit_default_prediction",
)The command script will:
- Use an environment that defines software and runtime libraries needed for the training script. Azure Machine Learning provides many curated or ready-made environments, which are useful for common training and inference scenarios. You’ll use one of those environments here. In Tutorial: Train a model in Azure Machine Learning, you’ll learn how to create a custom environment.
- Configure the command line action itself – python main.py in this case. The inputs/outputs are accessible in the command via the ${{ … }} notation.
- In this sample, we access the data from a file on the internet.
- Since a compute resource was not specified, the script will be run on a serverless compute cluster that is automatically created.
3. Submit the command to run the training script
It’s now time to submit the job to run in Azure Machine Learning. This time you’ll use create_or_update on ml_client.
Python
ml_client.create_or_update(job)Click the link to see the details.
Click on Link:
4. View Training results
When completed, View Training results.
Check the Credit Card data:
This could take approx 10 mins.
5. Deploy an endpoint
Now deploy your machine learning model as a web service in the Azure cloud, an online endpoint.
To deploy a machine learning service, you’ll use the model you registered.
import uuid
# Creating a unique name for the endpoint
online_endpoint_name = "credit-endpoint-" + str(uuid.uuid4())[:8]Create the endpoint:
Python
# Expect the endpoint creation to take a few minutes
from azure.ai.ml.entities import (
ManagedOnlineEndpoint,
ManagedOnlineDeployment,
Model,
Environment,
)
# create an online endpoint
endpoint = ManagedOnlineEndpoint(
name=online_endpoint_name,
description="this is an online endpoint",
auth_mode="key",
tags={
"training_dataset": "credit_defaults",
"model_type": "sklearn.GradientBoostingClassifier",
},
)
endpoint = ml_client.online_endpoints.begin_create_or_update(endpoint).result()
print(f"Endpoint {endpoint.name} provisioning state: {endpoint.provisioning_state}")
Once the endpoint has been created, you can retrieve it as below:
PythonCopy
endpoint = ml_client.online_endpoints.get(name=online_endpoint_name)
print(
f'Endpoint "{endpoint.name}" with provisioning state "{endpoint.provisioning_state}" is retrieved'
)
6. Deploy model to endpoint
Once the endpoint is created, deploy the model with the entry script. Each endpoint can have multiple deployments. Direct traffic to these deployments can be specified using rules. Here you’ll create a single deployment that handles 100% of the incoming traffic. We have chosen a color name for the deployment, for example, blue, green, red deployments, which is arbitrary.
You can check the Models page on Azure Machine Learning studio, to identify the latest version of your registered model. Alternatively, the code below will retrieve the latest version number for you to use.
Python
# Let's pick the latest version of the model
latest_model_version = max(
[int(m.version) for m in ml_client.models.list(name=registered_model_name)]
)
print(f'Latest model is version "{latest_model_version}" ')Deploy the latest version of the model.
Python
# picking the model to deploy. Here we use the latest version of our registered model
model = ml_client.models.get(name=registered_model_name, version=latest_model_version)
# Expect this deployment to take approximately 6 to 8 minutes.
# create an online deployment.
# if you run into an out of quota error, change the instance_type to a comparable VM that is available.
# Learn more on https://azure.microsoft.com/pricing/details/machine-learning/.
blue_deployment = ManagedOnlineDeployment(
name="blue",
endpoint_name=online_endpoint_name,
model=model,
instance_type="Standard_DS3_v2",
instance_count=1,
)
blue_deployment = ml_client.begin_create_or_update(blue_deployment).result()
7. Test Deployment
Once the model is deployed to the endpoint, you can run inference with it.
Create a sample request file following the design expected in the run method in the score script.
Python
deploy_dir = "./deploy"
os.makedirs(deploy_dir, exist_ok=True)
PythonCopy
%%writefile {deploy_dir}/sample-request.json
{
"input_data": {
"columns": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],
"index": [0, 1],
"data": [
[20000,2,2,1,24,2,2,-1,-1,-2,-2,3913,3102,689,0,0,0,0,689,0,0,0,0],
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8]
]
}
}
Python
# test the blue deployment with some sample data
ml_client.online_endpoints.invoke(
endpoint_name=online_endpoint_name,
request_file="./deploy/sample-request.json",
deployment_name="blue",
)Congratulations!
Clean up resources
If you’re not going to use the endpoint, delete it to stop using the resource. Make sure no other deployments are using an endpoint before you delete it.
Note: Expect the complete deletion to take approximately 20 minutes.
Python
ml_client.online_endpoints.begin_delete(name=online_endpoint_name)Next Steps:
We will be covering the below topics in upcoming posts.
| Upload, access and explore your data in Azure Machine Learning | Store large data in the cloud and retrieve it from notebooks and scripts |
| Model development on a cloud workstation | Start prototyping and developing machine learning models |
| Train a model in Azure Machine Learning | Dive in to the details of training a model |
| Deploy a model as an online endpoint | Dive in to the details of deploying a model |
| Create production machine learning pipelines | Split a complete machine learning task into a multistep workflow. |
Conclusion
Azure ML democratizes machine learning by providing a platform compatible with open-source frameworks like PyTorch and TensorFlow.
It caters to all skill levels with advanced MLOps features and simple no-code model creation and deployment. This post serves as a starting point for individuals looking to familiarize themselves with Azure ML and begin their journey in machine learning model management.
The post emphasizes the importance of a structured approach to machine learning, highlighting the ease of use and flexibility of Azure ML in handling the end-to-end lifecycle of machine learning models.
By following the steps outlined, users can gain hands-on experience with Azure ML and leverage its capabilities to enhance their machine learning projects.
References:
Quickstart: Get started with Azure Machine Learning – Azure Machine Learning | Microsoft Learn

