Azure Machine Learning

A workspace is a context for the experiments, data, compute targets, and other assets associated with a machine learning workload.

Workspaces for Machine Learning Assets

A workspace defines the boundary for a set of related machine learning assets. You can use workspaces to group machine learning assets based on projects, deployment environments (for example, test and production), teams, or some other organizing principle. The assets in a workspace include:

  • Compute targets for development, training, and deployment.
  • Data for experimentation and model training.
  • Notebooks containing shared code and documentation.
  • Experiments, including run history with logged metrics and outputs.
  • Pipelines that define orchestrated multi-step processes.
  • Models that you have trained.

Workspaces as Azure Resources

Workspaces are Azure resources, and as such they are defined within a resource group in an Azure subscription, along with other related Azure resources that are required to support the workspace.

An Azure Machine Learning workspace

The Azure resources created alongside a workspace include:

  • A storage account – used to store files used by the workspace as well as data for experiments and model training.
  • An Application Insights instance, used to monitor predictive services in the workspace.
  • An Azure Key Vault instance, used to manage secrets such as authentication keys and credentials used by the workspace.
  • Virtual Machines, and their associated virtual hardware resources, used to provide compute for notebook development in the workspace.
  • A container registry, used to manage containers for deployed models.

Role-Based Access Control

You can assign role-based authorization policies to a workspace, enabling you to manage permissions that restrict what actions specific Azure Active Directory (AAD) principals can perform. For example, you could create a policy that allows only users in the IT Operations group to create compute targets and datastores, while allowing users in the Data Scientists group to create and run experiments and register models.

Creating a Workspace

You can create a workspace in any of the following ways:

  • In the Microsoft Azure portal, create a new Machine Learning resource, specifying the subscription, resource group, workspace name, and workspace edition.
  • Use the Azure Machine Learning Python SDK to run code that creates a workspace. For example:

from azureml.core import Workspace

ws = Workspace.create(name='aml-workspace', 
                  subscription_id='123456-abc-123...',
                  resource_group='aml-resources',
                  create_resource_group=True,
                  location='eastus',
                  sku='enterprise'
                 )

Use the Azure Command Line Interface (CLI) with the Azure Machine Learning CLI extension. For example, you could use the following command (which assumes a resource group named aml-resources has already been created):

az ml workspace create -w ‘aml-workspace’ -g ‘aml-resources’

region and edition are most important in case of creating a machine learning studio.

Azure Machine Learning tools and interfaces

Azure Machine Learning studio

You can manage the assets in your Azure Machine Learning workspace in the Azure portal, but as this is a general interface for managing all kinds of resources in Azure, data scientists and other users involved in machine learning operations may prefer to use a more focused, dedicated interface.

Azure Machine Learning studio

Azure Machine Learning studio is a web-based tool for managing an Azure Machine Learning workspace. It enables you to create, manage, and view all of the assets in your workspace and provides the following graphical tools:

  • Designer: A drag and drop interface for “no code” machine learning model development.
  • Automated Machine Learning: A wizard interface that enables you to train a model using a combination of algorithms and data preprocessing techniques to find the best model for your data.

The Azure Machine Learning SDK

While graphical interfaces like Azure Machine Learning studio make it easy to create and manage machine learning assets, it is often advantageous to use a code-based approach to managing resources. By writing scripts to create and manage resources, you can:

  • Run machine learning operations from your preferred development environment.
  • Automate asset creation and configuration to make it repeatable.
  • Ensure consistency for resources that must be replicated in multiple environments (for example, development, test, and production)
  • Incorporate machine learning asset configuration into developer operations (DevOps) workflows, such as continuous integration / continuous deployment (CI/CD) pipelines.

Azure Machine Learning provides software development kits (SDKs) for Python and R, which you can use to create, manage, and use assets in an Azure Machine Learning workspace.

Installing the Azure Machine Learning SDK for Python

You can install the Azure Machine Learning SDK for Python by using the pip package management utility, as shown in the following code sample:bashCopy

