You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/machine-learning/how-to-train-scikit-learn.md
+88-52Lines changed: 88 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,16 +29,16 @@ Whether you're training a machine learning scikit-learn model from the ground-up
29
29
## Prerequisites
30
30
<!-- M.A: update the prerequisites (path to the notebook) before sign-off -->
31
31
32
-
You can run this code in either an Azure Machine Learning compute instance, or your own Jupyter Notebook:
32
+
You can run this code in either an Azure Machine Learning compute instance, or your own Jupyter Notebook.
33
33
34
34
- Azure Machine Learning compute instance
35
35
- Complete the [Quickstart: Get started with Azure Machine Learning](quickstart-create-resources.md) to create a compute instance. Every compute instance includes a dedicated notebook server pre-loaded with the SDK and the notebooks sample repository.
36
36
- Select the notebook tab in the Azure Machine Learning studio. In the samples training folder, find a completed and expanded notebook by navigating to this directory: **how-to-use-azureml > ml-frameworks > scikit-learn > train-hyperparameter-tune-deploy-with-sklearn** folder.
37
37
- You can use the pre-populated code in the sample training folder to complete this tutorial.
38
38
39
-
- Create a Jupyter Notebook server and run the code in the following sections.
40
-
39
+
- Create a Jupyter notebook server and run the code in the following sections.
41
40
-[Install the Azure Machine Learning SDK (v2)](https://aka.ms/sdk-v2-install).
41
+
-[Create a workspace configuration file](how-to-configure-environment.md#workspace).
42
42
43
43
44
44
## Set up the experiment
@@ -49,10 +49,10 @@ This section sets up the training experiment by loading the required Python pack
49
49
50
50
First, you'll need to connect to your Azure Machine Learning workspace. The [AzureML workspace](concept-workspace.md) is the top-level resource for the service. It provides you with a centralized place to work with all the artifacts you create when you use Azure Machine Learning.
51
51
52
-
We are using `DefaultAzureCredential` to get access to the workspace. `DefaultAzureCredential` should be capable of handling most Azure SDK authentication scenarios.
52
+
We're using `DefaultAzureCredential` to get access to the workspace. This credential should be capable of handling most Azure SDK authentication scenarios.
53
53
54
54
<!-- M.A: link to "configure credential example" is missing (broken in notebook) -->
55
-
If this credential does not work for you, see configure credential example and [`azure-identity reference documentation`](/python/api/azure-identity/azure.identity?view=azure-python) for more available credentials.
55
+
If `DefaultAzureCredential` credential does not work for you, see [`azure-identity reference documentation`](/python/api/azure-identity/azure.identity?view=azure-python) or [`Set up authentication`](/azure/machine-learning/how-to-setup-authentication?tabs=sdk) for more available credentials.
@@ -68,14 +68,15 @@ from azure.identity import InteractiveBrowserCredential
68
68
credential = InteractiveBrowserCredential()
69
69
```
70
70
71
-
Next, get a handle to the workspace by providing your Subscription ID, Resource Group name, and Workspace name. To find your Subscription ID and Resource Group:
71
+
Next, get a handle to the workspace by providing your Subscription ID, Resource Group name, and workspace name. To find these values:
72
72
73
-
1. Select your workspace name from the upper-right corner of the Azure Machine Learning Studio toolbar.
74
-
2. Copy the value for Resource group and Subscription ID into the code.
73
+
1. Find your workspace name in the upper-right corner of the Azure Machine Learning Studio toolbar.
74
+
2. Select your workspace name to show your Resource group and Subscription ID.
75
+
3. Copy the values for Resource group and Subscription ID into the code.
The result of this example script is a workspace handle that you'll use to manage other resources and jobs.
79
+
The result of this script is a workspace handle that you'll use to manage other resources and jobs.
79
80
80
81
Note:
81
82
@@ -85,8 +86,7 @@ Note:
85
86
86
87
AzureML needs a compute resource to run a job. This resource can be single or multi-node machines with Linux or Windows OS, or a specific compute fabric like Spark.
87
88
88
-
<!-- MA: find proper way to link to the marketing page (second link) -->
89
-
In the following example script, we provision a Linux [`compute cluster`](/azure/machine-learning/how-to-create-attach-compute-cluster?tabs=python). You can see the [`Azure Machine Learning pricing`](https://azure.microsoft.com/en-us/pricing/details/machine-learning/) page for the full list of VM sizes and prices. Also, we only need a basic cluster for this example. Let's pick a Standard_DS3_v2 model with 2 vCPU cores and 7 GB RAM to create an AzureML Compute.
89
+
In the following example script, we provision a Linux [`compute cluster`](/azure/machine-learning/how-to-create-attach-compute-cluster?tabs=python). You can see the [`Azure Machine Learning pricing`](https://azure.microsoft.com/pricing/details/machine-learning/) page for the full list of VM sizes and prices. We only need a basic cluster for this example; thus, we'll pick a Standard_DS3_v2 model with 2 vCPU cores and 7 GB RAM to create an AzureML Compute.
@@ -108,68 +108,73 @@ Then, create the file in the dependencies directory. In this example, we've name
108
108
109
109
The specification contains some usual packages (such as numpy and pip) that you'll use in your job.
110
110
111
-
Next, use the YAML file to create and register this custom environment in your workspace.
111
+
Next, use the YAML file to create and register this custom environment in your workspace. The environment will be packaged into a Docker container at runtime.
For more information on creating and using environments, see [Create and use software environments in Azure Machine Learning](how-to-use-environments.md).
116
116
117
-
### Data for training
117
+
## Configure and submit your training job
118
+
119
+
In this section, we'll cover how to run a training job, using a training script that we've provided. To begin, you'll build the training job, configure the command for running a training script, and then submit the training job to run in AzureML.
120
+
118
121
119
-
<!--### Prepare scripts
122
+
### Prepare the training script
120
123
121
-
For this tutorial, we've provided the [training script **train_iris.py**](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/ml-frameworks/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train_iris.py) for you. In practice, you should be able to take any custom training script as is and run it with AzureML without having to modify your code.
124
+
In this article, we've provided the [training script **train_iris.py**](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/ml-frameworks/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train_iris.py) for you. In practice, you should be able to take any custom training script as is and run it with AzureML without having to modify your code.
122
125
123
126
Notes:
124
127
125
-
The provided training script,
126
-
- Shows how to log some metrics to your AzureML run using the `Run` object .
127
-
- Uses example data from the `iris = datasets.load_iris()` function. To use and access your own data, see [how to train with datasets](v1/how-to-train-with-datasets.md) to make data available during training. -->
128
+
The provided training script does the following:
129
+
- shows how to log some metrics to your AzureML run;
130
+
- downloads and extracts the training data using the `iris = datasets.load_iris()` function; and
131
+
- trains a model, then saves and registers it.
128
132
133
+
To use and access your own data, see [how to train with datasets](v1/how-to-train-with-datasets.md) to make data available during training.
129
134
130
-
## Configure and submit your training run
135
+
To use the training script, first create a directory where you will store the file.
131
136
132
-
### Create a ScriptRunConfig
133
-
Create a ScriptRunConfig object to specify the configuration details of your training job, including your training script, environment to use, and the compute target to run on.
134
-
Any arguments to your training script will be passed via command line if specified in the `arguments` parameter.
If you want to instead run your job on a remote cluster, you can specify the desired compute target to the `compute_target` parameter of ScriptRunConfig.
145
+
Now that you have all the assets required to run your job, it's time to build it using the AzureML Python SDK v2. For this, we'll be creating a `command` job.
An AzureML `command` job is a resource that specifies all the details needed to execute your training code in the cloud. These details include the inputs and outputs, type of hardware to use, software to install, and how to run your code. The `command` job contains information to execute a single command.
159
148
160
-
### Submit your run
161
-
```python
162
-
from azureml.core import Experiment
163
149
164
-
run = Experiment(ws,'Tutorial-TrainIRIS').submit(src)
165
-
run.wait_for_completion(show_output=True)
166
-
```
150
+
#### Configure the command
151
+
152
+
You'll use the general purpose `command` to run the training script and perform your desired tasks. Create a Command object to specify the configuration details of your training job. The inputs used in this command include the:
153
+
154
+
- number of epochs, learning rate, momentum and output directory;
155
+
- compute cluster `cpu_compute_target = "cpu-cluster"` that you created earlier for running this command; and
156
+
- custom environment `sklearn-env` that you created earlier for running the AzureML job.
157
+
158
+
You'll also need to configure the following parameter values for input into the `command`:
159
+
160
+
- the command line action itself – in this case, the command is `python train_iris.py`. You can access the inputs and outputs in the command via the `${{ ... }}` notation; and
161
+
- metadata such as the display name and experiment name; where an experiment is a container for all the iterations one does on a certain project. Note that all the jobs submitted under the same experiment name would be listed next to each other in AzureML studio.
Once completed, the job will register a model in your workspace (as a result of training) and output a link for viewing the job in AzureML studio.
167
172
168
173
> [!WARNING]
169
174
> Azure Machine Learning runs training scripts by copying the entire source directory. If you have sensitive data that you don't want to upload, use a [.ignore file](how-to-save-write-experiment-files.md#storage-limits-of-experiment-snapshots) or don't include it in the source directory . Instead, access your data using an Azure ML [dataset](v1/how-to-train-with-datasets.md).
170
175
171
-
### What happens during run execution
172
-
As the run is executed, it goes through the following stages:
176
+
### What happens during job execution
177
+
As the job is executed, it goes through the following stages:
173
178
174
179
-**Preparing**: A docker image is created according to the environment defined. The image is uploaded to the workspace's container registry and cached for later runs. Logs are also streamed to the run history and can be viewed to monitor progress. If a curated environment is specified instead, the cached image backing that curated environment will be used.
175
180
@@ -179,9 +184,40 @@ As the run is executed, it goes through the following stages:
179
184
180
185
-**Post-Processing**: The **./outputs** folder of the run is copied over to the run history.
181
186
182
-
## Save and register the model
187
+
## Tune model hyperparameters
183
188
184
-
Once you've trained the model, you can save and register it to your workspace. Model registration lets you store and version your models in your workspace to simplify [model management and deployment](concept-model-management-and-deployment.md).
189
+
Now that we've seen how to do a simple Scikit-learn training run using the SDK, let's see if we can further improve the accuracy of our model. We can optimize our model's hyperparameters using Azure Machine Learning's [`sweep`](python/api/azure-ai-ml/azure.ai.ml.sweep?view=azure-python-preview) capabilities.
190
+
191
+
To tune the model's hyperparameters, you'll define the parameter space in which to search during training. You'll do this by replacing some of the parameters (`kernel` and `penalty`) passed to the training job with special inputs from the `azure.ml.sweep` package.
Then you'll configure sweep on the command job, using some sweep-specific parameters, such as the primary metric to watch and the sampling algorithm to use.
196
+
197
+
In the following code we use random sampling to try different configuration sets of hyperparameters in an attempt to maximize our primary metric, `Accuracy`.
you've trained the model, you can save and register it to your workspace. Model registration lets you store and version your models in your workspace to simplify [model management and deployment](concept-model-management-and-deployment.md).
185
221
186
222
Add the following code to your training script, train_iris.py, to save the model.
187
223
@@ -202,7 +238,7 @@ model = run.register_model(model_name='sklearn-iris',
0 commit comments