|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Copyright (c) Microsoft Corporation. All rights reserved. \n", |
| 8 | + "Licensed under the MIT License." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "# Use LightGBM Estimator in Azure Machine Learning\n", |
| 23 | + "In this notebook we will demonstrate how to run a training job using LightGBM Estimator. [LightGBM](https://lightgbm.readthedocs.io/en/latest/) is a gradient boosting framework that uses tree based learning algorithms. " |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "## Prerequisites\n", |
| 31 | + "This notebook uses azureml-contrib-gbdt package, if you don't already have the package, please install by uncommenting below cell." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "#!pip install azureml-contrib-gbdt --extra-index-url https://azuremlsdktestpypi.azureedge.net/LightGBMPrivateRelease" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "from azureml.core import Workspace, Run, Experiment\n", |
| 50 | + "import shutil, os\n", |
| 51 | + "from azureml.widgets import RunDetails\n", |
| 52 | + "from azureml.contrib.gbdt import LightGBM\n", |
| 53 | + "from azureml.train.dnn import Mpi\n", |
| 54 | + "from azureml.core.compute import AmlCompute, ComputeTarget\n", |
| 55 | + "from azureml.core.compute_target import ComputeTargetException" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "If you are using an AzureML Compute Instance, you are all set. Otherwise, go through the [configuration.ipynb](../../../configuration.ipynb) notebook to install the Azure Machine Learning Python SDK and create an Azure ML Workspace" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## Set up machine learning resources" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "ws = Workspace.from_config()\n", |
| 79 | + "\n", |
| 80 | + "print('Workspace name: ' + ws.name, \n", |
| 81 | + " 'Azure region: ' + ws.location, \n", |
| 82 | + " 'Subscription id: ' + ws.subscription_id, \n", |
| 83 | + " 'Resource group: ' + ws.resource_group, sep = '\\n')" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "cluster_vm_size = \"STANDARD_DS14_V2\"\n", |
| 93 | + "cluster_min_nodes = 0\n", |
| 94 | + "cluster_max_nodes = 20\n", |
| 95 | + "cpu_cluster_name = 'TrainingCompute' \n", |
| 96 | + "\n", |
| 97 | + "try:\n", |
| 98 | + " cpu_cluster = AmlCompute(ws, cpu_cluster_name)\n", |
| 99 | + " if cpu_cluster and type(cpu_cluster) is AmlCompute:\n", |
| 100 | + " print('found compute target: ' + cpu_cluster_name)\n", |
| 101 | + "except ComputeTargetException:\n", |
| 102 | + " print('creating a new compute target...')\n", |
| 103 | + " provisioning_config = AmlCompute.provisioning_configuration(vm_size = cluster_vm_size, \n", |
| 104 | + " vm_priority = 'lowpriority', \n", |
| 105 | + " min_nodes = cluster_min_nodes, \n", |
| 106 | + " max_nodes = cluster_max_nodes)\n", |
| 107 | + " cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, provisioning_config)\n", |
| 108 | + " \n", |
| 109 | + " # can poll for a minimum number of nodes and for a specific timeout. \n", |
| 110 | + " # if no min node count is provided it will use the scale settings for the cluster\n", |
| 111 | + " cpu_cluster.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)\n", |
| 112 | + " \n", |
| 113 | + " # For a more detailed view of current Azure Machine Learning Compute status, use get_status()\n", |
| 114 | + " print(cpu_cluster.get_status().serialize())" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "From this point, you can either upload training data file directly or use Datastore for training data storage\n", |
| 122 | + "## Upload training file from local" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": null, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "scripts_folder = \"scripts_folder\"\n", |
| 132 | + "if not os.path.isdir(scripts_folder):\n", |
| 133 | + " os.mkdir(scripts_folder)\n", |
| 134 | + "shutil.copy('./train.conf', os.path.join(scripts_folder, 'train.conf'))\n", |
| 135 | + "shutil.copy('./binary0.train', os.path.join(scripts_folder, 'binary0.train'))\n", |
| 136 | + "shutil.copy('./binary1.train', os.path.join(scripts_folder, 'binary1.train'))\n", |
| 137 | + "shutil.copy('./binary0.test', os.path.join(scripts_folder, 'binary0.test'))\n", |
| 138 | + "shutil.copy('./binary1.test', os.path.join(scripts_folder, 'binary1.test'))" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [], |
| 146 | + "source": [ |
| 147 | + "training_data_list=[\"binary0.train\", \"binary1.train\"]\n", |
| 148 | + "validation_data_list = [\"binary0.test\", \"binary1.test\"]\n", |
| 149 | + "lgbm = LightGBM(source_directory=scripts_folder, \n", |
| 150 | + " compute_target=cpu_cluster, \n", |
| 151 | + " distributed_training=Mpi(),\n", |
| 152 | + " node_count=2,\n", |
| 153 | + " lightgbm_config='train.conf',\n", |
| 154 | + " data=training_data_list,\n", |
| 155 | + " valid=validation_data_list\n", |
| 156 | + " )\n", |
| 157 | + "experiment_name = 'lightgbm-estimator-test'\n", |
| 158 | + "experiment = Experiment(ws, name=experiment_name)\n", |
| 159 | + "run = experiment.submit(lgbm, tags={\"test public docker image\": None})\n", |
| 160 | + "RunDetails(run).show()" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [], |
| 168 | + "source": [ |
| 169 | + "run.wait_for_completion(show_output=True)" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "## Use data reference" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "from azureml.core.datastore import Datastore\n", |
| 186 | + "from azureml.data.data_reference import DataReference\n", |
| 187 | + "datastore = ws.get_default_datastore()" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [ |
| 196 | + "datastore.upload(src_dir='.',\n", |
| 197 | + " target_path='.',\n", |
| 198 | + " show_progress=True)" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": null, |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [], |
| 206 | + "source": [ |
| 207 | + "training_data_list=[\"binary0.train\", \"binary1.train\"]\n", |
| 208 | + "validation_data_list = [\"binary0.test\", \"binary1.test\"]\n", |
| 209 | + "lgbm = LightGBM(source_directory='.', \n", |
| 210 | + " compute_target=cpu_cluster, \n", |
| 211 | + " distributed_training=Mpi(),\n", |
| 212 | + " node_count=2,\n", |
| 213 | + " inputs=[datastore.as_mount()],\n", |
| 214 | + " lightgbm_config='train.conf',\n", |
| 215 | + " data=training_data_list,\n", |
| 216 | + " valid=validation_data_list\n", |
| 217 | + " )\n", |
| 218 | + "experiment_name = 'lightgbm-estimator-test'\n", |
| 219 | + "experiment = Experiment(ws, name=experiment_name)\n", |
| 220 | + "run = experiment.submit(lgbm, tags={\"use datastore.as_mount()\": None})\n", |
| 221 | + "RunDetails(run).show()" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [ |
| 230 | + "run.wait_for_completion(show_output=True)" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "code", |
| 235 | + "execution_count": null, |
| 236 | + "metadata": {}, |
| 237 | + "outputs": [], |
| 238 | + "source": [ |
| 239 | + "# uncomment below and run if compute resources are no longer needed\n", |
| 240 | + "# cpu_cluster.delete() " |
| 241 | + ] |
| 242 | + } |
| 243 | + ], |
| 244 | + "metadata": { |
| 245 | + "authors": [ |
| 246 | + { |
| 247 | + "name": "jingywa" |
| 248 | + } |
| 249 | + ], |
| 250 | + "kernelspec": { |
| 251 | + "display_name": "Python 3.6", |
| 252 | + "language": "python", |
| 253 | + "name": "python36" |
| 254 | + }, |
| 255 | + "language_info": { |
| 256 | + "codemirror_mode": { |
| 257 | + "name": "ipython", |
| 258 | + "version": 3 |
| 259 | + }, |
| 260 | + "file_extension": ".py", |
| 261 | + "mimetype": "text/x-python", |
| 262 | + "name": "python", |
| 263 | + "nbconvert_exporter": "python", |
| 264 | + "pygments_lexer": "ipython3", |
| 265 | + "version": "3.6.9" |
| 266 | + } |
| 267 | + }, |
| 268 | + "nbformat": 4, |
| 269 | + "nbformat_minor": 2 |
| 270 | +} |
0 commit comments