Running Code in the Cloud
#
Experiments and RunsAzure ML is a machine-learning service that facilitates running your code in
the cloud. A Run
is an abstraction layer around each such submission, and is used to
monitor the job in real time as well as keep a history of your results.
- Run: A run represents a single execution of your code. See Run for more details.
- Experiments: An experiment is a light-weight container for
Run
. Use experiments to submit and track runs.
Create an experiment in your workspace ws
.
#
ScriptRunConfigA common way to run code in the cloud is via the ScriptRunConfig
which packages
your source code (Script) and run configuration (RunConfig).
Consider the following layout for your code.
To run script.py
in the cloud via the ScriptRunConfig
where:
source_directory='source_directory'
: Local directory with your code.script='script.py'
: Script to run. This does not need to be at the root ofsource_directory
.compute_taget=target
: See Compute Targetenvironment
: See Environmentarguments
: See Arguments
Submit this code to Azure with
This will present you with a link to monitor your run on the web (https://ml.azure.com) as well as streaming logs to your terminal.
#
Command Line ArgumentsTo pass command line arguments to your script use the arguments
parameter in ScriptRunConfig
.
Arguments are specified as a list:
which are then passed to the script as command-line arguments as follows:
This also supports using named arguments:
Arguments can be of type int
, float
str
and can also be used to reference data.
For more details on referencing data via the command line: Use dataset in a remote run
sys.argv
#
Example: In this example we pass two arguments to our script. If we were running this from the console:
To mimic this command using argument
in ScriptRunConfig
:
which can be consumed as usual in our script:
argparse
#
Example: In this example we pass two named arguments to our script. If we were running this from the console:
To mimic this behavior in ScriptRunConfig
:
which can be consumed as usual in our script:
#
CommandsIt is possible to provide the explicit command to run.
This example is equivalent to setting the argument script='script.py'
in place of
the command
argument.
This option provides a lot of flexibility. For example:
Set environment variables: Some useful examples:
Run setup script: Run a setup script e.g. to download data, set environment variables.
#
Using Datasets#
via ArgumentsPass a dataset to your ScriptRunConfig as an argument
This mounts the dataset to the run where it can be referenced by script.py
.
#
Run#
InteractiveIn an interactive setting e.g. a Jupyter notebook
#
Example: Jupyter notebookA common use case for interactive logging is to train a model in a notebook.
Follow the link to the run to see the metric logging in real time.
#
Get ContextCode that is running within Azure ML is associated to a Run
. The submitted code
can access its own run.
#
Example: Logging metrics to current run contextA common use-case is logging metrics in a training script.
When this code is submitted to Azure ML (e.g. via ScriptRunConfig) it will log metrics to its associated run.
For more details: Logging Metrics