Skip to content

Commit 3e5de52

Browse files
authored
Merge pull request #103261 from sdgilley/notebook-snippets
add notebook snippets
2 parents 24e5e6c + 991010c commit 3e5de52

File tree

3 files changed

+26
-140
lines changed

3 files changed

+26
-140
lines changed

.openpublishing.publish.config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@
277277
"url": "https://github.com/Azure/pcs-remote-monitoring-webui.git",
278278
"branch": "master"
279279
},
280+
{
281+
"path_to_root": "MachineLearningNotebooks",
282+
"url": "https://github.com/Azure/MachineLearningNotebooks",
283+
"branch": "master"
284+
},
280285
{
281286
"path_to_root": "aml-sdk-samples",
282287
"url": "https://github.com/Azure/MachineLearningNotebooks",

articles/machine-learning/how-to-create-labeling-projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In this article, you'll learn how to:
4040
* The data that you want to label, either in local files or in Azure blob storage.
4141
* The set of labels that you want to apply.
4242
* The instructions for labeling.
43-
* An Azure subscription. If you dont have an Azure subscription, create a [free account](https://aka.ms/AMLFree) before you begin.
43+
* An Azure subscription. If you don't have an Azure subscription, create a [free account](https://aka.ms/AMLFree) before you begin.
4444
* A Machine Learning workspace. See [Create an Azure Machine Learning workspace](how-to-manage-workspace.md).
4545

4646
## Create a labeling project

articles/machine-learning/how-to-track-experiments.md

Lines changed: 20 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.service: machine-learning
1010
ms.subservice: core
1111
ms.workload: data-services
1212
ms.topic: conceptual
13-
ms.date: 12/05/2019
13+
ms.date: 03/12/2020
1414

1515
ms.custom: seodec18
1616
---
@@ -54,76 +54,27 @@ Before adding logging and submitting an experiment, you must set up the workspac
5454

5555
1. Load the workspace. To learn more about setting the workspace configuration, see [workspace configuration file](how-to-configure-environment.md#workspace).
5656

57-
```python
58-
from azureml.core import Experiment, Run, Workspace
59-
import azureml.core
60-
61-
ws = Workspace.from_config()
62-
```
63-
57+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-within-notebook/train-within-notebook.ipynb?name=load_ws)]
58+
59+
6460
## Option 1: Use start_logging
6561

6662
**start_logging** creates an interactive run for use in scenarios such as notebooks. Any metrics that are logged during the session are added to the run record in the experiment.
6763

6864
The following example trains a simple sklearn Ridge model locally in a local Jupyter notebook. To learn more about submitting experiments to different environments, see [Set up compute targets for model training with Azure Machine Learning](https://docs.microsoft.com/azure/machine-learning/how-to-set-up-training-targets).
6965

70-
1. Create a training script in a local Jupyter notebook.
66+
### Load the data
7167

72-
```python
73-
# load diabetes dataset, a well-known small dataset that comes with scikit-learn
74-
from sklearn.datasets import load_diabetes
75-
from sklearn.linear_model import Ridge
76-
from sklearn.metrics import mean_squared_error
77-
from sklearn.model_selection import train_test_split
78-
from sklearn.externals import joblib
79-
80-
X, y = load_diabetes(return_X_y = True)
81-
columns = ['age', 'gender', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
82-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
83-
data = {
84-
"train":{"X": X_train, "y": y_train},
85-
"test":{"X": X_test, "y": y_test}
86-
}
87-
reg = Ridge(alpha = 0.03)
88-
reg.fit(data['train']['X'], data['train']['y'])
89-
preds = reg.predict(data['test']['X'])
90-
print('Mean Squared Error is', mean_squared_error(preds, data['test']['y']))
91-
joblib.dump(value = reg, filename = 'model.pkl');
92-
```
68+
This example uses the diabetes dataset, a well-known small dataset that comes with scikit-learn. This cell loads the dataset and splits it into random training and testing sets.
9369

94-
2. Add experiment tracking using the Azure Machine Learning SDK, and upload a persisted model into the experiment run record. The following code adds tags, logs, and uploads a model file to the experiment run.
70+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-within-notebook/train-within-notebook.ipynb?name=load_data)]
9571

96-
```python
97-
# Get an experiment object from Azure Machine Learning
98-
experiment = Experiment(workspace=ws, name="train-within-notebook")
99-
100-
# Create a run object in the experiment
101-
run = experiment.start_logging()
102-
# Log the algorithm parameter alpha to the run
103-
run.log('alpha', 0.03)
104-
105-
# Create, fit, and test the scikit-learn Ridge regression model
106-
regression_model = Ridge(alpha=0.03)
107-
regression_model.fit(data['train']['X'], data['train']['y'])
108-
preds = regression_model.predict(data['test']['X'])
109-
110-
# Output the Mean Squared Error to the notebook and to the run
111-
print('Mean Squared Error is', mean_squared_error(data['test']['y'], preds))
112-
run.log('mse', mean_squared_error(data['test']['y'], preds))
113-
114-
# Save the model to the outputs directory for capture
115-
model_file_name = 'outputs/model.pkl'
116-
117-
joblib.dump(value = regression_model, filename = model_file_name)
118-
119-
# upload the model file explicitly into artifacts
120-
run.upload_file(name = model_file_name, path_or_stream = model_file_name)
121-
122-
# Complete the run
123-
run.complete()
124-
```
72+
### Add tracking
73+
Add experiment tracking using the Azure Machine Learning SDK, and upload a persisted model into the experiment run record. The following code adds tags, logs, and uploads a model file to the experiment run.
12574

