<jupyter_start><jupyter_text>Copyright (c) Microsoft Corporation. All rights reserved.Licensed under the MIT License.  03. Train on Azure Container Instance* Create Workspace* Create `train.py` in the project folder.* Configure an ACI (Azure Container Instance) run* Execute in ACI  PrerequisitesMake sure you go through the [00. Installation and Configuration](00.configuration.ipynb) Notebook first if you haven't.<jupyter_code># Check core SDK version number
import azureml.core

print("SDK version:", azureml.core.VERSION)<jupyter_output><empty_output><jupyter_text>Initialize WorkspaceInitialize a workspace object from persisted configuration<jupyter_code>from azureml.core import Workspace

ws = Workspace.from_config()
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\n')<jupyter_output><empty_output><jupyter_text>Create An Experiment**Experiment** is a logical container in an Azure ML Workspace. It hosts run records which can include run metrics and output artifacts from your experiments.<jupyter_code>from azureml.core import Experiment
experiment_name = 'train-on-aci'
experiment = Experiment(workspace = ws, name = experiment_name)<jupyter_output><empty_output><jupyter_text>Remote execution on ACIThe training script `train.py` is already created for you. Let's have a look.<jupyter_code>with open('./train.py', 'r') as f:
    print(f.read())<jupyter_output><empty_output><jupyter_text>Configure for using ACILinux-based ACI is available in `West US`, `East US`, `West Europe`, `North Europe`, `West US 2`, `Southeast Asia`, `Australia East`, `East US 2`, and `Central US` regions. See details [here](https://docs.microsoft.com/en-us/azure/container-instances/container-instances-quotasregion-availability).<jupyter_code>from azureml.core.runconfig import RunConfiguration
from azureml.core.conda_dependencies import CondaDependencies

# create a new runconfig object
run_config = RunConfiguration()

# signal that you want to use ACI to execute script.
run_config.target = "containerinstance"

# ACI container group is only supported in certain regions, which can be different than the region the Workspace is in.
run_config.container_instance.region = 'eastus2'

# set the ACI CPU and Memory 
run_config.container_instance.cpu_cores = 1
run_config.container_instance.memory_gb = 2

# enable Docker 
run_config.environment.docker.enabled = True

# set Docker base image to the default CPU-based image
run_config.environment.docker.base_image = azureml.core.runconfig.DEFAULT_CPU_IMAGE

# use conda_dependencies.yml to create a conda environment in the Docker image for execution
run_config.environment.python.user_managed_dependencies = False

# auto-prepare the Docker image when used for execution (if it is not already prepared)
run_config.auto_prepare_environment = True

# specify CondaDependencies obj
run_config.environment.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'])<jupyter_output><empty_output><jupyter_text>Submit the ExperimentFinally, run the training job on the ACI<jupyter_code>%%time 
from azureml.core.script_run_config import ScriptRunConfig

script_run_config = ScriptRunConfig(source_directory='./',
                                    script='train.py',
                                    run_config=run_config)

run = experiment.submit(script_run_config)
# Show run details
run
%%time
# Shows output of the run on stdout.
run.wait_for_completion(show_output=True)
# get all metris logged in the run
run.get_metrics()
metrics = run.get_metrics()
import numpy as np
print('When alpha is {1:0.2f}, we have min MSE {0:0.2f}.'.format(
    min(metrics['mse']), 
    metrics['alpha'][np.argmin(metrics['mse'])]
))
# show all the files stored within the run record
run.get_file_names()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08962264150943396, 'fraction_numerical': 0.004454926624737945, 'mean_word_length': 5.626736111111111, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '156781', 'license': 'MIT', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1115, 'n_words': 342, 'path': '01.getting-started/03.train-on-aci/03.train-on-aci.ipynb', 'repo_name': 'msdavx/MachineLearningNotebooks', 'size': '3816'}