pip install azureml-sdk

The SDK includes optional extras that aren’t required for core operations, but can be useful in some scenarios. For example, the **notebooks extra include widgets for displaying detailed output in Jupyter Notebooks, the automl extra includes packages for automated machine learning training, and the explain extra includes packages for generating model explanations. To install extras, specify them in brackets as shown here:bashCopy

pip install azureml-sdk[notebooks,automl,explain]

More Information: For more information about installing the Azure Machine Learning SDK for Python, see the SDK documentation. Also, you should be aware that the SDK is updated on a regular basis, and review the release notes for the latest release.

Connecting to a Workspace

After installing the SDK package in your Python environment, you can write code to connect to your workspace and perform machine learning operations. The easiest way to connect to a workspace is to use a workspace configuration file, which includes the Azure subscription, resource group, and workspace details as shown here:JSONCopy

{
    "subscription_id": "1234567-abcde-890-fgh...",
    "resource_group": "aml-resources",
    "workspace_name": "aml-workspace"
}

 Tip

You can download a configuration file for a workspace from the Overview page of its blade in the Azure portal.

To connect to the workspace using the configuration file, you can use the from_config method of the Workspace class in the SDK, as shown here:PythonCopy

from azureml.core import Workspace

ws = Workspace.from_config()

By default, the from_config method looks for a file named config.json in the folder containing the Python code file, but you can specify another path if necessary.

As an alternative to using a configuration file, you can use the get method of the Workspace class with explicitly specified subscription, resource group, and workspace details as shown here – though the configuration file technique is generally preferred due to its greater flexibility when using multiple scripts:PythonCopy

from azureml.core import Workspace

ws = Workspace.get(name='aml-workspace',
                   subscription_id='1234567-abcde-890-fgh...',
                   resource_group='aml-resources')

Whichever technique you use, if there is no current active session with your Azure subscription, you will be prompted to authenticate.

Working with the Workspace Class

The Workspace class is the starting point for most code operations. For example, you can use its compute_targets attribute to retrieve a dictionary object containing the compute targets defined in the workspace, like this:PythonCopy

for compute_name in ws.compute_targets:
    compute = ws.compute_targets[compute_name]
    print(compute.name, ":", compute.type)

The SDK contains a rich library of classes that you can use to create, manage, and use many kinds of asset in an Azure Machine Learning workspace.

Compute Instances

Azure Machine Learning includes the ability to create Compute Instances in a workspace to provide a development environment that is managed with all of the other assets in the workspace.

Notebook VM

Compute Instances include Jupyter Notebook and JupyterLab installations that you can use to write and run code that uses the Azure Machine Learning SDK to work with assets in your workspace.

You can choose a VM image that provides the compute specification you need, from small CPU-only VMs to large GPU-enabled workstations. Because the VMs are hosted in Azure, you only pay for the compute resources when they are running; so you can create a VM to suit your needs, and stop it when your workload has completed to minimize costs.

You can store notebooks independently in workspace storage, and open them in any VM.

Azure Machine Learning experiments

Like any scientific discipline, data science involves running experiments; typically to explore data or to build and evaluate predictive models. In Azure Machine Learning, an experiment is a named process, usually the running of a script or a pipeline, that can generate metrics and outputs and be tracked in the Azure Machine Learning workspace.

An experiment run generating metrics and outputs

An experiment can be run multiple times, with different data, code, or settings; and Azure Machine Learning tracks each run, enabling you to view run history and compare results for each run.

The Experiment Run Context

When you submit an experiment, you use its run context to initialize and end the experiment run that is tracked in Azure Machine Learning, as shown in the following code sample:

from azureml.core import Experiment

create an experiment variable

experiment = Experiment(workspace = ws, name = “my-experiment”)

start the experiment

run = experiment.start_logging()

experiment code goes here

end the experiment

run.complete()

Logging Metrics and Creating Outputs

