Lab 1B: Working with Azure Machine Learning Tools
In this lab, you will explore the tools available for working with an Azure Machine Learning workspace.
You will use Azure Machine Learning studio, JupyterLab on a compute instance, GitHub, and the Azure Machine Learning SDK v2 for Python.
Before You Start
Before starting this lab, you must have created an Azure Machine Learning workspace by following the instructions in the previous lab.
You also need:
- An Azure subscription with permission to access the Azure Machine Learning workspace.
- A running Azure Machine Learning compute instance.
- Access to the course repository.
Task 1: Use Azure ML SDK v2 in a Compute Instance
You can manage many Azure Machine Learning resources through the Studio interface. However, the Azure Machine Learning SDK v2 enables you to automate repeatable tasks and manage Azure ML resources from Python code.
Select your Azure Machine Learning workspace.
In the left menu, select Compute.
Open the Compute instances tab.
Start the compute instance created in the previous lab if it is not already running.
Wait until the compute instance status changes to Running.
Select the JupyterLab link for the compute instance.
In JupyterLab, create a new notebook.
In the upper-right corner of the notebook, select a current Azure Machine Learning SDK v2 kernel. The kernel name may be similar to:
Python 3.10 - SDK v2Run the following code cell to verify that the Azure ML SDK v2 is available:
import sys from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential print(f"Python executable: {sys.executable}") print("Azure Machine Learning SDK v2 is ready.")If the imports fail with a
ModuleNotFoundError, open a new terminal in JupyterLab and install the required packages:python -m pip install --upgrade azure-ai-ml azure-identityRestart the notebook kernel after installing packages, and run the verification cell again.
More Information: For current Azure Machine Learning SDK v2 documentation, see the Azure ML SDK for Python documentation.
Task 2: Clone the Course Repository
You will use notebooks and supporting files from the course repository.
In JupyterLab, select File > New > Terminal.
Change to your Azure Machine Learning user directory, replacing
<your-user-name>with the folder name shown under Users in the JupyterLab file browser (this is based on your Azure AD identity, not your Linux username):cd ~/cloudfiles/code/Users/<your-user-name>Clone the course repository:
git clone https://github.com/razitpl/MachineLearningCourse.gitIf you have already cloned the repository, update it instead:
cd ~/cloudfiles/code/Users/<your-user-name>/MachineLearningCourse/materialy/azure-ml/en git pullClose the terminal tab.
In the JupyterLab file browser, refresh the page if necessary.
Open the following folder:
MachineLearningCourse/materialy/azure-ml/enLocate and open the 01B - Intro to the Azure ML SDK.ipynb notebook for this lab.
Confirm that the notebook uses the Python 3.10 - SDK v2 kernel, or another current Azure ML SDK v2 kernel.
Run the notebook cells one at a time and read the explanations.
Task 3: Connect to an Azure ML Workspace
The Azure Machine Learning SDK v2 uses an MLClient object to connect to and manage Azure Machine Learning workspaces.
You can connect by supplying your subscription ID, resource group name, and workspace name in code. Alternatively, you can download a config.json file from the Azure portal and use it to create the client.
Download the workspace configuration
Open the Azure portal in a new browser tab.
Find and open the Azure Machine Learning workspace created in the previous lab.
On the workspace Overview page, select Download config.json.
Save the downloaded file.
In JupyterLab, upload
config.jsonto the root folder of theMachineLearningCourse/materialy/azure-ml/enrepository.Ensure that
config.jsonis not committed to a public GitHub repository.If the repository does not already contain a
.gitignorefile, create one.Add the following entry to
.gitignore:config.json
Connect with the Azure ML SDK v2
Open a notebook that uses the Python 3.10 - SDK v2 kernel.
Run the following cell:
from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() ml_client = MLClient.from_config( credential=credential ) print(f"Connected to workspace: {ml_client.workspace_name}")If authentication fails, open a JupyterLab terminal and sign in to Azure:
az loginComplete the browser-based sign-in process.
Return to the notebook and run the connection cell again.
When the connection succeeds, verify that the correct Azure Machine Learning workspace name is displayed.
Note:
DefaultAzureCredentialcan use multiple authentication methods. During interactive development, an Azure CLI sign-in is often the simplest method.
Task 4: Explore Azure ML Resources with SDK v2
After connecting to the workspace, you can use MLClient to list and manage Azure Machine Learning resources.
Run the following code to list the available compute resources:
for compute in ml_client.compute.list(): print(f"{compute.name}: {compute.type}")Run the following code to list registered data assets:
for data_asset in ml_client.data.list(): print( f"Name: {data_asset.name}, " f"Version: {data_asset.version}, " f"Type: {data_asset.type}" )Run the following code to list registered models:
for model in ml_client.models.list(): print( f"Name: {model.name}, " f"Version: {model.version}, " f"Type: {model.type}" )Run the following code to list recent Azure Machine Learning jobs:
for job in ml_client.jobs.list(): print( f"Name: {job.name}, " f"Status: {job.status}, " f"Display name: {job.display_name}" )Compare the resources returned by the SDK with the resources displayed in Azure Machine Learning studio.
Note: The SDK v2 lets you manage data assets, environments, compute resources, jobs, models, and endpoints programmatically. You will use these capabilities in later labs.
Task 5: Optional Development with GitHub Codespaces
You can also work with the repository outside Azure Machine Learning Studio by using GitHub Codespaces.
GitHub Codespaces provides a browser-based Visual Studio Code development environment.
Open the MachineLearningCourse/materialy/azure-ml/en repository.
Select Code.
Select the Codespaces tab.
Select Create codespace on main.
Wait for the Codespace to start.
Open the integrated terminal in Visual Studio Code.
Create a Python virtual environment:
python -m venv .venvActivate the virtual environment:
source .venv/bin/activateUpgrade
pipand install the Azure ML SDK v2:python -m pip install --upgrade pip python -m pip install azure-ai-ml azure-identity ipykernelSign in to Azure:
az loginIn Visual Studio Code, select the Python interpreter from the
.venvvirtual environment.Open the lab notebook and select the
.venvPython kernel if prompted.
Note: GitHub Codespaces is optional. You can complete all labs by using JupyterLab on an Azure Machine Learning compute instance.
Clean Up
When you have finished working, stop your compute instance if you do not plan to use it immediately.
Return to Azure Machine Learning studio.
Select Compute.
Open the Compute instances tab.
Select your compute instance.
Select Stop.
Note: Stopping a compute instance prevents further compute charges. You can start it again when you continue working on the labs.