|
| 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