Skip to content

Commit 80bba4c

Browse files
committed
code for amlcompute section
1 parent 3c581b5 commit 80bba4c

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Code for Azure Machine Learning Compute - Run-based creation
2+
3+
# Check core SDK version number
4+
import azureml.core
5+
6+
print("SDK version:", azureml.core.VERSION)
7+
8+
9+
from azureml.core import Workspace
10+
ws = Workspace.from_config()
11+
12+
13+
# Set up an experiment
14+
from azureml.core import Experiment
15+
experiment_name = 'my-experiment'
16+
script_folder= "./"
17+
18+
exp = Experiment(workspace=ws, name=experiment_name)
19+
20+
21+
#<amlcompute_temp>
22+
from azureml.core.compute import ComputeTarget, AmlCompute
23+
24+
# First, list the supported VM families for Azure Machine Learning Compute
25+
print(AmlCompute.supported_vmsizes(workspace=ws))
26+
27+
from azureml.core.runconfig import RunConfiguration
28+
# Create a new runconfig object
29+
run_temp_compute = RunConfiguration()
30+
31+
# Signal that you want to use AmlCompute to execute the script
32+
run_temp_compute.target = "amlcompute"
33+
34+
# AmlCompute is created in the same region as your workspace
35+
# Set the VM size for AmlCompute from the list of supported_vmsizes
36+
run_temp_compute.amlcompute.vm_size = 'STANDARD_D2_V2'
37+
#</amlcompute_temp>
38+
39+
40+
# Submit the experiment using the run configuration
41+
from azureml.core import ScriptRunConfig
42+
43+
src = ScriptRunConfig(source_directory = script_folder, script = 'train.py', run_config = run_temp_compute)
44+
run = exp.submit(src)
45+
run.wait_for_completion(show_output = True)
46+
47+
48+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Code for Azure Machine Learning Compute - Persistent compute
2+
3+
# Check core SDK version number
4+
import azureml.core
5+
6+
print("SDK version:", azureml.core.VERSION)
7+
8+
from azureml.core import Workspace
9+
ws = Workspace.from_config()
10+
11+
12+
# Set up an experiment
13+
from azureml.core import Experiment
14+
experiment_name = 'my-experiment'
15+
script_folder= "./"
16+
17+
exp = Experiment(workspace=ws, name=experiment_name)
18+
19+
#<cpu_basic>
20+
from azureml.core.compute import ComputeTarget, AmlCompute
21+
from azureml.core.compute_target import ComputeTargetException
22+
23+
# Choose a name for your CPU cluster
24+
cpu_cluster_name = "cpucluster"
25+
26+
# Verify that cluster does not exist already
27+
try:
28+
cpu_cluster = ComputeTarget(workspace=ws, name=cpu_cluster_name)
29+
print('Found existing cluster, use it.')
30+
except ComputeTargetException:
31+
compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D2_V2',
32+
max_nodes=4)
33+
cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, compute_config)
34+
35+
cpu_cluster.wait_for_completion(show_output=True)
36+
#</cpu_basic>
37+
38+
#<aml_runconfig>
39+
from azureml.core.runconfig import RunConfiguration
40+
from azureml.core.conda_dependencies import CondaDependencies
41+
from azureml.core.runconfig import DEFAULT_CPU_IMAGE
42+
43+
# Create a new runconfig object
44+
run_amlcompute = RunConfiguration()
45+
46+
# Use the cpu_cluster you created above.
47+
run_amlcompute.target = cpu_cluster
48+
49+
# Enable Docker
50+
run_amlcompute.environment.docker.enabled = True
51+
52+
# Set Docker base image to the default CPU-based image
53+
run_amlcompute.environment.docker.base_image = DEFAULT_CPU_IMAGE
54+
55+
# Use conda_dependencies.yml to create a conda environment in the Docker image for execution
56+
run_amlcompute.environment.python.user_managed_dependencies = False
57+
58+
# Auto-prepare the Docker image when used for execution (if it is not already prepared)
59+
run_amlcompute.auto_prepare_environment = True
60+
61+
# Specify CondaDependencies obj, add necessary packages
62+
run_amlcompute.environment.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'])
63+
#</aml_runconfig>
64+
65+
# Submit the experiment using the run configuration
66+
#<amlcompute_submit>
67+
from azureml.core import ScriptRunConfig
68+
69+
src = ScriptRunConfig(source_directory = script_folder, script = 'train.py', run_config = run_amlcompute)
70+
run = exp.submit(src)
71+
run.wait_for_completion(show_output = True)
72+
#</amlcompute_submit>

0 commit comments

Comments
 (0)