Experiments are most useful when they produce metrics and outputs that can be tracked across runs.

Logging Metrics

Every experiment generates log files that include the messages that would be written to the terminal during interactive execution. This enables you to use simple print statements to write messages to the log. However, if you want to record named metrics for comparison across runs, you can do so by using the Run object; which provides a range of logging functions specifically for this purpose. These include:

  • log: Record a single named value.
  • log_list: Record a named list of values.
  • log_row: Record a row with multiple columns.
  • log_table: Record a dictionary as a table.
  • log_image: Record an image file or a plot.

Retrieving and Viewing Logged Metrics

You can view the metrics logged by an experiment run in Azure Machine Learning studio or by using the RunDetails widget in a notebook, as shown here:PythonCopy

from azureml.widgets import RunDetails

RunDetails(run).show()

You can also retrieve the metrics using the Run object’s get_metrics method, which returns a JSON representation of the metrics, as shown here:PythonCopy

import json

# Get logged metrics
metrics = run.get_metrics()
print(json.dumps(metrics, indent=2))

The previous code produces output similar to this:JSONCopy

{
  "observations": 15000
}

Experiment Output Files

In addition to logging metrics, an experiment can generate output files. Often these are trained machine learning models, but you can save any sort of file and make it available as an output of your experiment run. The output files of an experiment are saved in its outputs folder.

The technique you use to add files to the outputs of an experiment depend on how you’re running the experiment. The examples shown so far control the experiment lifecycle inline in your code, and when taking this approach you can upload local files to the run’s outputs folder by using the Run object’s upload_file method in your experiment code as shown here:PythonCopy

run.upload_file(name='outputs/sample.csv', path_or_stream='./sample.csv')

When running an experiment in a remote compute context (which we’ll discuss later in this course), any files written to the outputs folder in the compute context are automatically uploaded to the run’s outputs folder when the run completes.

Whichever approach you use to run your experiment, you can retrieve a list of output files from the Run object like this:PythonCopy

import json

files = run.get_file_names()
print(json.dumps(files, indent=2))

The previous code produces output similar to this:JSONCopy

[
  "outputs/sample.csv"
]

Running a Script as an Experiment

You can run an experiment inline using the start_logging method of the Experiment object, but it’s more common to encapsulate the experiment logic in a script and run the script as an experiment. The script can be run in any valid compute context, making this a more flexible solution for running experiments as scale.

An experiment script is just a Python code file that contains the code you want to run in the experiment. To access the experiment run context (which is needed to log metrics) the script must import the azureml.core.Run class and call its get_context method. The script can then use the run context to log metrics, upload files, and complete the experiment, as shown here:PythonCopy

from azureml.core import Run
import pandas as pd
import matplotlib.pyplot as plt
import os

# Get the experiment run context
run = Run.get_context()

# load the diabetes dataset
data = pd.read_csv('data.csv')

# Count the rows and log the result
row_count = (len(data))
run.log('observations', row_count)

# Save a sample of the data
os.makedirs('outputs', exist_ok=True)
data.sample(100).to_csv("outputs/sample.csv", index=False, header=True)

# Complete the run
run.complete()

To run a script as an experiment, you must define a run configuration that defines the Python environment in which the script will be run, and a script run configuration that associates the run environment with the script. These are implemented by using the RunConfiguration and ScriptRunConfig objects.

For example, the following code could be used to run an experiment based on a script in the experiment_files folder (which must also contain any files used by the script, such as the data.csv file in previous script code example):PythonCopy

from azureml.core import Experiment, RunConfiguration, ScriptRunConfig

# create a new RunConfig object
experiment_run_config = RunConfiguration()

# Create a script config
script_config = ScriptRunConfig(source_directory=experiment_folder, 
                      script='experiment.py',
                      run_config=experiment_run_config) 

# submit the experiment
experiment = Experiment(workspace = ws, name = 'my-experiment')
run = experiment.submit(config=script_config)
run.wait_for_completion(show_output=True)

github

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s