126-
The script ends with ```run.complete()```, which marks the run as completed. This function is typically used in interactive notebook scenarios.
75+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-within-notebook/train-within-notebook.ipynb?name=create_experiment)]
76+
77+
The script ends with ```run.complete()```, which marks the run as completed. This function is typically used in interactive notebook scenarios.
12778

12879
## Option 2: Use ScriptRunConfig
12980

@@ -133,94 +84,24 @@ This example expands on the basic sklearn Ridge model from above. It does a simp
13384

13485
1. Create a training script `train.py`.
13586

136-
```python
137-
# train.py
138-
139-
import os
140-
from sklearn.datasets import load_diabetes
141-
from sklearn.linear_model import Ridge
142-
from sklearn.metrics import mean_squared_error
143-
from sklearn.model_selection import train_test_split
144-
from azureml.core.run import Run
145-
from sklearn.externals import joblib
146-
147-
import numpy as np
148-
149-
#os.makedirs('./outputs', exist_ok = True)
150-
151-
X, y = load_diabetes(return_X_y = True)
152-
153-
run = Run.get_context()
87+
[!code-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-on-local/train.py)]
15488

155-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
156-
data = {"train": {"X": X_train, "y": y_train},
157-
"test": {"X": X_test, "y": y_test}}
158-
159-
# list of numbers from 0.0 to 1.0 with a 0.05 interval
160-
alphas = mylib.get_alphas()
161-
162-
for alpha in alphas:
163-
# Use Ridge algorithm to create a regression model
164-
reg = Ridge(alpha = alpha)
165-
reg.fit(data["train"]["X"], data["train"]["y"])
166-
167-
preds = reg.predict(data["test"]["X"])
168-
mse = mean_squared_error(preds, data["test"]["y"])
169-
# log the alpha and mse values
170-
run.log('alpha', alpha)
171-
run.log('mse', mse)
172-
173-
model_file_name = 'ridge_{0:.2f}.pkl'.format(alpha)
174-
# save model in the outputs folder so it automatically get uploaded
175-
with open(model_file_name, "wb") as file:
176-
joblib.dump(value = reg, filename = model_file_name)
177-
178-
# upload the model file explicitly into artifacts
179-
run.upload_file(name = model_file_name, path_or_stream = model_file_name)
89+
2. The `train.py` script references `mylib.py` which allows you to get the list of alpha values to use in the ridge model.
18090

181-
# register the model
182-
#run.register_model(file_name = model_file_name)
91+
[!code-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-on-local/mylib.py)]
18392

184-
print('alpha is {0:.2f}, and mse is {1:0.2f}'.format(alpha, mse))
185-
186-
```
93+
3. Configure a user-managed local environment.
18794

188-
2. The `train.py` script references `mylib.py` which allows you to get the list of alpha values to use in the ridge model.
95+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-on-local/train-on-local.ipynb?name=user_managed_env)]
18996

190-
```python
191-
# mylib.py
192-
193-
import numpy as np
19497

195-
def get_alphas():
196-
# list of numbers from 0.0 to 1.0 with a 0.05 interval
197-
return np.arange(0.0, 1.0, 0.05)
198-
```
98+
4. Submit the ```train.py``` script to run in the user-managed environment. This whole script folder is submitted for training, including the ```mylib.py``` file.
19999

200-
3. Configure a user-managed local environment.
100+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-on-local/train-on-local.ipynb?name=src)]
101+
[!notebook-python[] (~/MachineLearningNotebooks/how-to-use-azureml/training/train-on-local/train-on-local.ipynb?name=run)]
201102

202-
```python
203-
from azureml.core.environment import Environment
204-
205-
# Editing a run configuration property on-fly.
206-
user_managed_env = Environment("user-managed-env")
207-
208-
user_managed_env.python.user_managed_dependencies = True
209-
210-
# You can choose a specific Python environment by pointing to a Python path
211-
#user_managed_env.python.interpreter_path = '/home/johndoe/miniconda3/envs/myenv/bin/python'
212-
```
213103

214-
4. Submit the ```train.py``` script to run in the user-managed environment. This whole script folder is submitted for training, including the ```mylib.py``` file.
215104

216-
```python
217-
from azureml.core import ScriptRunConfig
218-
219-
exp = Experiment(workspace=ws, name="train-on-local")
220-
src = ScriptRunConfig(source_directory='./', script='train.py')
221-
src.run_config.environment = user_managed_env
222-
run = exp.submit(src)
223-
```
224105

225106
## Manage a run
226107

0 commit comments

Comments
 (0)