<jupyter_start><jupyter_text>Tutorial: Train your first ML model (Part 2 of 3) IntroductionIn the [previous tutorial](1.hello-world.ipynb), you ran a trivial "hello world!" script in the cloud using Azure Machine Learning's Python SDK. This time you take it a step further by submitting a script that will train a machine learning model. This example will help you understand how Azure Machine Learning eases consistent behavior between debugging on a compute instance or laptop development environment, and remote runs.Learning these concepts means that by the end of this session, you can:* Use Conda to define an Azure Machine Learning environment* Train a model in the cloud* Log metrics to Azure Machine Learning--- Your machine learning codeThis tutorial shows you how to train a PyTorch model on the CIFAR 10 dataset using an Azure Machine Learning Cluster. In this case you will be using a CPU cluster, but this could equally be a GPU cluster. Whilst this tutorial uses PyTorch, the steps we show you apply to *any* machine learning code. In the `../../code/models/pytorch/cifar10-cnn` subdirectory you will see 2 files:1. [model.py](../../code/models/pytorch/cifar10-cnn/model.py) - this defines the neural network architecture1. [train.py](../../code/models/pytorch/cifar10-cnn/train.py) - This is the training script. This script downloads the CIFAR10 dataset using PyTorch `torchvision.dataset` APIs, sets up the network defined in`model.py`, and trains it for two epochs using standard SGD and cross-entropy loss.Note the code is based on [this introductory example from PyTorch](https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html).   Define the Python environment for your machine learning codeFor demonstration purposes, we're going to use a Conda environment but the steps for a pip virtual environment are almost identical. This environment has all the dependencies that your model and training script require. In the `../../environments` directory there is a *conda dependencies* file called [pytorch-example.yml](../../environments/pytorch-example.yml) that specifies the dependencies to run the python code.   Submit your machine learning code to Azure Machine LearningThe difference to the control script below and the one used to submit "hello world" is that you adjust the environment to be set from the conda dependencies file you created earlier.> ! NOTE > The first time you run this script, Azure Machine Learning will build a new docker image from your PyTorch environment. The whole run could take 5-10 minutes to complete. You can see the docker build logs in the widget by selecting the `20_image_build_log.txt` in the log files dropdown. This image will be reused in future runs making them run much quicker.<jupyter_code>import git
from pathlib import Path

prefix = Path(git.Repo(".", search_parent_directories=True).working_tree_dir)
prefix
from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig
from azureml.widgets import RunDetails

ws = Workspace.from_config()
env = Environment.from_conda_specification(
    name="pytorch-env-tutorial",
    file_path=prefix.joinpath("environments", "pytorch-example.yml"),
)
exp = Experiment(workspace=ws, name="getting-started-train-model-tutorial")
src = ScriptRunConfig(
    source_directory=prefix.joinpath(
        "code", "models", "pytorch", "cifar10-cnn"
    ),
    script="train.py",
    compute_target="cpu-cluster",
    environment=env,
)

run = exp.submit(src)
RunDetails(run).show()<jupyter_output><empty_output><jupyter_text>Understand the control codeCompared to the control script that submitted the "hello world" example, this control script introduces the following:| Code | Description| --- | --- || `env = Environment.from_conda_specification( ...)` | Azure Machine Learning provides the concept of an `Environment` to represent a reproducible, versioned Python environment for running experiments. Here you have created it from a yaml conda dependencies file.| **There are many ways to create AML environments, including [from a pip requirements.txt](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py&preserve-view=truefrom-pip-requirements-name--file-path-), or even [from an existing local Conda environment](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py&preserve-view=truefrom-existing-conda-environment-name--conda-environment-name-).** Once your image is built, select `70_driver_log.txt` to see the output of your training script, which should look like:```txtDownloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data/cifar-10-python.tar.gz...Files already downloaded and verifiedepoch=1, batch= 2000: loss 2.19...epoch=2, batch=12000: loss 1.27Finished Training```Environments can be registered to a workspace with `env.register(ws)`, allowing them to be easily shared, reused, and versioned. Environments make it easy to reproduce previous results and to collaborate with your team.Azure Machine Learning also maintains a collection of curated environments. These environments cover common ML scenarios and are backed by cached Docker images. Cached Docker images make the first remote run faster.In short, using registered environments can save you time! More details can be found on the [environments documentation](./how-to-use-environments.md).  Log training metricsNow that you have a model training in Azure Machine Learning, start tracking some performance metrics.The current training script prints metrics to the terminal. Azure Machine Learning provides amechanism for logging metrics with more functionality. By adding a few lines of code, you gain the ability to visualize metrics in the studio and to compare metrics between multiple runs. Machine learning code updatesIn the `../../code/models/pytorch/cifar10-cnn` directory you will notice the [train-with-logging.py](../../code/models/pytorch/cifar10-cnn/train-with-logging.py) script has been modified with one additional line that will log the loss to the Azure Machine Learning Studio:```python in train.pyimport mlflow...mlflow.log_metric("loss", loss)```Metrics in Azure Machine Learning are:- Organized by experiment and run so it's easy to keep track of andcompare metrics- Equipped with a UI so we can visualize training performance in the studio or in the notebook widget- **Designed to scale** You can submit concurrent experiments and the Azure Machine Learning cluster will scale out (up to the maximum node count of the cluster) to run the experiments in parallel  Submit your machine learning code to Azure Machine LearningSubmit your code once more. This time the widget includes the metrics where you can now see live updates on the model training loss!<jupyter_code>from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig
from azureml.widgets import RunDetails

ws = Workspace.from_config()
env = Environment.from_conda_specification(
    name="pytorch-env-tutorial",
    file_path=prefix.joinpath("environments", "pytorch-example.yml"),
)
exp = Experiment(
    workspace=ws, name="getting-started-train-model-with-logging-tutorial"
)
src = ScriptRunConfig(
    source_directory=prefix.joinpath(
        "code", "models", "pytorch", "cifar10-cnn"
    ),
    script="train-with-logging.py",
    compute_target="cpu-cluster",
    environment=env,
)

run = exp.submit(src)
RunDetails(run).show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08052259698706839, 'fraction_numerical': 0.007199040127982935, 'mean_word_length': 5.016038492381716, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '122213', 'license': 'MIT', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1903, 'n_words': 852, 'path': 'tutorials/getting-started-train/2.pytorch-model.ipynb', 'repo_name': 'datashinobi/azureml-examples', 'size': '7501'}