Connect to workspace
from azureml.core import Workspace
ws = Workspace.from_config()
The workspace object is the primary handle on your Azure ML assets, and it's utilised throughout ws
(it's also known as "workspace").
compute_target = ws.compute_targets['<compute-target-name>']
Sample Usage
compute_target = ws.compute_targets['powerful-gpu']
config = ScriptRunConfig(
compute_target=compute_target, # compute target used to run train.py script
source_directory='.',
script='train.py',
)
from azureml.core import Workspace
ws = Workspace.create(name='your_workspace', #this will be created and will be added to your system in Azure ML Studio
subscription_id='<azure-subscription-id>', #make sure that you have it has a Secret
resource_group='your_resourcegroup',
create_resource_group=True,
location='eastus2' #location do refer the documentation of the Azure
)
from azureml.core import Environment
# Option 1. From pip
environment = Environment.from_pip_requirements('<env-name>', '<path/to/requirements.txt - in ml Studio>')
# Option 2. From Conda
environment = Environment.from_conda_specification('<env-name>', '<path/to/env.yml - in ml Studio>')
You can use a pip requirements.txt
file or a Conda env.yml
file to define a Python environment on your compute.
Sample Code
environment = Environment.from_pip_requirements('<env-name>', '<path/to/requirements.txt - in ml Studio>')
config = ScriptRunConfig(
environment=environment, # set the python environment
source_directory='.',
script='train.py',
)
you can get to know more about the command line argument
from azureml.core import ScriptRunConfig
config = ScriptRunConfig(
source_directory='<path/to/code - in ml Studio>', # relative paths okay
script='script.py',
compute_target=compute_target,
environment=environment,
arguments=arguments,
)
To run $ (env) python <path/to/code>/script.py [arguments] - in ml Studio>
on a remote compute cluster target: ComputeTarget
with an environment env: Environment
we can use the ScriptRunConfig
class
from azureml.core import ScriptRunConfig
config = ScriptRunConfig(
source_directory='<path/to/code - in ml Studio>', # relative paths okay
script='script.py',
compute_target=compute_target,
environment=environment,
arguments=arguments,
)
Commands for the Configuration
compute_target
: If not provided the script will run on your local machine/Desktop
environment
: If not provided, uses a default Python environment managed by Azure ML. See Environment.
command = 'echo cool && python script.py'.split()
config = ScriptRunConfig(
source_directory='<path/to/code - in ml Studio>', # relative paths some times will be avaialbe as the Auto Fill
command=command,
compute_target=compute_target,
environment=environment,
arguments=arguments,
)
Experiments in Azure ML
exp = Experiment(ws, '<experiment-name - in ml Studio>')
run = exp.submit(config)
print(run.get_portal_url())
#In Azure ML Studio where you can monitor your run.
cmd
$ conda env create -f env.yml # create environment called pytorch
$ conda activate pytorch
(pytorch) $ cd <path/to/code - in ml Studio>
(pytorch) $ python train.py --learning_rate 0.001 --momentum 0.9
ws = Workspace.from_config()
compute_target = ws.compute_targets['powerful-gpu']
environment = Environment.from_conda_specification('pytorch', 'env.yml')
config = ScriptRunConfig(
source_directory='<path/to/code - in ml Studio>',
script='train.py',
environment=environment,
arguments=['--learning_rate', 0.001, '--momentum', 0.9],
)
run = Experiment(ws, 'PyTorch model training').submit(config)
from azureml.core import Workspace, Experiment, ScriptRunConfig
from azureml.core import Environment
from azureml.core.runconfig import MpiConfiguration
ws = Workspace.from_config()
compute_target = ws.compute_targets['powerful-gpu']
environment = Environment.from_conda_specification('pytorch', 'env.yml')
environment.docker.enabled = True
environment.docker.base_image = 'mcr.microsoft.com/azureml/openmpi3.1.2-cuda10.1-cudnn7-ubuntu18.04'
# train on 2 nodes each with 4 GPUs
mpiconfig = MpiConfiguration(process_count_per_node=4, node_count=2)
config = ScriptRunConfig(
source_directory='<path/to/code - in ml Studio>', # directory containing train.py
script='train.py',
environment=environment,
arguments=['--learning_rate', 0.001, '--momentum', 0.9],
distributed_job_config=mpiconfig, # add the distributed configuration
)
run = Experiment(ws, 'PyTorch model training').submit(config)
This will be for the ScriptRunConfig
Here
mcr.microsoft.com/azureml/openmpi3.1.2-cuda10.1-cudnn7-ubuntu18.04
: is a docker image with OpenMPI. This is required for distributed training on Azure ML.
MpiConfiguration
: is where you specify the number of nodes and GPUs (per node) you want to train on.
datastore = ws.get_default_datastore()
dataset = Dataset.File.from_files(path=(datastore, '<path/on/datastore> - in ml Studio'))
To work with data in your training scripts using your workspace ws
and its default datastore:
Now Just pass this to your training script as a command line argument.
arguments=['--data', dataset.as_mount()]