mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 01:27:06 -05:00
Compare commits
1 Commits
update-spa
...
release_up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c30d9a5f56 |
@@ -103,7 +103,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -46,9 +46,10 @@
|
|||||||
"Please see the [configuration notebook](../../configuration.ipynb) for information about creating one, if required.\n",
|
"Please see the [configuration notebook](../../configuration.ipynb) for information about creating one, if required.\n",
|
||||||
"This notebook also requires the following packages:\n",
|
"This notebook also requires the following packages:\n",
|
||||||
"* `azureml-contrib-fairness`\n",
|
"* `azureml-contrib-fairness`\n",
|
||||||
"* `fairlearn==0.4.6` (v0.5.0 will work with minor modifications)\n",
|
"* `fairlearn>=0.6.2` (pre-v0.5.0 will work with minor modifications)\n",
|
||||||
"* `joblib`\n",
|
"* `joblib`\n",
|
||||||
"* `liac-arff`\n",
|
"* `liac-arff`\n",
|
||||||
|
"* `raiwidgets==0.4.0`\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Fairlearn relies on features introduced in v0.22.1 of `scikit-learn`. If you have an older version already installed, please uncomment and run the following cell:"
|
"Fairlearn relies on features introduced in v0.22.1 of `scikit-learn`. If you have an older version already installed, please uncomment and run the following cell:"
|
||||||
]
|
]
|
||||||
@@ -85,7 +86,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from fairlearn.reductions import GridSearch, DemographicParity, ErrorRate\n",
|
"from fairlearn.reductions import GridSearch, DemographicParity, ErrorRate\n",
|
||||||
"from fairlearn.widget import FairlearnDashboard\n",
|
"from raiwidgets import FairnessDashboard\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from sklearn.compose import ColumnTransformer\n",
|
"from sklearn.compose import ColumnTransformer\n",
|
||||||
"from sklearn.impute import SimpleImputer\n",
|
"from sklearn.impute import SimpleImputer\n",
|
||||||
@@ -256,9 +257,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"FairlearnDashboard(sensitive_features=A_test, sensitive_feature_names=['Sex', 'Race'],\n",
|
"FairnessDashboard(sensitive_features=A_test,\n",
|
||||||
" y_true=y_test,\n",
|
" y_true=y_test,\n",
|
||||||
" y_pred={\"unmitigated\": unmitigated_predictor.predict(X_test)})"
|
" y_pred={\"unmitigated\": unmitigated_predictor.predict(X_test)})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -311,8 +312,8 @@
|
|||||||
"sweep.fit(X_train, y_train,\n",
|
"sweep.fit(X_train, y_train,\n",
|
||||||
" sensitive_features=A_train.sex)\n",
|
" sensitive_features=A_train.sex)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# For Fairlearn v0.5.0, need sweep.predictors_\n",
|
"# For Fairlearn pre-v0.5.0, need sweep._predictors\n",
|
||||||
"predictors = sweep._predictors"
|
"predictors = sweep.predictors_"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -329,16 +330,14 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"errors, disparities = [], []\n",
|
"errors, disparities = [], []\n",
|
||||||
"for m in predictors:\n",
|
"for predictor in predictors:\n",
|
||||||
" classifier = lambda X: m.predict(X)\n",
|
|
||||||
" \n",
|
|
||||||
" error = ErrorRate()\n",
|
" error = ErrorRate()\n",
|
||||||
" error.load_data(X_train, pd.Series(y_train), sensitive_features=A_train.sex)\n",
|
" error.load_data(X_train, pd.Series(y_train), sensitive_features=A_train.sex)\n",
|
||||||
" disparity = DemographicParity()\n",
|
" disparity = DemographicParity()\n",
|
||||||
" disparity.load_data(X_train, pd.Series(y_train), sensitive_features=A_train.sex)\n",
|
" disparity.load_data(X_train, pd.Series(y_train), sensitive_features=A_train.sex)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" errors.append(error.gamma(classifier)[0])\n",
|
" errors.append(error.gamma(predictor.predict)[0])\n",
|
||||||
" disparities.append(disparity.gamma(classifier).max())\n",
|
" disparities.append(disparity.gamma(predictor.predict).max())\n",
|
||||||
" \n",
|
" \n",
|
||||||
"all_results = pd.DataFrame( {\"predictor\": predictors, \"error\": errors, \"disparity\": disparities})\n",
|
"all_results = pd.DataFrame( {\"predictor\": predictors, \"error\": errors, \"disparity\": disparities})\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -387,10 +386,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"FairlearnDashboard(sensitive_features=A_test, \n",
|
"FairnessDashboard(sensitive_features=A_test, \n",
|
||||||
" sensitive_feature_names=['Sex', 'Race'],\n",
|
" y_true=y_test.tolist(),\n",
|
||||||
" y_true=y_test.tolist(),\n",
|
" y_pred=predictions_dominant)"
|
||||||
" y_pred=predictions_dominant)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -409,7 +407,7 @@
|
|||||||
"<a id=\"AzureUpload\"></a>\n",
|
"<a id=\"AzureUpload\"></a>\n",
|
||||||
"## Uploading a Fairness Dashboard to Azure\n",
|
"## Uploading a Fairness Dashboard to Azure\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Uploading a fairness dashboard to Azure is a two stage process. The `FairlearnDashboard` invoked in the previous section relies on the underlying Python kernel to compute metrics on demand. This is obviously not available when the fairness dashboard is rendered in AzureML Studio. By default, the dashboard in Azure Machine Learning Studio also requires the models to be registered. The required stages are therefore:\n",
|
"Uploading a fairness dashboard to Azure is a two stage process. The `FairnessDashboard` invoked in the previous section relies on the underlying Python kernel to compute metrics on demand. This is obviously not available when the fairness dashboard is rendered in AzureML Studio. By default, the dashboard in Azure Machine Learning Studio also requires the models to be registered. The required stages are therefore:\n",
|
||||||
"1. Register the dominant models\n",
|
"1. Register the dominant models\n",
|
||||||
"1. Precompute all the required metrics\n",
|
"1. Precompute all the required metrics\n",
|
||||||
"1. Upload to Azure\n",
|
"1. Upload to Azure\n",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ dependencies:
|
|||||||
- pip:
|
- pip:
|
||||||
- azureml-sdk
|
- azureml-sdk
|
||||||
- azureml-contrib-fairness
|
- azureml-contrib-fairness
|
||||||
- fairlearn==0.4.6
|
- fairlearn>=0.6.2
|
||||||
- joblib
|
- joblib
|
||||||
- liac-arff
|
- liac-arff
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
"1. [Training Models](#TrainingModels)\n",
|
"1. [Training Models](#TrainingModels)\n",
|
||||||
"1. [Logging in to AzureML](#LoginAzureML)\n",
|
"1. [Logging in to AzureML](#LoginAzureML)\n",
|
||||||
"1. [Registering the Models](#RegisterModels)\n",
|
"1. [Registering the Models](#RegisterModels)\n",
|
||||||
"1. [Using the Fairlearn Dashboard](#LocalDashboard)\n",
|
"1. [Using the Fairness Dashboard](#LocalDashboard)\n",
|
||||||
"1. [Uploading a Fairness Dashboard to Azure](#AzureUpload)\n",
|
"1. [Uploading a Fairness Dashboard to Azure](#AzureUpload)\n",
|
||||||
" 1. Computing Fairness Metrics\n",
|
" 1. Computing Fairness Metrics\n",
|
||||||
" 1. Uploading to Azure\n",
|
" 1. Uploading to Azure\n",
|
||||||
@@ -48,9 +48,10 @@
|
|||||||
"Please see the [configuration notebook](../../configuration.ipynb) for information about creating one, if required.\n",
|
"Please see the [configuration notebook](../../configuration.ipynb) for information about creating one, if required.\n",
|
||||||
"This notebook also requires the following packages:\n",
|
"This notebook also requires the following packages:\n",
|
||||||
"* `azureml-contrib-fairness`\n",
|
"* `azureml-contrib-fairness`\n",
|
||||||
"* `fairlearn==0.4.6` (should also work with v0.5.0)\n",
|
"* `fairlearn>=0.6.2` (also works for pre-v0.5.0 with slight modifications)\n",
|
||||||
"* `joblib`\n",
|
"* `joblib`\n",
|
||||||
"* `liac-arff`\n",
|
"* `liac-arff`\n",
|
||||||
|
"* `raiwidgets==0.4.0`\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Fairlearn relies on features introduced in v0.22.1 of `scikit-learn`. If you have an older version already installed, please uncomment and run the following cell:"
|
"Fairlearn relies on features introduced in v0.22.1 of `scikit-learn`. If you have an older version already installed, please uncomment and run the following cell:"
|
||||||
]
|
]
|
||||||
@@ -388,12 +389,11 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from fairlearn.widget import FairlearnDashboard\n",
|
"from raiwidgets import FairnessDashboard\n",
|
||||||
"\n",
|
"\n",
|
||||||
"FairlearnDashboard(sensitive_features=A_test, \n",
|
"FairnessDashboard(sensitive_features=A_test, \n",
|
||||||
" sensitive_feature_names=['Sex', 'Race'],\n",
|
" y_true=y_test.tolist(),\n",
|
||||||
" y_true=y_test.tolist(),\n",
|
" y_pred=ys_pred)"
|
||||||
" y_pred=ys_pred)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -403,7 +403,7 @@
|
|||||||
"<a id=\"AzureUpload\"></a>\n",
|
"<a id=\"AzureUpload\"></a>\n",
|
||||||
"## Uploading a Fairness Dashboard to Azure\n",
|
"## Uploading a Fairness Dashboard to Azure\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Uploading a fairness dashboard to Azure is a two stage process. The `FairlearnDashboard` invoked in the previous section relies on the underlying Python kernel to compute metrics on demand. This is obviously not available when the fairness dashboard is rendered in AzureML Studio. The required stages are therefore:\n",
|
"Uploading a fairness dashboard to Azure is a two stage process. The `FairnessDashboard` invoked in the previous section relies on the underlying Python kernel to compute metrics on demand. This is obviously not available when the fairness dashboard is rendered in AzureML Studio. The required stages are therefore:\n",
|
||||||
"1. Precompute all the required metrics\n",
|
"1. Precompute all the required metrics\n",
|
||||||
"1. Upload to Azure\n",
|
"1. Upload to Azure\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ dependencies:
|
|||||||
- pip:
|
- pip:
|
||||||
- azureml-sdk
|
- azureml-sdk
|
||||||
- azureml-contrib-fairness
|
- azureml-contrib-fairness
|
||||||
- fairlearn==0.4.6
|
- fairlearn>=0.6.2
|
||||||
- joblib
|
- joblib
|
||||||
- liac-arff
|
- liac-arff
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.29.0
|
- azureml-widgets~=1.30.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_win32_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.30.0/validated_win32_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.29.0
|
- azureml-widgets~=1.30.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_linux_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.30.0/validated_linux_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ dependencies:
|
|||||||
|
|
||||||
- pip:
|
- pip:
|
||||||
# Required packages for AzureML execution, history, and data preparation.
|
# Required packages for AzureML execution, history, and data preparation.
|
||||||
- azureml-widgets~=1.29.0
|
- azureml-widgets~=1.30.0
|
||||||
- pytorch-transformers==1.0.0
|
- pytorch-transformers==1.0.0
|
||||||
- spacy==2.1.8
|
- spacy==2.1.8
|
||||||
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
- https://aka.ms/automl-resources/packages/en_core_web_sm-2.1.0.tar.gz
|
||||||
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.29.0/validated_darwin_requirements.txt [--no-deps]
|
- -r https://automlresources-prod.azureedge.net/validated-requirements/1.30.0/validated_darwin_requirements.txt [--no-deps]
|
||||||
|
|||||||
@@ -105,7 +105,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -93,7 +93,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -81,7 +81,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -54,17 +54,17 @@ try:
|
|||||||
end_time_last_slice = ds.data_changed_time.replace(tzinfo=None)
|
end_time_last_slice = ds.data_changed_time.replace(tzinfo=None)
|
||||||
print("Dataset {0} last updated on {1}".format(args.ds_name,
|
print("Dataset {0} last updated on {1}".format(args.ds_name,
|
||||||
end_time_last_slice))
|
end_time_last_slice))
|
||||||
except Exception:
|
except Exception as e:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
print("Dataset with name {0} not found, registering new dataset.".format(args.ds_name))
|
print("Dataset with name {0} not found, registering new dataset.".format(args.ds_name))
|
||||||
register_dataset = True
|
register_dataset = True
|
||||||
end_time_last_slice = datetime.today() - relativedelta(weeks=4)
|
end_time_last_slice = datetime.today() - relativedelta(weeks=2)
|
||||||
|
|
||||||
end_time = datetime.utcnow()
|
end_time = datetime.utcnow()
|
||||||
train_df = get_noaa_data(end_time_last_slice, end_time)
|
train_df = get_noaa_data(end_time_last_slice, end_time)
|
||||||
|
|
||||||
if train_df.size > 0:
|
if train_df.size > 0:
|
||||||
print("Received {0} rows of new data after {1}.".format(
|
print("Received {0} rows of new data after {0}.".format(
|
||||||
train_df.shape[0], end_time_last_slice))
|
train_df.shape[0], end_time_last_slice))
|
||||||
folder_name = "{}/{:04d}/{:02d}/{:02d}/{:02d}/{:02d}/{:02d}".format(args.ds_name, end_time.year,
|
folder_name = "{}/{:04d}/{:02d}/{:02d}/{:02d}/{:02d}/{:02d}".format(args.ds_name, end_time.year,
|
||||||
end_time.month, end_time.day,
|
end_time.month, end_time.day,
|
||||||
|
|||||||
@@ -0,0 +1,420 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Automated Machine Learning\n",
|
||||||
|
"_**Classification of credit card fraudulent transactions on local managed compute **_\n",
|
||||||
|
"\n",
|
||||||
|
"## Contents\n",
|
||||||
|
"1. [Introduction](#Introduction)\n",
|
||||||
|
"1. [Setup](#Setup)\n",
|
||||||
|
"1. [Train](#Train)\n",
|
||||||
|
"1. [Results](#Results)\n",
|
||||||
|
"1. [Test](#Test)\n",
|
||||||
|
"1. [Acknowledgements](#Acknowledgements)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Introduction\n",
|
||||||
|
"\n",
|
||||||
|
"In this example we use the associated credit card dataset to showcase how you can use AutoML for a simple classification problem. The goal is to predict if a credit card transaction is considered a fraudulent charge.\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook is using local managed compute to train the model.\n",
|
||||||
|
"\n",
|
||||||
|
"If you are using an Azure Machine Learning Compute Instance, you are all set. Otherwise, go through the [configuration](../../../configuration.ipynb) notebook first if you haven't already to establish your connection to the AzureML Workspace. \n",
|
||||||
|
"\n",
|
||||||
|
"In this notebook you will learn how to:\n",
|
||||||
|
"1. Create an experiment using an existing workspace.\n",
|
||||||
|
"2. Configure AutoML using `AutoMLConfig`.\n",
|
||||||
|
"3. Train the model using local managed compute.\n",
|
||||||
|
"4. Explore the results.\n",
|
||||||
|
"5. Test the fitted model."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup\n",
|
||||||
|
"\n",
|
||||||
|
"As part of the setup you have already created an Azure ML `Workspace` object. For Automated ML you will need to create an `Experiment` object, which is a named object in a `Workspace` used to run experiments."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import logging\n",
|
||||||
|
"\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"import azureml.core\n",
|
||||||
|
"from azureml.core.compute_target import LocalTarget\n",
|
||||||
|
"from azureml.core.experiment import Experiment\n",
|
||||||
|
"from azureml.core.workspace import Workspace\n",
|
||||||
|
"from azureml.core.dataset import Dataset\n",
|
||||||
|
"from azureml.train.automl import AutoMLConfig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This sample notebook may use features that are not available in previous versions of the Azure ML SDK."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"\n",
|
||||||
|
"# choose a name for experiment\n",
|
||||||
|
"experiment_name = 'automl-local-managed'\n",
|
||||||
|
"\n",
|
||||||
|
"experiment=Experiment(ws, experiment_name)\n",
|
||||||
|
"\n",
|
||||||
|
"output = {}\n",
|
||||||
|
"output['Subscription ID'] = ws.subscription_id\n",
|
||||||
|
"output['Workspace'] = ws.name\n",
|
||||||
|
"output['Resource Group'] = ws.resource_group\n",
|
||||||
|
"output['Location'] = ws.location\n",
|
||||||
|
"output['Experiment Name'] = experiment.name\n",
|
||||||
|
"pd.set_option('display.max_colwidth', -1)\n",
|
||||||
|
"outputDf = pd.DataFrame(data = output, index = [''])\n",
|
||||||
|
"outputDf.T"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Determine if local docker is configured for Linux images\n",
|
||||||
|
"\n",
|
||||||
|
"Local managed runs will leverage a Linux docker container to submit the run to. Due to this, the docker needs to be configured to use Linux containers."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Check if Docker is installed and Linux containers are enabled\n",
|
||||||
|
"import subprocess\n",
|
||||||
|
"from subprocess import CalledProcessError\n",
|
||||||
|
"try:\n",
|
||||||
|
" assert subprocess.run(\"docker -v\", shell=True).returncode == 0, 'Local Managed runs require docker to be installed.'\n",
|
||||||
|
" out = subprocess.check_output(\"docker system info\", shell=True).decode('ascii')\n",
|
||||||
|
" assert \"OSType: linux\" in out, 'Docker engine needs to be configured to use Linux containers.' \\\n",
|
||||||
|
" 'https://docs.docker.com/docker-for-windows/#switch-between-windows-and-linux-containers'\n",
|
||||||
|
"except CalledProcessError as ex:\n",
|
||||||
|
" raise Exception('Local Managed runs require docker to be installed.') from ex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Load Data\n",
|
||||||
|
"\n",
|
||||||
|
"Load the credit card dataset from a csv file containing both training features and labels. The features are inputs to the model, while the training labels represent the expected output of the model. Next, we'll split the data using random_split and extract the training data for the model."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"data = \"https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/creditcard.csv\"\n",
|
||||||
|
"dataset = Dataset.Tabular.from_delimited_files(data)\n",
|
||||||
|
"training_data, validation_data = dataset.random_split(percentage=0.8, seed=223)\n",
|
||||||
|
"label_column_name = 'Class'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Train\n",
|
||||||
|
"\n",
|
||||||
|
"Instantiate a AutoMLConfig object. This defines the settings and data used to run the experiment.\n",
|
||||||
|
"\n",
|
||||||
|
"|Property|Description|\n",
|
||||||
|
"|-|-|\n",
|
||||||
|
"|**task**|classification or regression|\n",
|
||||||
|
"|**primary_metric**|This is the metric that you want to optimize. Classification supports the following primary metrics: <br><i>accuracy</i><br><i>AUC_weighted</i><br><i>average_precision_score_weighted</i><br><i>norm_macro_recall</i><br><i>precision_score_weighted</i>|\n",
|
||||||
|
"|**enable_early_stopping**|Stop the run if the metric score is not showing improvement.|\n",
|
||||||
|
"|**n_cross_validations**|Number of cross validation splits.|\n",
|
||||||
|
"|**training_data**|Input dataset, containing both features and label column.|\n",
|
||||||
|
"|**label_column_name**|The name of the label column.|\n",
|
||||||
|
"|**enable_local_managed**|Enable the experimental local-managed scenario.|\n",
|
||||||
|
"\n",
|
||||||
|
"**_You can find more information about primary metrics_** [here](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-auto-train#primary-metric)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"automl_settings = {\n",
|
||||||
|
" \"n_cross_validations\": 3,\n",
|
||||||
|
" \"primary_metric\": 'average_precision_score_weighted',\n",
|
||||||
|
" \"enable_early_stopping\": True,\n",
|
||||||
|
" \"experiment_timeout_hours\": 0.3, #for real scenarios we recommend a timeout of at least one hour \n",
|
||||||
|
" \"verbosity\": logging.INFO,\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"automl_config = AutoMLConfig(task = 'classification',\n",
|
||||||
|
" debug_log = 'automl_errors.log',\n",
|
||||||
|
" compute_target = LocalTarget(),\n",
|
||||||
|
" enable_local_managed = True,\n",
|
||||||
|
" training_data = training_data,\n",
|
||||||
|
" label_column_name = label_column_name,\n",
|
||||||
|
" **automl_settings\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Call the `submit` method on the experiment object and pass the run configuration. Depending on the data and the number of iterations this can run for a while. Validation errors and current status will be shown when setting `show_output=True` and the execution will be synchronous."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"parent_run = experiment.submit(automl_config, show_output = True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# If you need to retrieve a run that already started, use the following code\n",
|
||||||
|
"#from azureml.train.automl.run import AutoMLRun\n",
|
||||||
|
"#parent_run = AutoMLRun(experiment = experiment, run_id = '<replace with your run id>')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"parent_run"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Results"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Explain model\n",
|
||||||
|
"\n",
|
||||||
|
"Automated ML models can be explained and visualized using the SDK Explainability library. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Analyze results\n",
|
||||||
|
"\n",
|
||||||
|
"### Retrieve the Best Child Run\n",
|
||||||
|
"\n",
|
||||||
|
"Below we select the best pipeline from our iterations. The `get_best_child` method returns the best run. Overloads on `get_best_child` allow you to retrieve the best run for *any* logged metric."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"best_run = parent_run.get_best_child()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Test the fitted model\n",
|
||||||
|
"\n",
|
||||||
|
"Now that the model is trained, split the data in the same way the data was split for training (The difference here is the data is being split locally) and then run the test data through the trained model to get the predicted values."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"X_test_df = validation_data.drop_columns(columns=[label_column_name])\n",
|
||||||
|
"y_test_df = validation_data.keep_columns(columns=[label_column_name], validate=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Creating ModelProxy for submitting prediction runs to the training environment.\n",
|
||||||
|
"We will create a ModelProxy for the best child run, which will allow us to submit a run that does the prediction in the training environment. Unlike the local client, which can have different versions of some libraries, the training environment will have all the compatible libraries for the model already."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.train.automl.model_proxy import ModelProxy\n",
|
||||||
|
"best_model_proxy = ModelProxy(best_run)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# call the predict functions on the model proxy\n",
|
||||||
|
"y_pred = best_model_proxy.predict(X_test_df).to_pandas_dataframe()\n",
|
||||||
|
"y_pred"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Acknowledgements"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This Credit Card fraud Detection dataset is made available under the Open Database License: http://opendatacommons.org/licenses/odbl/1.0/. Any rights in individual contents of the database are licensed under the Database Contents License: http://opendatacommons.org/licenses/dbcl/1.0/ and is available at: https://www.kaggle.com/mlg-ulb/creditcardfraud\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"The dataset has been collected and analysed during a research collaboration of Worldline and the Machine Learning Group (http://mlg.ulb.ac.be) of ULB (Universit\u00c3\u0192\u00c2\u00a9 Libre de Bruxelles) on big data mining and fraud detection. More details on current and past projects on related topics are available on https://www.researchgate.net/project/Fraud-detection-5 and the page of the DefeatFraud project\n",
|
||||||
|
"Please cite the following works: \n",
|
||||||
|
"\u00c3\u00a2\u00e2\u201a\u00ac\u00c2\u00a2\tAndrea Dal Pozzolo, Olivier Caelen, Reid A. Johnson and Gianluca Bontempi. Calibrating Probability with Undersampling for Unbalanced Classification. In Symposium on Computational Intelligence and Data Mining (CIDM), IEEE, 2015\n",
|
||||||
|
"\u00c3\u00a2\u00e2\u201a\u00ac\u00c2\u00a2\tDal Pozzolo, Andrea; Caelen, Olivier; Le Borgne, Yann-Ael; Waterschoot, Serge; Bontempi, Gianluca. Learned lessons in credit card fraud detection from a practitioner perspective, Expert systems with applications,41,10,4915-4928,2014, Pergamon\n",
|
||||||
|
"\u00c3\u00a2\u00e2\u201a\u00ac\u00c2\u00a2\tDal Pozzolo, Andrea; Boracchi, Giacomo; Caelen, Olivier; Alippi, Cesare; Bontempi, Gianluca. Credit card fraud detection: a realistic modeling and a novel learning strategy, IEEE transactions on neural networks and learning systems,29,8,3784-3797,2018,IEEE\n",
|
||||||
|
"o\tDal Pozzolo, Andrea Adaptive Machine learning for credit card fraud detection ULB MLG PhD thesis (supervised by G. Bontempi)\n",
|
||||||
|
"\u00c3\u00a2\u00e2\u201a\u00ac\u00c2\u00a2\tCarcillo, Fabrizio; Dal Pozzolo, Andrea; Le Borgne, Yann-A\u00c3\u0192\u00c2\u00abl; Caelen, Olivier; Mazzer, Yannis; Bontempi, Gianluca. Scarff: a scalable framework for streaming credit card fraud detection with Spark, Information fusion,41, 182-194,2018,Elsevier\n",
|
||||||
|
"\u00c3\u00a2\u00e2\u201a\u00ac\u00c2\u00a2\tCarcillo, Fabrizio; Le Borgne, Yann-A\u00c3\u0192\u00c2\u00abl; Caelen, Olivier; Bontempi, Gianluca. Streaming active learning strategies for real-life credit card fraud detection: assessment and visualization, International Journal of Data Science and Analytics, 5,4,285-300,2018,Springer International Publishing"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "sekrupa"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"category": "tutorial",
|
||||||
|
"compute": [
|
||||||
|
"AML Compute"
|
||||||
|
],
|
||||||
|
"datasets": [
|
||||||
|
"Creditcard"
|
||||||
|
],
|
||||||
|
"deployment": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"exclude_from_index": false,
|
||||||
|
"file_extension": ".py",
|
||||||
|
"framework": [
|
||||||
|
"None"
|
||||||
|
],
|
||||||
|
"friendly_name": "Classification of credit card fraudulent transactions using Automated ML",
|
||||||
|
"index_order": 5,
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.7"
|
||||||
|
},
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"tags": [
|
||||||
|
"AutomatedML"
|
||||||
|
],
|
||||||
|
"task": "Classification",
|
||||||
|
"version": "3.6.7"
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: auto-ml-classification-credit-card-fraud-local-managed
|
||||||
|
dependencies:
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
@@ -91,7 +91,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -113,7 +113,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -87,7 +87,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -97,7 +97,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -94,7 +94,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -82,7 +82,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -96,7 +96,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -92,7 +92,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -350,32 +350,6 @@
|
|||||||
"displayHTML(\"<a href={} target='_blank'>Azure Portal: {}</a>\".format(local_run.get_portal_url(), local_run.id))"
|
"displayHTML(\"<a href={} target='_blank'>Azure Portal: {}</a>\".format(local_run.get_portal_url(), local_run.id))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"#### Retrieve All Child Runs after the experiment is completed (in portal)\n",
|
|
||||||
"You can also use SDK methods to fetch all the child runs and see individual metrics that we log."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"children = list(local_run.get_children())\n",
|
|
||||||
"metricslist = {}\n",
|
|
||||||
"for run in children:\n",
|
|
||||||
" properties = run.get_properties()\n",
|
|
||||||
" #print(properties)\n",
|
|
||||||
" metrics = {k: v for k, v in run.get_metrics().items() if isinstance(v, float)} \n",
|
|
||||||
" metricslist[int(properties['iteration'])] = metrics\n",
|
|
||||||
"\n",
|
|
||||||
"rundata = pd.DataFrame(metricslist).sort_index(1)\n",
|
|
||||||
"rundata"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
|||||||
@@ -352,32 +352,6 @@
|
|||||||
"displayHTML(\"<a href={} target='_blank'>Azure Portal: {}</a>\".format(local_run.get_portal_url(), local_run.id))"
|
"displayHTML(\"<a href={} target='_blank'>Azure Portal: {}</a>\".format(local_run.get_portal_url(), local_run.id))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"#### Retrieve All Child Runs after the experiment is completed (in portal)\n",
|
|
||||||
"You can also use SDK methods to fetch all the child runs and see individual metrics that we log."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"children = list(local_run.get_children())\n",
|
|
||||||
"metricslist = {}\n",
|
|
||||||
"for run in children:\n",
|
|
||||||
" properties = run.get_properties()\n",
|
|
||||||
" #print(properties)\n",
|
|
||||||
" metrics = {k: v for k, v in run.get_metrics().items() if isinstance(v, float)} \n",
|
|
||||||
" metricslist[int(properties['iteration'])] = metrics\n",
|
|
||||||
"\n",
|
|
||||||
"rundata = pd.DataFrame(metricslist).sort_index(1)\n",
|
|
||||||
"rundata"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
|||||||
@@ -217,7 +217,6 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
|
||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Choose a name for your CPU cluster\n",
|
"# Choose a name for your CPU cluster\n",
|
||||||
@@ -267,7 +266,7 @@
|
|||||||
"available_packages = pkg_resources.working_set\n",
|
"available_packages = pkg_resources.working_set\n",
|
||||||
"sklearn_ver = None\n",
|
"sklearn_ver = None\n",
|
||||||
"pandas_ver = None\n",
|
"pandas_ver = None\n",
|
||||||
"for dist in available_packages:\n",
|
"for dist in list(available_packages):\n",
|
||||||
" if dist.key == 'scikit-learn':\n",
|
" if dist.key == 'scikit-learn':\n",
|
||||||
" sklearn_ver = dist.version\n",
|
" sklearn_ver = dist.version\n",
|
||||||
" elif dist.key == 'pandas':\n",
|
" elif dist.key == 'pandas':\n",
|
||||||
@@ -286,7 +285,6 @@
|
|||||||
"azureml_pip_packages.extend([sklearn_dep, pandas_dep])\n",
|
"azureml_pip_packages.extend([sklearn_dep, pandas_dep])\n",
|
||||||
"run_config.environment.python.conda_dependencies = CondaDependencies.create(pip_packages=azureml_pip_packages)\n",
|
"run_config.environment.python.conda_dependencies = CondaDependencies.create(pip_packages=azureml_pip_packages)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from azureml.core import Run\n",
|
|
||||||
"from azureml.core import ScriptRunConfig\n",
|
"from azureml.core import ScriptRunConfig\n",
|
||||||
"\n",
|
"\n",
|
||||||
"src = ScriptRunConfig(source_directory=project_folder, \n",
|
"src = ScriptRunConfig(source_directory=project_folder, \n",
|
||||||
@@ -416,7 +414,6 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Retrieve x_test for visualization\n",
|
"# Retrieve x_test for visualization\n",
|
||||||
"import joblib\n",
|
|
||||||
"x_test_path = './x_test_boston_housing.pkl'\n",
|
"x_test_path = './x_test_boston_housing.pkl'\n",
|
||||||
"run.download_file('x_test_boston_housing.pkl', output_file_path=x_test_path)"
|
"run.download_file('x_test_boston_housing.pkl', output_file_path=x_test_path)"
|
||||||
]
|
]
|
||||||
@@ -444,7 +441,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from interpret_community.widget import ExplanationDashboard"
|
"from raiwidgets import ExplanationDashboard"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -453,7 +450,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"ExplanationDashboard(global_explanation, original_model, datasetX=x_test)"
|
"ExplanationDashboard(global_explanation, original_model, dataset=x_test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,3 +11,4 @@ dependencies:
|
|||||||
- matplotlib
|
- matplotlib
|
||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -87,7 +87,6 @@
|
|||||||
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
||||||
"from sklearn.svm import SVC\n",
|
"from sklearn.svm import SVC\n",
|
||||||
"import pandas as pd\n",
|
"import pandas as pd\n",
|
||||||
"import numpy as np\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# Explainers:\n",
|
"# Explainers:\n",
|
||||||
"# 1. SHAP Tabular Explainer\n",
|
"# 1. SHAP Tabular Explainer\n",
|
||||||
@@ -533,7 +532,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from interpret_community.widget import ExplanationDashboard"
|
"from raiwidgets import ExplanationDashboard"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -542,7 +541,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"ExplanationDashboard(downloaded_global_explanation, model, datasetX=x_test)"
|
"ExplanationDashboard(downloaded_global_explanation, model, dataset=x_test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ dependencies:
|
|||||||
- ipython
|
- ipython
|
||||||
- matplotlib
|
- matplotlib
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -170,7 +170,6 @@
|
|||||||
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
||||||
"from sklearn.impute import SimpleImputer\n",
|
"from sklearn.impute import SimpleImputer\n",
|
||||||
"from sklearn.pipeline import Pipeline\n",
|
"from sklearn.pipeline import Pipeline\n",
|
||||||
"from sklearn.linear_model import LogisticRegression\n",
|
|
||||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from interpret.ext.blackbox import TabularExplainer\n",
|
"from interpret.ext.blackbox import TabularExplainer\n",
|
||||||
@@ -221,7 +220,6 @@
|
|||||||
" ('classifier', RandomForestClassifier())])\n",
|
" ('classifier', RandomForestClassifier())])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Split data into train and test\n",
|
"# Split data into train and test\n",
|
||||||
"from sklearn.model_selection import train_test_split\n",
|
|
||||||
"x_train, x_test, y_train, y_test = train_test_split(attritionXData,\n",
|
"x_train, x_test, y_train, y_test = train_test_split(attritionXData,\n",
|
||||||
" target,\n",
|
" target,\n",
|
||||||
" test_size=0.2,\n",
|
" test_size=0.2,\n",
|
||||||
@@ -296,7 +294,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from interpret_community.widget import ExplanationDashboard"
|
"from raiwidgets import ExplanationDashboard"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -305,7 +303,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"ExplanationDashboard(global_explanation, clf, datasetX=x_test)"
|
"ExplanationDashboard(global_explanation, clf, dataset=x_test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -383,10 +381,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import Model\n",
|
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"from azureml.exceptions import WebserviceException\n",
|
"from azureml.exceptions import WebserviceException\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ dependencies:
|
|||||||
- ipython
|
- ipython
|
||||||
- matplotlib
|
- matplotlib
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -218,7 +218,6 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
|
||||||
"from azureml.core.compute_target import ComputeTargetException\n",
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Choose a name for your CPU cluster\n",
|
"# Choose a name for your CPU cluster\n",
|
||||||
@@ -380,7 +379,6 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Retrieve x_test for visualization\n",
|
"# Retrieve x_test for visualization\n",
|
||||||
"import joblib\n",
|
|
||||||
"x_test_path = './x_test.pkl'\n",
|
"x_test_path = './x_test.pkl'\n",
|
||||||
"run.download_file('x_test_ibm.pkl', output_file_path=x_test_path)\n",
|
"run.download_file('x_test_ibm.pkl', output_file_path=x_test_path)\n",
|
||||||
"x_test = joblib.load(x_test_path)"
|
"x_test = joblib.load(x_test_path)"
|
||||||
@@ -400,7 +398,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from interpret_community.widget import ExplanationDashboard"
|
"from raiwidgets import ExplanationDashboard"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -409,7 +407,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"ExplanationDashboard(global_explanation, original_svm_model, datasetX=x_test)"
|
"ExplanationDashboard(global_explanation, original_svm_model, dataset=x_test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -426,8 +424,6 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.conda_dependencies import CondaDependencies \n",
|
|
||||||
"\n",
|
|
||||||
"# WARNING: to install this, g++ needs to be available on the Docker image and is not by default (look at the next cell)\n",
|
"# WARNING: to install this, g++ needs to be available on the Docker image and is not by default (look at the next cell)\n",
|
||||||
"azureml_pip_packages = [\n",
|
"azureml_pip_packages = [\n",
|
||||||
" 'azureml-defaults', 'azureml-core', 'azureml-telemetry',\n",
|
" 'azureml-defaults', 'azureml-core', 'azureml-telemetry',\n",
|
||||||
@@ -437,7 +433,6 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# Note: this is to pin the scikit-learn and pandas versions to be same as notebook.\n",
|
"# Note: this is to pin the scikit-learn and pandas versions to be same as notebook.\n",
|
||||||
"# In production scenario user would choose their dependencies\n",
|
"# In production scenario user would choose their dependencies\n",
|
||||||
"import pkg_resources\n",
|
|
||||||
"available_packages = pkg_resources.working_set\n",
|
"available_packages = pkg_resources.working_set\n",
|
||||||
"sklearn_ver = None\n",
|
"sklearn_ver = None\n",
|
||||||
"pandas_ver = None\n",
|
"pandas_ver = None\n",
|
||||||
@@ -483,10 +478,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"from azureml.core.model import Model\n",
|
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"from azureml.exceptions import WebserviceException\n",
|
"from azureml.exceptions import WebserviceException\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -12,3 +12,4 @@ dependencies:
|
|||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- azureml-core
|
- azureml-core
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
|
- raiwidgets==0.4.0
|
||||||
|
|||||||
@@ -681,7 +681,6 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import logging\n",
|
|
||||||
"from azureml.train.automl import AutoMLConfig\n",
|
"from azureml.train.automl import AutoMLConfig\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Change iterations to a reasonable number (50) to get better accuracy\n",
|
"# Change iterations to a reasonable number (50) to get better accuracy\n",
|
||||||
@@ -784,8 +783,8 @@
|
|||||||
" path = download_path + '/azureml/' + output_folder + '/' + output_name\n",
|
" path = download_path + '/azureml/' + output_folder + '/' + output_name\n",
|
||||||
" return path\n",
|
" return path\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def fetch_df(step, output_name):\n",
|
"def fetch_df(current_step, output_name):\n",
|
||||||
" output_data = step.get_output_data(output_name) \n",
|
" output_data = current_step.get_output_data(output_name) \n",
|
||||||
" download_path = './outputs/' + output_name\n",
|
" download_path = './outputs/' + output_name\n",
|
||||||
" output_data.download(download_path, overwrite=True)\n",
|
" output_data.download(download_path, overwrite=True)\n",
|
||||||
" df_path = get_download_path(download_path, output_name) + '/processed.parquet'\n",
|
" df_path = get_download_path(download_path, output_name) + '/processed.parquet'\n",
|
||||||
@@ -941,32 +940,6 @@
|
|||||||
"#RunDetails(automl_run).show()"
|
"#RunDetails(automl_run).show()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"#### Retrieve all Child runs\n",
|
|
||||||
"\n",
|
|
||||||
"We use SDK methods to fetch all the child runs and see individual metrics that we log."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"children = list(automl_run.get_children())\n",
|
|
||||||
"metricslist = {}\n",
|
|
||||||
"for run in children:\n",
|
|
||||||
" properties = run.get_properties()\n",
|
|
||||||
" metrics = {k: v for k, v in run.get_metrics().items() if isinstance(v, float)}\n",
|
|
||||||
" metricslist[int(properties['iteration'])] = metrics\n",
|
|
||||||
"\n",
|
|
||||||
"rundata = pd.DataFrame(metricslist).sort_index(1)\n",
|
|
||||||
"rundata"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Install required packages\n",
|
"## Install required packages\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This notebook works with Fairlearn v0.4.6, and not later versions. If needed, please uncomment and run the following cell:"
|
"This notebook works with Fairlearn v0.6.1, but not with versions pre-v0.5.0. If needed, please uncomment and run the following cell:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# %pip install --upgrade fairlearn==0.4.6"
|
"# %pip install --upgrade fairlearn>=0.6.2"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -70,21 +70,18 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from fairlearn.reductions import GridSearch\n",
|
"from fairlearn.reductions import GridSearch\n",
|
||||||
"from fairlearn.reductions import DemographicParity, ErrorRate\n",
|
"from fairlearn.reductions import DemographicParity\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from sklearn.compose import ColumnTransformer, make_column_selector\n",
|
"from sklearn.compose import ColumnTransformer, make_column_selector\n",
|
||||||
"from sklearn.preprocessing import LabelEncoder,StandardScaler\n",
|
"from sklearn.preprocessing import LabelEncoder, StandardScaler, OneHotEncoder\n",
|
||||||
"from sklearn.linear_model import LogisticRegression\n",
|
"from sklearn.linear_model import LogisticRegression\n",
|
||||||
"from sklearn.pipeline import Pipeline\n",
|
"from sklearn.pipeline import Pipeline\n",
|
||||||
"from sklearn.impute import SimpleImputer\n",
|
"from sklearn.impute import SimpleImputer\n",
|
||||||
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
|
|
||||||
"from sklearn.svm import SVC\n",
|
|
||||||
"from sklearn.metrics import accuracy_score\n",
|
"from sklearn.metrics import accuracy_score\n",
|
||||||
"\n",
|
"\n",
|
||||||
"import pandas as pd\n",
|
"import pandas as pd\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# SHAP Tabular Explainer\n",
|
"# SHAP Tabular Explainer\n",
|
||||||
"from interpret.ext.blackbox import KernelExplainer\n",
|
|
||||||
"from interpret.ext.blackbox import MimicExplainer\n",
|
"from interpret.ext.blackbox import MimicExplainer\n",
|
||||||
"from interpret.ext.glassbox import LGBMExplainableModel"
|
"from interpret.ext.glassbox import LGBMExplainableModel"
|
||||||
]
|
]
|
||||||
@@ -340,13 +337,13 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from fairlearn.widget import FairlearnDashboard\n",
|
"from raiwidgets import FairnessDashboard\n",
|
||||||
"\n",
|
"\n",
|
||||||
"y_pred = model.predict(X_test)\n",
|
"y_pred = model.predict(X_test)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"FairlearnDashboard(sensitive_features=sensitive_features_test,\n",
|
"FairnessDashboard(sensitive_features=sensitive_features_test,\n",
|
||||||
" y_true=y_test,\n",
|
" y_true=y_test,\n",
|
||||||
" y_pred=y_pred)"
|
" y_pred=y_pred)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -402,7 +399,7 @@
|
|||||||
"sweep.fit(X_train_prep, y_train,\n",
|
"sweep.fit(X_train_prep, y_train,\n",
|
||||||
" sensitive_features=sensitive_features_train.sex)\n",
|
" sensitive_features=sensitive_features_train.sex)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"predictors = sweep._predictors"
|
"predictors = sweep.predictors_"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -468,7 +465,7 @@
|
|||||||
"for name, predictor in dominant_models_dict.items():\n",
|
"for name, predictor in dominant_models_dict.items():\n",
|
||||||
" dominant_all[name] = predictor.predict(X_test_prep)\n",
|
" dominant_all[name] = predictor.predict(X_test_prep)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"FairlearnDashboard(sensitive_features=sensitive_features_test, \n",
|
"FairnessDashboard(sensitive_features=sensitive_features_test, \n",
|
||||||
" y_true=y_test,\n",
|
" y_true=y_test,\n",
|
||||||
" y_pred=dominant_all)"
|
" y_pred=dominant_all)"
|
||||||
]
|
]
|
||||||
@@ -563,7 +560,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import joblib\n",
|
"import joblib\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"from azureml.core import Model, Experiment, Run\n",
|
"from azureml.core import Model, Experiment\n",
|
||||||
"\n",
|
"\n",
|
||||||
"os.makedirs('models', exist_ok=True)\n",
|
"os.makedirs('models', exist_ok=True)\n",
|
||||||
"def register_model(name, model):\n",
|
"def register_model(name, model):\n",
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ dependencies:
|
|||||||
- azureml-sdk
|
- azureml-sdk
|
||||||
- azureml-interpret
|
- azureml-interpret
|
||||||
- azureml-contrib-fairness
|
- azureml-contrib-fairness
|
||||||
- fairlearn==0.4.6
|
- fairlearn>=0.6.2
|
||||||
- matplotlib
|
- matplotlib
|
||||||
- azureml-dataset-runtime
|
- azureml-dataset-runtime
|
||||||
- ipywidgets
|
- ipywidgets
|
||||||
- raiwidgets
|
- raiwidgets==0.4.0
|
||||||
- liac-arff
|
- liac-arff
|
||||||
|
|||||||
@@ -100,7 +100,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# Check core SDK version number\n",
|
"# Check core SDK version number\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using SDK version 1.29.0, you are currently running version\", azureml.core.VERSION)"
|
"print(\"This notebook was created using SDK version 1.30.0, you are currently running version\", azureml.core.VERSION)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
1
index.md
1
index.md
@@ -25,6 +25,7 @@ Machine Learning notebook samples and encourage efficient retrieval of topics an
|
|||||||
| [Forecasting away from training data](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-forecast-function/auto-ml-forecasting-function.ipynb) | Forecasting | None | Remote | None | Azure ML AutoML | Forecasting, Confidence Intervals |
|
| [Forecasting away from training data](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/forecasting-forecast-function/auto-ml-forecasting-function.ipynb) | Forecasting | None | Remote | None | Azure ML AutoML | Forecasting, Confidence Intervals |
|
||||||
| [Automated ML run with basic edition features.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-bank-marketing-all-features/auto-ml-classification-bank-marketing-all-features.ipynb) | Classification | Bankmarketing | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
| [Automated ML run with basic edition features.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-bank-marketing-all-features/auto-ml-classification-bank-marketing-all-features.ipynb) | Classification | Bankmarketing | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
||||||
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-credit-card-fraud/auto-ml-classification-credit-card-fraud.ipynb) | Classification | Creditcard | AML Compute | None | None | remote_run, AutomatedML |
|
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/classification-credit-card-fraud/auto-ml-classification-credit-card-fraud.ipynb) | Classification | Creditcard | AML Compute | None | None | remote_run, AutomatedML |
|
||||||
|
| [Classification of credit card fraudulent transactions using Automated ML](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/experimental/classification-credit-card-fraud-local-managed/auto-ml-classification-credit-card-fraud-local-managed.ipynb) | Classification | Creditcard | AML Compute | None | None | AutomatedML |
|
||||||
| [Automated ML run with featurization and model explainability.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression-explanation-featurization/auto-ml-regression-explanation-featurization.ipynb) | Regression | MachineData | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
| [Automated ML run with featurization and model explainability.](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/automated-machine-learning/regression-explanation-featurization/auto-ml-regression-explanation-featurization.ipynb) | Regression | MachineData | AML | ACI | None | featurization, explainability, remote_run, AutomatedML |
|
||||||
| :star:[Azure Machine Learning Pipeline with DataTranferStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-data-transfer.ipynb) | Demonstrates the use of DataTranferStep | Custom | ADF | None | Azure ML | None |
|
| :star:[Azure Machine Learning Pipeline with DataTranferStep](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-data-transfer.ipynb) | Demonstrates the use of DataTranferStep | Custom | ADF | None | Azure ML | None |
|
||||||
| [Getting Started with Azure Machine Learning Pipelines](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-getting-started.ipynb) | Getting Started notebook for ANML Pipelines | Custom | AML Compute | None | Azure ML | None |
|
| [Getting Started with Azure Machine Learning Pipelines](https://github.com/Azure/MachineLearningNotebooks/blob/master//how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-getting-started.ipynb) | Getting Started notebook for ANML Pipelines | Custom | AML Compute | None | Azure ML | None |
|
||||||
|
|||||||
@@ -102,7 +102,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import azureml.core\n",
|
"import azureml.core\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"This notebook was created using version 1.29.0 of the Azure ML SDK\")\n",
|
"print(\"This notebook was created using version 1.30.0 of the Azure ML SDK\")\n",
|
||||||
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
"print(\"You are currently using version\", azureml.core.VERSION, \"of the Azure ML SDK\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -23,59 +23,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"You'll learn how to:\n",
|
"You'll learn how to:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"> * Download a dataset and look at the data\n",
|
"* Download a dataset and look at the data\n",
|
||||||
"> * Train an image classification model and log metrics\n",
|
"* Train an image classification model and log metrics using MLflow\n",
|
||||||
"> * Deploy the model"
|
"* Deploy the model to do real-time inference"
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"## Connect to your workspace and create an experiment\n",
|
|
||||||
"\n",
|
|
||||||
"Import some libraries and create an experiment to track the runs in your workspace. A workspace can have multiple experiments, and all users that have access to the workspace can collaborate on them."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612965916889
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"import numpy as np\n",
|
|
||||||
"import matplotlib.pyplot as plt\n",
|
|
||||||
"\n",
|
|
||||||
"import azureml.core\n",
|
|
||||||
"from azureml.core import Workspace\n",
|
|
||||||
"from azureml.core import Experiment\n",
|
|
||||||
"\n",
|
|
||||||
"# connect to your workspace\n",
|
|
||||||
"ws = Workspace.from_config()\n",
|
|
||||||
"\n",
|
|
||||||
"# create experiment and start logging to a new run in the experiment\n",
|
|
||||||
"experiment_name = \"azure-ml-in10-mins-tutorial\"\n",
|
|
||||||
"exp = Experiment(workspace=ws, name=experiment_name)\n",
|
|
||||||
"run = exp.start_logging(snapshot_directory=None)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -95,46 +45,23 @@
|
|||||||
"* Download the MNIST dataset\n",
|
"* Download the MNIST dataset\n",
|
||||||
"* Display some sample images\n",
|
"* Display some sample images\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Download the MNIST dataset\n",
|
|
||||||
"\n",
|
|
||||||
"You'll use Azure Open Datasets to get the raw MNIST data files. [Azure Open Datasets](https://docs.microsoft.com/azure/open-datasets/overview-what-are-open-datasets) are curated public datasets that you can use to add scenario-specific features to machine learning solutions for better models. Each dataset has a corresponding class, `MNIST` in this case, to retrieve the data in different ways."
|
"You'll use Azure Open Datasets to get the raw MNIST data files. [Azure Open Datasets](https://docs.microsoft.com/azure/open-datasets/overview-what-are-open-datasets) are curated public datasets that you can use to add scenario-specific features to machine learning solutions for better models. Each dataset has a corresponding class, `MNIST` in this case, to retrieve the data in different ways."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"gather": {
|
|
||||||
"logged": 1612965922274
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"from azureml.core import Dataset\n",
|
|
||||||
"from azureml.opendatasets import MNIST\n",
|
"from azureml.opendatasets import MNIST\n",
|
||||||
"\n",
|
"\n",
|
||||||
"data_folder = os.path.join(os.getcwd(), \"data\")\n",
|
"data_folder = os.path.join(os.getcwd(), \"/tmp/qs_data\")\n",
|
||||||
"os.makedirs(data_folder, exist_ok=True)\n",
|
"os.makedirs(data_folder, exist_ok=True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"mnist_file_dataset = MNIST.get_file_dataset()\n",
|
"mnist_file_dataset = MNIST.get_file_dataset()\n",
|
||||||
"mnist_file_dataset.download(data_folder, overwrite=True)\n",
|
"mnist_file_dataset.download(data_folder, overwrite=True)"
|
||||||
"\n",
|
|
||||||
"mnist_file_dataset = mnist_file_dataset.register(\n",
|
|
||||||
" workspace=ws,\n",
|
|
||||||
" name=\"mnist_opendataset\",\n",
|
|
||||||
" description=\"training and test dataset\",\n",
|
|
||||||
" create_new_version=True,\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -157,20 +84,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"gather": {
|
|
||||||
"logged": 1612965929041
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from utils import load_data\n",
|
"from utils import load_data\n",
|
||||||
@@ -236,13 +150,13 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Train model and log metrics\n",
|
"## Train model and log metrics with MLflow\n",
|
||||||
"\n",
|
"\n",
|
||||||
"You'll train the model using the code below. Your training runs and metrics will be registered in the experiment you created, so that this information is available after you've finished.\n",
|
"You'll train the model using the code below. Note that you are using MLflow autologging to track metrics and log model artefacts.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"You'll be using the [LogisticRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) classifier from the [SciKit Learn framework](https://scikit-learn.org/) to classify the data.\n",
|
"You'll be using the [LogisticRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) classifier from the [SciKit Learn framework](https://scikit-learn.org/) to classify the data.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"> **Note: The model training takes around 1 minute to complete.**"
|
"**Note: The model training takes approximately 2 minutes to complete.**"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -265,41 +179,43 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# create the model\n",
|
"# create the model\n",
|
||||||
|
"import mlflow\n",
|
||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
"from sklearn.linear_model import LogisticRegression\n",
|
"from sklearn.linear_model import LogisticRegression\n",
|
||||||
|
"from azureml.core import Workspace\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# connect to your workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"\n",
|
||||||
|
"# create experiment and start logging to a new run in the experiment\n",
|
||||||
|
"experiment_name = \"azure-ml-in10-mins-tutorial\"\n",
|
||||||
|
"\n",
|
||||||
|
"# set up MLflow to track the metrics\n",
|
||||||
|
"mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())\n",
|
||||||
|
"mlflow.set_experiment(experiment_name)\n",
|
||||||
|
"mlflow.autolog()\n",
|
||||||
|
"\n",
|
||||||
|
"# set up the Logistic regression model\n",
|
||||||
"reg = 0.5\n",
|
"reg = 0.5\n",
|
||||||
"clf = LogisticRegression(\n",
|
"clf = LogisticRegression(\n",
|
||||||
" C=1.0 / reg, solver=\"liblinear\", multi_class=\"auto\", random_state=42\n",
|
" C=1.0 / reg, solver=\"liblinear\", multi_class=\"auto\", random_state=42\n",
|
||||||
")\n",
|
")\n",
|
||||||
"clf.fit(X_train, y_train)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# make predictions using the test set and calculate the accuracy\n",
|
"# train the model\n",
|
||||||
"y_hat = clf.predict(X_test)\n",
|
"with mlflow.start_run() as run:\n",
|
||||||
"\n",
|
" clf.fit(X_train, y_train)"
|
||||||
"# calculate accuracy on the prediction\n",
|
|
||||||
"acc = np.average(y_hat == y_test)\n",
|
|
||||||
"print(\"Accuracy is\", acc)\n",
|
|
||||||
"\n",
|
|
||||||
"run.log(\"regularization rate\", np.float(reg))\n",
|
|
||||||
"run.log(\"accuracy\", np.float(acc))"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
|
"## View Experiment\n",
|
||||||
|
"In the left-hand menu in Azure Machine Learning Studio, select __Experiments__ and then select your experiment (azure-ml-in10-mins-tutorial). An experiment is a grouping of many runs from a specified script or piece of code. Information for the run is stored under that experiment. If the name doesn't exist when you submit an experiment, if you select your run you will see various tabs containing metrics, logs, explanations, etc.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Version control your models with the model registry\n",
|
"## Version control your models with the model registry\n",
|
||||||
"\n",
|
"\n",
|
||||||
"You can use model registration to store and version your models in your workspace. Registered models are identified by name and version. Each time you register a model with the same name as an existing one, the registry increments the version. Azure Machine Learning supports any model that can be loaded through Python 3.\n",
|
"You can use model registration to store and version your models in your workspace. Registered models are identified by name and version. Each time you register a model with the same name as an existing one, the registry increments the version. The code below registers and versions the model you trained above. Once you have executed the code cell below you will be able to see the model in the registry by selecting __Models__ in the left-hand menu in Azure Machine Learning Studio."
|
||||||
"\n",
|
|
||||||
"The code below:\n",
|
|
||||||
"\n",
|
|
||||||
"1. Saves the model to disk\n",
|
|
||||||
"1. Uploads the model file to the run \n",
|
|
||||||
"1. Registers the uploaded model file\n",
|
|
||||||
"1. Transitions the run to a completed state"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -321,30 +237,20 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import joblib\n",
|
"# register the model\n",
|
||||||
"from azureml.core.model import Model\n",
|
"model_uri = \"runs:/{}/model\".format(run.info.run_id)\n",
|
||||||
"\n",
|
"model = mlflow.register_model(model_uri, \"sklearn_mnist_model\")"
|
||||||
"path = \"sklearn_mnist_model.pkl\"\n",
|
|
||||||
"joblib.dump(value=clf, filename=path)\n",
|
|
||||||
"\n",
|
|
||||||
"run.upload_file(name=path, path_or_stream=path)\n",
|
|
||||||
"\n",
|
|
||||||
"model = run.register_model(\n",
|
|
||||||
" model_name=\"sklearn_mnist_model\",\n",
|
|
||||||
" model_path=path,\n",
|
|
||||||
" description=\"Mnist handwriting recognition\",\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
|
||||||
"run.complete()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Deploy the model\n",
|
"## Deploy the model for real-time inference\n",
|
||||||
|
"In this section you learn how to deploy a model so that an application can consume (inference) the model over REST.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The next cell deploys the model to an Azure Container Instance so that you can score data in real-time (Azure Machine Learning also provides mechanisms to do batch scoring). A real-time endpoint allows application developers to integrate machine learning into their apps."
|
"### Create deployment configuration\n",
|
||||||
|
"The code cell gets a _curated environment_, which specifies all the dependencies required to host the model (for example, the packages like scikit-learn). Also, you create a _deployment configuration_, which specifies the amount of compute required to host the model. In this case, the compute will have 1CPU and 1GB memory."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -369,22 +275,17 @@
|
|||||||
"# create environment for the deploy\n",
|
"# create environment for the deploy\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
||||||
"\n",
|
|
||||||
"# to install required packages\n",
|
|
||||||
"env = Environment(\"quickstart-env\")\n",
|
|
||||||
"cd = CondaDependencies.create(\n",
|
|
||||||
" pip_packages=[\"azureml-dataset-runtime[pandas,fuse]\", \"azureml-defaults\"],\n",
|
|
||||||
" conda_packages=[\"scikit-learn==0.22.1\"],\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
|
||||||
"env.python.conda_dependencies = cd\n",
|
|
||||||
"\n",
|
|
||||||
"# Register environment to re-use later\n",
|
|
||||||
"env.register(workspace=ws)\n",
|
|
||||||
"\n",
|
|
||||||
"# create config file\n",
|
|
||||||
"from azureml.core.webservice import AciWebservice\n",
|
"from azureml.core.webservice import AciWebservice\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# get a curated environment\n",
|
||||||
|
"env = Environment.get(\n",
|
||||||
|
" workspace=ws, \n",
|
||||||
|
" name=\"AzureML-sklearn-0.24.1-ubuntu18.04-py37-cpu-inference\",\n",
|
||||||
|
" version=1\n",
|
||||||
|
")\n",
|
||||||
|
"env.inferencing_stack_version='latest'\n",
|
||||||
|
"\n",
|
||||||
|
"# create deployment config i.e. compute resources\n",
|
||||||
"aciconfig = AciWebservice.deploy_configuration(\n",
|
"aciconfig = AciWebservice.deploy_configuration(\n",
|
||||||
" cpu_cores=1,\n",
|
" cpu_cores=1,\n",
|
||||||
" memory_gb=1,\n",
|
" memory_gb=1,\n",
|
||||||
@@ -403,7 +304,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"> **Note: The deployment takes around 3 minutes to complete.**"
|
"### Deploy model\n",
|
||||||
|
"\n",
|
||||||
|
"This next code cell deploys the model to Azure Container Instance (ACI).\n",
|
||||||
|
"\n",
|
||||||
|
"**Note: The deployment takes approximately 3 minutes to complete.**"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -424,19 +329,17 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"%%time\n",
|
"%%time\n",
|
||||||
"import uuid\n",
|
"import uuid\n",
|
||||||
"from azureml.core.webservice import Webservice\n",
|
|
||||||
"from azureml.core.model import InferenceConfig\n",
|
"from azureml.core.model import InferenceConfig\n",
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"from azureml.core import Workspace\n",
|
|
||||||
"from azureml.core.model import Model\n",
|
"from azureml.core.model import Model\n",
|
||||||
"\n",
|
"\n",
|
||||||
"ws = Workspace.from_config()\n",
|
"# get the registered model\n",
|
||||||
"model = Model(ws, \"sklearn_mnist_model\")\n",
|
"model = Model(ws, \"sklearn_mnist_model\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# create an inference config i.e. the scoring script and environment\n",
|
||||||
|
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=env)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"myenv = Environment.get(workspace=ws, name=\"quickstart-env\", version=\"1\")\n",
|
"# deploy the service\n",
|
||||||
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)\n",
|
|
||||||
"\n",
|
|
||||||
"service_name = \"sklearn-mnist-svc-\" + str(uuid.uuid4())[:4]\n",
|
"service_name = \"sklearn-mnist-svc-\" + str(uuid.uuid4())[:4]\n",
|
||||||
"service = Model.deploy(\n",
|
"service = Model.deploy(\n",
|
||||||
" workspace=ws,\n",
|
" workspace=ws,\n",
|
||||||
@@ -456,7 +359,10 @@
|
|||||||
"The [*scoring script*](score.py) file referenced in the code above can be found in the same folder as this notebook, and has two functions:\n",
|
"The [*scoring script*](score.py) file referenced in the code above can be found in the same folder as this notebook, and has two functions:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"1. an `init` function that executes once when the service starts - in this function you normally get the model from the registry and set global variables\n",
|
"1. an `init` function that executes once when the service starts - in this function you normally get the model from the registry and set global variables\n",
|
||||||
"1. a `run(data)` function that executes each time a call is made to the service. In this function, you normally format the input data, run a prediction, and output the predicted result."
|
"1. a `run(data)` function that executes each time a call is made to the service. In this function, you normally format the input data, run a prediction, and output the predicted result.\n",
|
||||||
|
"\n",
|
||||||
|
"### View Endpoint\n",
|
||||||
|
"Once the model has been successfully deployed, you can view the endpoint by navigating to __Endpoints__ in the left-hand menu in Azure Machine Learning Studio. You will be able to see the state of the endpoint (healthy/unhealthy), logs, and consume (how applications can consume the model)."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -474,29 +380,6 @@
|
|||||||
"You can test the model by sending a raw HTTP request to test the web service. "
|
"You can test the model by sending a raw HTTP request to test the web service. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612881527399
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# scoring web service HTTP endpoint\n",
|
|
||||||
"print(service.scoring_uri)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -525,56 +408,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"headers = {\"Content-Type\": \"application/json\"}\n",
|
"headers = {\"Content-Type\": \"application/json\"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# for AKS deployment you'd need to the service key in the header as well\n",
|
|
||||||
"# api_key = service.get_key()\n",
|
|
||||||
"# headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}\n",
|
|
||||||
"\n",
|
|
||||||
"resp = requests.post(service.scoring_uri, input_data, headers=headers)\n",
|
"resp = requests.post(service.scoring_uri, input_data, headers=headers)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(\"POST to url\", service.scoring_uri)\n",
|
"print(\"POST to url\", service.scoring_uri)\n",
|
||||||
"# print(\"input data:\", input_data)\n",
|
|
||||||
"print(\"label:\", y_test[random_index])\n",
|
"print(\"label:\", y_test[random_index])\n",
|
||||||
"print(\"prediction:\", resp.text)"
|
"print(\"prediction:\", resp.text)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"\n",
|
|
||||||
"### View the results of your training\n",
|
|
||||||
"\n",
|
|
||||||
"When you're finished with an experiment run, you can always return to view the results of your model training here in the Azure Machine Learning studio:\n",
|
|
||||||
"\n",
|
|
||||||
"1. Select **Experiments** (left-hand menu)\n",
|
|
||||||
"1. Select **azure-ml-in10-mins-tutorial**\n",
|
|
||||||
"1. Select **Run 1**\n",
|
|
||||||
"1. Select the **Metrics** Tab\n",
|
|
||||||
"\n",
|
|
||||||
"The metrics tab will display the parameter values that were logged to the run."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### View the model in the model registry\n",
|
|
||||||
"\n",
|
|
||||||
"You can see the stored model by navigating to **Models** in the left-hand menu bar. Select the **sklearn_mnist_model** to see the details of the model, including the experiment run ID that created the model."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ def init():
|
|||||||
# AZUREML_MODEL_DIR is an environment variable created during deployment.
|
# AZUREML_MODEL_DIR is an environment variable created during deployment.
|
||||||
# It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)
|
# It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)
|
||||||
# For multiple models, it points to the folder containing all deployed models (./azureml-models)
|
# For multiple models, it points to the folder containing all deployed models (./azureml-models)
|
||||||
model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "sklearn_mnist_model.pkl")
|
model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "model/model.pkl")
|
||||||
model = joblib.load(model_path)
|
model = joblib.load(model_path)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,12 +17,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"# Quickstart: Learn how to get started with Azure ML Job Submission\n",
|
"# Quickstart: Learn how to submit batch jobs with the Azure Machine Learning Python SDK\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In this quickstart, you train a machine learning model by submitting a Job to a compute target. \n",
|
"In this quickstart, you learn how to submit a batch training job using the Python SDK. In this example, we submit the job to the 'local' machine (the compute instance you are running this notebook on). However, you can use exactly the same method to submit the job to different compute targets (for example, AKS, Azure Machine Learning Compute Cluster, Synapse, etc) by changing a single line of code. A full list of support compute targets can be viewed [here](https://docs.microsoft.com/en-us/azure/machine-learning/concept-compute-target). \n",
|
||||||
"When training, it is common to start on your local computer, and then later scale out to a cloud-based cluster. \n",
|
|
||||||
"\n",
|
|
||||||
"All you need to do is define the environment for each compute target within a script run configuration. Then, when you want to run your training experiment on a different compute target, specify the run configuration for that compute.\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"This quickstart trains a simple logistic regression using the [MNIST](https://azure.microsoft.com/services/open-datasets/catalog/mnist/) dataset and [scikit-learn](http://scikit-learn.org) with Azure Machine Learning. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of 28x28 pixels, representing a number from 0 to 9. The goal is to create a multi-class classifier to identify the digit a given image represents. \n",
|
"This quickstart trains a simple logistic regression using the [MNIST](https://azure.microsoft.com/services/open-datasets/catalog/mnist/) dataset and [scikit-learn](http://scikit-learn.org) with Azure Machine Learning. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of 28x28 pixels, representing a number from 0 to 9. The goal is to create a multi-class classifier to identify the digit a given image represents. \n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -30,6 +27,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"> * Download a dataset and look at the data\n",
|
"> * Download a dataset and look at the data\n",
|
||||||
"> * Train an image classification model by submitting a batch job to a compute resource\n",
|
"> * Train an image classification model by submitting a batch job to a compute resource\n",
|
||||||
|
"> * Use MLflow autologging to track model metrics and log the model artefact\n",
|
||||||
"> * Review training results, find and register the best model"
|
"> * Review training results, find and register the best model"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -67,16 +65,14 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import numpy as np\r\n",
|
"\n",
|
||||||
"import matplotlib.pyplot as plt\r\n",
|
"from azureml.core import Workspace\n",
|
||||||
"\r\n",
|
"from azureml.core import Experiment\n",
|
||||||
"from azureml.core import Workspace\r\n",
|
"\n",
|
||||||
"from azureml.core import Experiment\r\n",
|
"# connect to your workspace\n",
|
||||||
"\r\n",
|
"ws = Workspace.from_config()\n",
|
||||||
"# connect to your workspace\r\n",
|
"\n",
|
||||||
"ws = Workspace.from_config()\r\n",
|
"experiment_name = \"get-started-with-jobsubmission-tutorial\"\n",
|
||||||
"\r\n",
|
|
||||||
"experiment_name = \"get-started-with-jobsubmission-tutorial\"\r\n",
|
|
||||||
"exp = Experiment(workspace=ws, name=experiment_name)"
|
"exp = Experiment(workspace=ws, name=experiment_name)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -90,14 +86,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Import Data\n",
|
"### The MNIST dataset\n",
|
||||||
"\n",
|
|
||||||
"Before you train a model, you need to understand the data that you are using to train it. In this section you will:\n",
|
|
||||||
"\n",
|
|
||||||
"* Download the MNIST dataset\n",
|
|
||||||
"* Display some sample images\n",
|
|
||||||
"\n",
|
|
||||||
"### Download the MNIST dataset\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"Use Azure Open Datasets to get the raw MNIST data files. [Azure Open Datasets](https://docs.microsoft.com/azure/open-datasets/overview-what-are-open-datasets) are curated public datasets that you can use to add scenario-specific features to machine learning solutions for more accurate models. Each dataset has a corresponding class, `MNIST` in this case, to retrieve the data in different ways.\n",
|
"Use Azure Open Datasets to get the raw MNIST data files. [Azure Open Datasets](https://docs.microsoft.com/azure/open-datasets/overview-what-are-open-datasets) are curated public datasets that you can use to add scenario-specific features to machine learning solutions for more accurate models. Each dataset has a corresponding class, `MNIST` in this case, to retrieve the data in different ways.\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -123,215 +112,16 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import os\n",
|
|
||||||
"from azureml.core import Dataset\n",
|
|
||||||
"from azureml.opendatasets import MNIST\n",
|
"from azureml.opendatasets import MNIST\n",
|
||||||
"\n",
|
"\n",
|
||||||
"data_folder = os.path.join(os.getcwd(), \"data\")\n",
|
"mnist_file_dataset = MNIST.get_file_dataset()"
|
||||||
"os.makedirs(data_folder, exist_ok=True)\n",
|
|
||||||
"\n",
|
|
||||||
"mnist_file_dataset = MNIST.get_file_dataset()\n",
|
|
||||||
"mnist_file_dataset.download(data_folder, overwrite=True)\n",
|
|
||||||
"\n",
|
|
||||||
"mnist_file_dataset = mnist_file_dataset.register(\n",
|
|
||||||
" workspace=ws,\n",
|
|
||||||
" name=\"mnist_opendataset\",\n",
|
|
||||||
" description=\"training and test dataset\",\n",
|
|
||||||
" create_new_version=True,\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Take a look at the data\n",
|
|
||||||
"You will load the compressed files into `numpy` arrays. Then use `matplotlib` to plot 30 random images from the dataset with their labels above them. Note this step requires a `load_data` function that's included in an `utils.py` file. This file is placed in the same folder as this notebook. The `load_data` function simply parses the compressed files into numpy arrays. \n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612965857960
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# make sure utils.py is in the same directory as this code\r\n",
|
|
||||||
"from src.utils import load_data\r\n",
|
|
||||||
"import glob\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"# note we also shrink the intensity values (X) from 0-255 to 0-1. This helps the model converge faster.\r\n",
|
|
||||||
"X_train = (\r\n",
|
|
||||||
" load_data(\r\n",
|
|
||||||
" glob.glob(\r\n",
|
|
||||||
" os.path.join(data_folder, \"**/train-images-idx3-ubyte.gz\"), recursive=True\r\n",
|
|
||||||
" )[0],\r\n",
|
|
||||||
" False,\r\n",
|
|
||||||
" )\r\n",
|
|
||||||
" / 255.0\r\n",
|
|
||||||
")\r\n",
|
|
||||||
"X_test = (\r\n",
|
|
||||||
" load_data(\r\n",
|
|
||||||
" glob.glob(\r\n",
|
|
||||||
" os.path.join(data_folder, \"**/t10k-images-idx3-ubyte.gz\"), recursive=True\r\n",
|
|
||||||
" )[0],\r\n",
|
|
||||||
" False,\r\n",
|
|
||||||
" )\r\n",
|
|
||||||
" / 255.0\r\n",
|
|
||||||
")\r\n",
|
|
||||||
"y_train = load_data(\r\n",
|
|
||||||
" glob.glob(\r\n",
|
|
||||||
" os.path.join(data_folder, \"**/train-labels-idx1-ubyte.gz\"), recursive=True\r\n",
|
|
||||||
" )[0],\r\n",
|
|
||||||
" True,\r\n",
|
|
||||||
").reshape(-1)\r\n",
|
|
||||||
"y_test = load_data(\r\n",
|
|
||||||
" glob.glob(\r\n",
|
|
||||||
" os.path.join(data_folder, \"**/t10k-labels-idx1-ubyte.gz\"), recursive=True\r\n",
|
|
||||||
" )[0],\r\n",
|
|
||||||
" True,\r\n",
|
|
||||||
").reshape(-1)\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"\r\n",
|
|
||||||
"# now let's show some randomly chosen images from the training set.\r\n",
|
|
||||||
"count = 0\r\n",
|
|
||||||
"sample_size = 30\r\n",
|
|
||||||
"plt.figure(figsize=(16, 6))\r\n",
|
|
||||||
"for i in np.random.permutation(X_train.shape[0])[:sample_size]:\r\n",
|
|
||||||
" count = count + 1\r\n",
|
|
||||||
" plt.subplot(1, sample_size, count)\r\n",
|
|
||||||
" plt.axhline(\"\")\r\n",
|
|
||||||
" plt.axvline(\"\")\r\n",
|
|
||||||
" plt.text(x=10, y=-10, s=y_train[i], fontsize=18)\r\n",
|
|
||||||
" plt.imshow(X_train[i].reshape(28, 28), cmap=plt.cm.Greys)\r\n",
|
|
||||||
"plt.show()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"## Submit your training job\n",
|
|
||||||
"\n",
|
|
||||||
"In this quickstart you submit a job to run on the local compute, but you can use the same code to submit this training job to other compute targets. With Azure Machine Learning, you can run your script on various compute targets without having to change your training script. \n",
|
|
||||||
"\n",
|
|
||||||
"To submit a job you need:\n",
|
|
||||||
"* A directory\n",
|
|
||||||
"* A training script\n",
|
|
||||||
"* Create a script run configuration\n",
|
|
||||||
"* Submit the job \n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"### Directory and training script \n",
|
|
||||||
"\n",
|
|
||||||
"You need a directory to deliver the necessary code from your computer to the remote resource. A directory with a training script has been created for you and can be found in the same folder as this notebook.\n",
|
|
||||||
"\n",
|
|
||||||
"Take a few minutes to examine the training script."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612965865707
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"with open(\"./src/train.py\", \"r\") as f:\n",
|
|
||||||
" print(f.read())"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"Notice how the script gets data and saves models:\n",
|
|
||||||
"\n",
|
|
||||||
"+ The training script reads an argument to find the directory containing the data. When you submit the job later, you point to the dataset for this argument:\n",
|
|
||||||
"`parser.add_argument('--data-folder', type=str, dest='data_folder', help='data directory mounting point')`\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"+ The training script saves your model into a directory named outputs. <br/>\n",
|
|
||||||
"`joblib.dump(value=clf, filename='outputs/sklearn_mnist_model.pkl')`<br/>\n",
|
|
||||||
"Anything written in this directory is automatically uploaded into your workspace. You'll access your model from this directory later in the tutorial.\n",
|
|
||||||
"\n",
|
|
||||||
"The file `utils.py` is referenced from the training script to load the dataset correctly. This script is also copied into the script folder so that it can be accessed along with the training script."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Configure the training job\n",
|
|
||||||
"\n",
|
|
||||||
"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. Configure the ScriptRunConfig by specifying:\n",
|
|
||||||
"\n",
|
|
||||||
"* The directory that contains your scripts. All the files in this directory are uploaded into the cluster nodes for execution. \n",
|
|
||||||
"* The compute target. In this case you will point to local compute\n",
|
|
||||||
"* The training script name, train.py\n",
|
|
||||||
"* An environment that contains the libraries needed to run the script\n",
|
|
||||||
"* Arguments required from the training script. \n",
|
|
||||||
"\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
"source": [
|
||||||
|
"### Define the Environment\n",
|
||||||
"An Environment defines Python packages, environment variables, and Docker settings that are used in machine learning experiments. Here you will be using a curated environment that has already been made available through the workspace. \n",
|
"An Environment defines Python packages, environment variables, and Docker settings that are used in machine learning experiments. Here you will be using a curated environment that has already been made available through the workspace. \n",
|
||||||
"\n",
|
"\n",
|
||||||
"Read [this article](https://docs.microsoft.com/azure/machine-learning/how-to-use-environments) if you want to learn more about Environments and how to use them."
|
"Read [this article](https://docs.microsoft.com/azure/machine-learning/how-to-use-environments) if you want to learn more about Environments and how to use them."
|
||||||
@@ -357,11 +147,12 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from azureml.core.environment import Environment\n",
|
"from azureml.core.environment import Environment\n",
|
||||||
"from azureml.core.conda_dependencies import CondaDependencies\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# use a curated environment that has already been built for you\n",
|
"# use a curated environment that has already been built for you\n",
|
||||||
"\n",
|
"\n",
|
||||||
"env = Environment.get(workspace=ws, name=\"AzureML-Scikit-learn-0.20.3\")"
|
"env = Environment.get(workspace=ws, \n",
|
||||||
|
" name=\"AzureML-Scikit-learn0.24-Cuda11-OpenMpi4.1.0-py36\", \n",
|
||||||
|
" version=1)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -374,9 +165,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"Create a [ScriptRunConfig](https://docs.microsoft.com/python/api/azureml-core/azureml.core.scriptrunconfig?preserve-view=true&view=azure-ml-py) object to specify the configuration details of your training job, including your training script, environment to use, and the compute target to run on. A script run configuration is used to configure the information necessary for submitting a training run as part of an experiment. In this case we will run this on a 'local' compute target, which is the compute instance you are running this notebook on.\r\n",
|
"### Configure the training job\n",
|
||||||
"\r\n",
|
"\n",
|
||||||
"Read more about configuring and submitting training runs [here](https://docs.microsoft.com/azure/machine-learning/how-to-set-up-training-targets). "
|
"Create a [ScriptRunConfig](https://docs.microsoft.com/python/api/azureml-core/azureml.core.script_run_config.scriptrunconfig?view=azure-ml-py) object to specify the configuration details of your training job, including your training script, environment to use, and the compute target to run on. Configure the ScriptRunConfig by specifying:\n",
|
||||||
|
"\n",
|
||||||
|
"* The directory that contains your scripts. All the files in this directory are uploaded into the cluster nodes for execution. \n",
|
||||||
|
"* The compute target. In this case you will point to local compute\n",
|
||||||
|
"* The training script name, train.py\n",
|
||||||
|
"* An environment that contains the libraries needed to run the script\n",
|
||||||
|
"* Arguments required from the training script. \n",
|
||||||
|
"\n",
|
||||||
|
"In this run we will be submitting to \"local\", which is the compute instance you are running this notebook. If you have another compute target (for example: AKS, Azure ML Compute Cluster, Azure Databricks, etc) then you just need to change the `compute_target` argument below. You can learn more about other compute targets [here](https://docs.microsoft.com/azure/machine-learning/how-to-set-up-training-targets). "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -423,7 +222,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"### Submit the job\n",
|
"### Submit the job\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Run the experiment by submitting the ScriptRunConfig object. After this there are many options for monitoring your run. You can either navigate to the experiment \"get-started-with-jobsubmission-tutorial\" in the left menu item Experiments to monitor the run (quick link to the run details page in the cell output below), or you can monitor the run inline in this notebook by using the Jupyter widget activated below."
|
"Run the experiment by submitting the ScriptRunConfig object. After this there are many options for monitoring your run. Once submitted, you can either navigate to the experiment \"get-started-with-jobsubmission-tutorial\" in the left menu item __Experiments__ to monitor the run, or you can monitor the run inline as the `run.wait_for_completion(show_output=True)` will stream the logs of the run. You will see that the environment is built for you to ensure reproducibility - this adds a couple of minutes to the run time. On subsequent runs, the environment is re-used making the runtime shorter."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -446,137 +245,9 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"run = exp.submit(config=src)\n",
|
"run = exp.submit(config=src)\n",
|
||||||
"run"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Jupyter widget\n",
|
|
||||||
"\n",
|
|
||||||
"Watch the progress of the run with a Jupyter widget. Like the run submission, the widget is asynchronous and provides live updates every 10-15 seconds until the job completes.\n",
|
|
||||||
"\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612966026710
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from azureml.widgets import RunDetails\n",
|
|
||||||
"\n",
|
|
||||||
"RunDetails(run).show()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"if you want to cancel a run, you can follow [these instructions](https://aka.ms/aml-docs-cancel-run)."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Get log results upon completion\n",
|
|
||||||
"\n",
|
|
||||||
"Model training happens in the background. You can use `wait_for_completion` to block and wait until the model has completed training before running more code. "
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612966045110
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# specify show_output to True for a verbose log\n",
|
|
||||||
"run.wait_for_completion(show_output=True)"
|
"run.wait_for_completion(show_output=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"### Display run results\n",
|
|
||||||
"\n",
|
|
||||||
"You now have a trained model. Retrieve all the metrics logged during the run, including the accuracy of the model:"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612966059052
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"print(run.get_metrics())"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@@ -589,42 +260,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Register model\n",
|
"## Register model\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The last step in the training script wrote the file `outputs/sklearn_mnist_model.pkl` in a directory named `outputs` on the compute where the job is executed. `outputs` is a special directory in that all content in this directory is automatically uploaded to your workspace. This content appears in the run record in the experiment under your workspace. Hence, the model file is now also available in your workspace."
|
"The training script used the MLflow autologging feature and therefore the model was captured and stored on your behalf. Below we register the model into the Azure Machine Learning Model registry, which lets you keep track of all the models in your Azure Machine Learning workspace.\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"Models are identified by name and version. Each time you register a model with the same name as an existing one, the registry assumes that it's a new version. The version is incremented, and the new model is registered under the same name.\n",
|
||||||
{
|
"\n",
|
||||||
"cell_type": "code",
|
"When you register the model, you can provide additional metadata tags and then use the tags when you search for models."
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"gather": {
|
|
||||||
"logged": 1612966064041
|
|
||||||
},
|
|
||||||
"jupyter": {
|
|
||||||
"outputs_hidden": false,
|
|
||||||
"source_hidden": false
|
|
||||||
},
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"print(run.get_file_names())"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {
|
|
||||||
"nteract": {
|
|
||||||
"transient": {
|
|
||||||
"deleting": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"source": [
|
|
||||||
"Register the model in the workspace so that you (or your team members with access to the workspace) can later query, examine, and deploy this model."
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -648,11 +288,18 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# register model\n",
|
"# register model\n",
|
||||||
"model = run.register_model(\n",
|
"model = run.register_model(\n",
|
||||||
" model_name=\"sklearn_mnist\", model_path=\"outputs/sklearn_mnist_model.pkl\"\n",
|
" model_name=\"sklearn_mnist\", model_path=\"model/model.pkl\"\n",
|
||||||
")\n",
|
")\n",
|
||||||
"print(model.name, model.id, model.version, sep=\"\\t\")"
|
"print(model.name, model.id, model.version, sep=\"\\t\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"You will now be able to see the model in the regsitry by selecting __Models__ in the left-hand menu of the Azure Machine Learning Studio."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
@@ -2,11 +2,10 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import glob
|
import glob
|
||||||
|
import joblib
|
||||||
|
import mlflow
|
||||||
|
|
||||||
from sklearn.linear_model import LogisticRegression
|
from sklearn.linear_model import LogisticRegression
|
||||||
import joblib
|
|
||||||
|
|
||||||
from azureml.core import Run
|
|
||||||
from utils import load_data
|
from utils import load_data
|
||||||
|
|
||||||
# let user feed in 2 parameters, the dataset to mount or download,
|
# let user feed in 2 parameters, the dataset to mount or download,
|
||||||
@@ -58,8 +57,8 @@ y_test = load_data(
|
|||||||
|
|
||||||
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep="\n")
|
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep="\n")
|
||||||
|
|
||||||
# get hold of the current run
|
# use mlflow autologging
|
||||||
run = Run.get_context()
|
mlflow.autolog()
|
||||||
|
|
||||||
print("Train a logistic regression model with regularization rate of", args.reg)
|
print("Train a logistic regression model with regularization rate of", args.reg)
|
||||||
clf = LogisticRegression(
|
clf = LogisticRegression(
|
||||||
@@ -73,10 +72,3 @@ y_hat = clf.predict(X_test)
|
|||||||
# calculate accuracy on the prediction
|
# calculate accuracy on the prediction
|
||||||
acc = np.average(y_hat == y_test)
|
acc = np.average(y_hat == y_test)
|
||||||
print("Accuracy is", acc)
|
print("Accuracy is", acc)
|
||||||
|
|
||||||
run.log("regularization rate", np.float(args.reg))
|
|
||||||
run.log("accuracy", np.float(acc))
|
|
||||||
|
|
||||||
os.makedirs("outputs", exist_ok=True)
|
|
||||||
# note file saved in the outputs folder is automatically uploaded into experiment record
|
|
||||||
joblib.dump(value=clf, filename="outputs/sklearn_mnist_model.pkl")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user