Skip to content

Commit 5cb4651

Browse files
authored
Merge pull request Azure#1556 from Azure/update-spark-notebook
updating spark notebook
2 parents d835b18 + 0ce37dd commit 5cb4651

File tree

1 file changed

+113
-105
lines changed

1 file changed

+113
-105
lines changed

how-to-use-azureml/deployment/spark/model-register-and-deploy-spark.ipynb

Lines changed: 113 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,144 +2,151 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {},
65
"source": [
76
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
87
"\n",
98
"Licensed under the MIT License."
10-
]
9+
],
10+
"metadata": {}
1111
},
1212
{
1313
"cell_type": "markdown",
14-
"metadata": {},
1514
"source": [
1615
"![Impressions](https://PixelServer20190423114238.azurewebsites.net/api/impressions/MachineLearningNotebooks/how-to-use-azureml/deployment/deploy-to-cloud/model-register-and-deploy.png)"
17-
]
16+
],
17+
"metadata": {}
1818
},
1919
{
2020
"cell_type": "markdown",
21-
"metadata": {},
2221
"source": [
2322
"# Register Spark Model and deploy as Webservice\n",
2423
"\n",
2524
"This example shows how to deploy a Webservice in step-by-step fashion:\n",
2625
"\n",
2726
" 1. Register Spark Model\n",
2827
" 2. Deploy Spark Model as Webservice"
29-
]
28+
],
29+
"metadata": {}
3030
},
3131
{
3232
"cell_type": "markdown",
33-
"metadata": {},
3433
"source": [
3534
"## Prerequisites\n",
3635
"If you are using an Azure Machine Learning Notebook VM, you are all set. Otherwise, make sure you go through the [configuration](../../../configuration.ipynb) Notebook first if you haven't."
37-
]
36+
],
37+
"metadata": {}
3838
},
3939
{
4040
"cell_type": "code",
4141
"execution_count": null,
42-
"metadata": {},
43-
"outputs": [],
4442
"source": [
45-
"# Check core SDK version number\n",
46-
"import azureml.core\n",
47-
"\n",
43+
"# Check core SDK version number\r\n",
44+
"import azureml.core\r\n",
45+
"\r\n",
4846
"print(\"SDK version:\", azureml.core.VERSION)"
49-
]
47+
],
48+
"outputs": [],
49+
"metadata": {}
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"metadata": {},
5453
"source": [
5554
"## Initialize Workspace\n",
5655
"\n",
5756
"Initialize a workspace object from persisted configuration."
58-
]
57+
],
58+
"metadata": {}
5959
},
6060
{
6161
"cell_type": "code",
6262
"execution_count": null,
63+
"source": [
64+
"from azureml.core import Workspace\r\n",
65+
"\r\n",
66+
"ws = Workspace.from_config()\r\n",
67+
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\\n')"
68+
],
69+
"outputs": [],
6370
"metadata": {
6471
"tags": [
6572
"create workspace"
6673
]
67-
},
68-
"outputs": [],
69-
"source": [
70-
"from azureml.core import Workspace\n",
71-
"\n",
72-
"ws = Workspace.from_config()\n",
73-
"print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\\n')"
74-
]
74+
}
7575
},
7676
{
7777
"cell_type": "markdown",
78-
"metadata": {},
7978
"source": [
8079
"### Register Model"
81-
]
80+
],
81+
"metadata": {}
8282
},
8383
{
8484
"cell_type": "markdown",
85-
"metadata": {},
8685
"source": [
8786
"You can add tags and descriptions to your Models. Note you need to have a `iris.model` file in the current directory. This model file is generated using [train in spark](../training/train-in-spark/train-in-spark.ipynb) notebook. The below call registers that file as a Model with the same name `iris.model` in the workspace.\n",
8887
"\n",
8988
"Using tags, you can track useful information such as the name and version of the machine learning library used to train the model. Note that tags must be alphanumeric."
90-
]
89+
],
90+
"metadata": {}
9191
},
9292
{
9393
"cell_type": "code",
9494
"execution_count": null,
95+
"source": [
96+
"from azureml.core.model import Model\r\n",
97+
"\r\n",
98+
"model = Model.register(model_path=\"iris.model\",\r\n",
99+
" model_name=\"iris.model\",\r\n",
100+
" tags={'type': \"regression\"},\r\n",
101+
" description=\"Logistic regression model to predict iris species\",\r\n",
102+
" workspace=ws)"
103+
],
104+
"outputs": [],
95105
"metadata": {
96106
"tags": [
97107
"register model from file"
98108
]
99-
},
100-
"outputs": [],
101-
"source": [
102-
"from azureml.core.model import Model\n",
103-
"\n",
104-
"model = Model.register(model_path=\"iris.model\",\n",
105-
" model_name=\"iris.model\",\n",
106-
" tags={'type': \"regression\"},\n",
107-
" description=\"Logistic regression model to predict iris species\",\n",
108-
" workspace=ws)"
109-
]
109+
}
110110
},
111111
{
112112
"cell_type": "markdown",
113-
"metadata": {},
114113
"source": [
115114
"### Fetch Environment"
116-
]
115+
],
116+
"metadata": {}
117117
},
118118
{
119119
"cell_type": "markdown",
120-
"metadata": {},
121120
"source": [
122121
"You can now create and/or use an Environment object when deploying a Webservice. The Environment can have been previously registered with your Workspace, or it will be registered with it as a part of the Webservice deployment.\n",
123122
"\n",
124123
"In this notebook, we will be using 'AzureML-PySpark-MmlSpark-0.15', a curated environment.\n",
125124
"\n",
126125
"More information can be found in our [using environments notebook](../training/using-environments/using-environments.ipynb)."
127-
]
126+
],
127+
"metadata": {}
128128
},
129129
{
130130
"cell_type": "code",
131131
"execution_count": null,
132-
"metadata": {},
133-
"outputs": [],
134132
"source": [
135-
"from azureml.core import Environment\n",
136-
"\n",
137-
"env = Environment.get(ws, name='AzureML-PySpark-MmlSpark-0.15')\n"
138-
]
133+
"from azureml.core import Environment\r\n",
134+
"from azureml.core.environment import SparkPackage\r\n",
135+
"from azureml.core.conda_dependencies import CondaDependencies\r\n",
136+
"\r\n",
137+
"myenv = Environment('my-pyspark-environment')\r\n",
138+
"myenv.docker.base_image = \"mcr.microsoft.com/mmlspark/release:0.15\"\r\n",
139+
"myenv.inferencing_stack_version = \"latest\"\r\n",
140+
"myenv.python.conda_dependencies = CondaDependencies.create(pip_packages=[\"azureml-core\",\"azureml-defaults\",\"azureml-telemetry\",\"azureml-train-restclients-hyperdrive\",\"azureml-train-core\"], python_version=\"3.6.2\")\r\n",
141+
"myenv.python.conda_dependencies.add_channel(\"conda-forge\")\r\n",
142+
"myenv.spark.packages = [SparkPackage(\"com.microsoft.ml.spark\", \"mmlspark_2.11\", \"0.15\"), SparkPackage(\"com.microsoft.azure\", \"azure-storage\", \"2.0.0\"), SparkPackage(\"org.apache.hadoop\", \"hadoop-azure\", \"2.7.0\")]\r\n",
143+
"myenv.spark.repositories = [\"https://mmlspark.azureedge.net/maven\"]\r\n"
144+
],
145+
"outputs": [],
146+
"metadata": {}
139147
},
140148
{
141149
"cell_type": "markdown",
142-
"metadata": {},
143150
"source": [
144151
"## Create Inference Configuration\n",
145152
"\n",
@@ -157,109 +164,109 @@
157164
" - source_directory = holds source path as string, this entire folder gets added in image so its really easy to access any files within this folder or subfolder\n",
158165
" - entry_script = contains logic specific to initializing your model and running predictions\n",
159166
" - environment = An environment object to use for the deployment. Doesn't have to be registered"
160-
]
167+
],
168+
"metadata": {}
161169
},
162170
{
163171
"cell_type": "code",
164172
"execution_count": null,
173+
"source": [
174+
"from azureml.core.model import InferenceConfig\r\n",
175+
"\r\n",
176+
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=myenv)"
177+
],
178+
"outputs": [],
165179
"metadata": {
166180
"tags": [
167181
"create image"
168182
]
169-
},
170-
"outputs": [],
171-
"source": [
172-
"from azureml.core.model import InferenceConfig\n",
173-
"\n",
174-
"inference_config = InferenceConfig(entry_script=\"score.py\", environment=env)"
175-
]
183+
}
176184
},
177185
{
178186
"cell_type": "markdown",
179-
"metadata": {},
180187
"source": [
181188
"### Deploy Model as Webservice on Azure Container Instance\n",
182189
"\n",
183190
"Note that the service creation can take few minutes."
184-
]
191+
],
192+
"metadata": {}
185193
},
186194
{
187195
"cell_type": "code",
188196
"execution_count": null,
197+
"source": [
198+
"from azureml.core.webservice import AciWebservice, Webservice\r\n",
199+
"from azureml.exceptions import WebserviceException\r\n",
200+
"\r\n",
201+
"deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)\r\n",
202+
"aci_service_name = 'aciservice1'\r\n",
203+
"\r\n",
204+
"try:\r\n",
205+
" # if you want to get existing service below is the command\r\n",
206+
" # since aci name needs to be unique in subscription deleting existing aci if any\r\n",
207+
" # we use aci_service_name to create azure aci\r\n",
208+
" service = Webservice(ws, name=aci_service_name)\r\n",
209+
" if service:\r\n",
210+
" service.delete()\r\n",
211+
"except WebserviceException as e:\r\n",
212+
" print()\r\n",
213+
"\r\n",
214+
"service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config)\r\n",
215+
"\r\n",
216+
"service.wait_for_deployment(True)\r\n",
217+
"print(service.state)"
218+
],
219+
"outputs": [],
189220
"metadata": {
190221
"tags": [
191222
"azuremlexception-remarks-sample"
192223
]
193-
},
194-
"outputs": [],
195-
"source": [
196-
"from azureml.core.webservice import AciWebservice, Webservice\n",
197-
"from azureml.exceptions import WebserviceException\n",
198-
"\n",
199-
"deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)\n",
200-
"aci_service_name = 'aciservice1'\n",
201-
"\n",
202-
"try:\n",
203-
" # if you want to get existing service below is the command\n",
204-
" # since aci name needs to be unique in subscription deleting existing aci if any\n",
205-
" # we use aci_service_name to create azure aci\n",
206-
" service = Webservice(ws, name=aci_service_name)\n",
207-
" if service:\n",
208-
" service.delete()\n",
209-
"except WebserviceException as e:\n",
210-
" print()\n",
211-
"\n",
212-
"service = Model.deploy(ws, aci_service_name, [model], inference_config, deployment_config)\n",
213-
"\n",
214-
"service.wait_for_deployment(True)\n",
215-
"print(service.state)"
216-
]
224+
}
217225
},
218226
{
219227
"cell_type": "markdown",
220-
"metadata": {},
221228
"source": [
222229
"#### Test web service"
223-
]
230+
],
231+
"metadata": {}
224232
},
225233
{
226234
"cell_type": "code",
227235
"execution_count": null,
228-
"metadata": {},
229-
"outputs": [],
230236
"source": [
231-
"import json\n",
232-
"test_sample = json.dumps({'features':{'type':1,'values':[4.3,3.0,1.1,0.1]},'label':2.0})\n",
233-
"\n",
234-
"test_sample_encoded = bytes(test_sample, encoding='utf8')\n",
235-
"prediction = service.run(input_data=test_sample_encoded)\n",
237+
"import json\r\n",
238+
"test_sample = json.dumps({'features':{'type':1,'values':[4.3,3.0,1.1,0.1]},'label':2.0})\r\n",
239+
"\r\n",
240+
"test_sample_encoded = bytes(test_sample, encoding='utf8')\r\n",
241+
"prediction = service.run(input_data=test_sample_encoded)\r\n",
236242
"print(prediction)"
237-
]
243+
],
244+
"outputs": [],
245+
"metadata": {}
238246
},
239247
{
240248
"cell_type": "markdown",
241-
"metadata": {},
242249
"source": [
243250
"#### Delete ACI to clean up"
244-
]
251+
],
252+
"metadata": {}
245253
},
246254
{
247255
"cell_type": "code",
248256
"execution_count": null,
257+
"source": [
258+
"service.delete()"
259+
],
260+
"outputs": [],
249261
"metadata": {
250262
"tags": [
251263
"deploy service",
252264
"aci"
253265
]
254-
},
255-
"outputs": [],
256-
"source": [
257-
"service.delete()"
258-
]
266+
}
259267
},
260268
{
261269
"cell_type": "markdown",
262-
"metadata": {},
263270
"source": [
264271
"### Model Profiling\n",
265272
"\n",
@@ -271,11 +278,11 @@
271278
"profiling_results = profile.get_results()\n",
272279
"print(profiling_results)\n",
273280
"```"
274-
]
281+
],
282+
"metadata": {}
275283
},
276284
{
277285
"cell_type": "markdown",
278-
"metadata": {},
279286
"source": [
280287
"### Model Packaging\n",
281288
"\n",
@@ -296,7 +303,8 @@
296303
"package.wait_for_creation(show_output=True)\n",
297304
"package.save(\"./local_context_dir\")\n",
298305
"```"
299-
]
306+
],
307+
"metadata": {}
300308
}
301309
],
302310
"metadata": {

0 commit comments

Comments
 (0)