Skip to content

Commit c110a0b

Browse files
committed
more updates to article
1 parent 9b8f68d commit c110a0b

File tree

1 file changed

+88
-52
lines changed

1 file changed

+88
-52
lines changed

articles/machine-learning/how-to-train-scikit-learn.md

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ Whether you're training a machine learning scikit-learn model from the ground-up
2929
## Prerequisites
3030
<!-- M.A: update the prerequisites (path to the notebook) before sign-off -->
3131

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

3434
- Azure Machine Learning compute instance
3535
- 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.
3636
- 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.
3737
- You can use the pre-populated code in the sample training folder to complete this tutorial.
3838

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.
4140
- [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).
4242

4343

4444
## Set up the experiment
@@ -49,10 +49,10 @@ This section sets up the training experiment by loading the required Python pack
4949

5050
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.
5151

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

5454
<!-- 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.
5656

5757
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=credential)]
5858

@@ -68,14 +68,15 @@ from azure.identity import InteractiveBrowserCredential
6868
credential = InteractiveBrowserCredential()
6969
```
7070

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

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

7677
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=ml_client)]
7778

78-
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.
7980

8081
Note:
8182

@@ -85,8 +86,7 @@ Note:
8586

8687
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.
8788

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

9191
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=cpu_compute_target)]
9292

@@ -108,68 +108,73 @@ Then, create the file in the dependencies directory. In this example, we've name
108108

109109
The specification contains some usual packages (such as numpy and pip) that you'll use in your job.
110110

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

113113
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=custom_environment)]
114114

115115
For more information on creating and using environments, see [Create and use software environments in Azure Machine Learning](how-to-use-environments.md).
116116

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

119-
<!-- ### Prepare scripts
122+
### Prepare the training script
120123

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

123126
Notes:
124127

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

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

130-
## Configure and submit your training run
135+
To use the training script, first create a directory where you will store the file.
131136

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.
137+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=make_src_folder)]
135138

136-
The following code will configure a ScriptRunConfig object for submitting your job for execution on your local machine.
139+
Next, create the script file in the source directory.
137140

138-
```python
139-
from azureml.core import ScriptRunConfig
141+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=create_script_file)]
140142

141-
src = ScriptRunConfig(source_directory='.',
142-
script='train_iris.py',
143-
arguments=['--kernel', 'linear', '--penalty', 1.0],
144-
environment=sklearn_env)
145-
```
143+
### Build the training job
146144

147-
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.
148146

149-
```python
150-
from azureml.core import ScriptRunConfig
151-
152-
compute_target = ws.compute_targets['<my-cluster-name>']
153-
src = ScriptRunConfig(source_directory='.',
154-
script='train_iris.py',
155-
arguments=['--kernel', 'linear', '--penalty', 1.0],
156-
compute_target=compute_target,
157-
environment=sklearn_env)
158-
```
147+
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.
159148

160-
### Submit your run
161-
```python
162-
from azureml.core import Experiment
163149

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.
162+
163+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=job)]
164+
165+
### Submit the job
166+
167+
It's now time to submit the job to run in AzureML. This time you'll use `create_or_update` on `ml_client.jobs`.
168+
169+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=create_job)]
170+
171+
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.
167172

168173
> [!WARNING]
169174
> 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).
170175
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:
173178

174179
- **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.
175180

@@ -179,9 +184,40 @@ As the run is executed, it goes through the following stages:
179184

180185
- **Post-Processing**: The **./outputs** folder of the run is copied over to the run history.
181186

182-
## Save and register the model
187+
## Tune model hyperparameters
183188

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.
192+
193+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=job_for_sweep)]
194+
195+
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`.
198+
199+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=sweep_job)]
200+
201+
Now you can submit this job as before. This time, you'll be running a sweep job that sweeps over your train job.
202+
203+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=create_sweep_job)]
204+
205+
You can monitor the job by using the studio user interface link that is presented during the job run.
206+
207+
208+
## Find and register the best model
209+
210+
Once all the runs complete, you can find the run that produced the model with the highest accuracy.
211+
212+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=model)]
213+
214+
You can now register this model.
215+
216+
[!notebook-python[](~/azureml-examples-v2samplesreorg/sdk/python/jobs/single-step/scikit-learn/train-hyperparameter-tune-deploy-with-sklearn/train-hyperparameter-tune-with-sklearn.ipynb?name=register_model)]
217+
218+
<!--
219+
220+
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).
185221
186222
Add the following code to your training script, train_iris.py, to save the model.
187223
@@ -202,7 +238,7 @@ model = run.register_model(model_name='sklearn-iris',
202238
model_framework=Model.Framework.SCIKITLEARN,
203239
model_framework_version='0.19.1',
204240
resource_configuration=ResourceConfiguration(cpu=1, memory_in_gb=0.5))
205-
```
241+
``` -->
206242

207243
## Deployment
208244

0 commit comments

Comments
 (0)