-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathML_with_ADB_and_AML.py
328 lines (238 loc) · 10.7 KB
/
ML_with_ADB_and_AML.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Databricks notebook source
# MAGIC %md
# MAGIC # An End to End ML Demo with Azure Databricks and MLflow integrated with Azure ML
# MAGIC ### Read Data, Build ML Model (Spark ML & Scikit learn), Track with MLflow, Compare Models, Model Registry, Deploy to production as batch with UDF & as REST endpoint with AML
# COMMAND ----------
# MAGIC %md
# MAGIC ### ML Objective:
# MAGIC Here we use a timeseries data from 5 sensors. Goal is to create a ML model that can predict Sensor 5 value based on other sensors
# COMMAND ----------
# MAGIC %md
# MAGIC ###Import Training Data
# MAGIC <img src="https://mcg1stanstor00.blob.core.windows.net/images/demos/Ignite/delta.jpg" alt="Delta" width="600">
# MAGIC </br></br>
# MAGIC The training data for this notebook is simply some time series data from devices that includes a collection of sensor readings.
# MAGIC The data is stored in the Delta Lake format. The data can be downloaded in CSV [here](https://mcg1stanstor00.blob.core.windows.net/publicdata/sensors/sensordata.csv).
# COMMAND ----------
# MAGIC %md
# MAGIC ### Initial Setup
# COMMAND ----------
#Install required modules
dbutils.library.installPyPI("azureml-mlflow")
dbutils.library.restartPython()
# COMMAND ----------
from pyspark.sql.types import *
from pyspark.sql.functions import *
import mlflow
import mlflow.spark
import mlflow.sklearn
import mlflow.azureml
import azureml
import azureml.core
from azureml.core import Workspace
# COMMAND ----------
# MAGIC %md
# MAGIC ### Review Data
# COMMAND ----------
# Here data is already in Delta lake and registered as table within Databricks
# To simulate, download the data mentioned above, use the 'Data' tab on the left sidebar to upload and set as table.
dataDf = spark.table("sensor").where(col('Device') == 'Device001')
display(dataDf)
# COMMAND ----------
# MAGIC %md
# MAGIC #Experiment Tracking and Model Deployment
# MAGIC ##with MLFlow and Azure Machine Learning
# MAGIC <img src="https://raw.githubusercontent.com/iheartdatascience/ignite2020/master/aml_adb.jpg" alt="Better Together" width="800">
# MAGIC </br></br>
# MAGIC This notebook walks through a basic Machine Learning example. Training runs will be logged to Azure Machine Learning using MLFlow's open-source APIs. </br> A resulting model from one of the models will then be deployed using MLFlow APIs as a) a Spark Pandas UDF for batch scoring and b) a web service in Azure Machine Learning
# COMMAND ----------
# MAGIC %md
# MAGIC ##Basic Setup
# MAGIC <img src="https://raw.githubusercontent.com/iheartdatascience/ignite2020/master/notebookimage1.JPG" alt="Basic Setup" width="600">
# MAGIC </br></br>
# MAGIC
# MAGIC Basic setup requires that the Databricks Workspace is linked with the AML workspace
# COMMAND ----------
# MAGIC %md
# MAGIC ##Experiment Tracking with MLFlow and AML
# MAGIC <img src="https://raw.githubusercontent.com/iheartdatascience/ignite2020/master/experiment.jpg" alt="Experiment Tracking" width="750">
# MAGIC </br>
# MAGIC MLFlow logging APIs will be used to log training experiments, metrics, and artifacts to AML.
# COMMAND ----------
#Set MLFlow Experiment
experimentName = "/Users/[email protected]/ml101/ML_with_ADB_and_AML"
mlflow.set_experiment(experimentName)
# COMMAND ----------
# MAGIC %md
# MAGIC ### ML Model with Spark ML
# MAGIC <img src="https://mcg1stanstor00.blob.core.windows.net/images/demos/Ignite/spark.jpg" alt="Spark" width="150">
# COMMAND ----------
from pyspark.ml.feature import VectorAssembler
from pyspark.ml import Pipeline
from pyspark.ml.regression import RandomForestRegressor
from pyspark.ml.evaluation import RegressionEvaluator
# COMMAND ----------
# Split the data into training and test sets (30% held out for testing)
(train_data, test_data) = dataDf.randomSplit([0.7, 0.3])
# COMMAND ----------
# Incorporate all input fields as vector for regression pipeline
assembler = VectorAssembler(
inputCols=["Sensor1", "Sensor2", "Sensor3", "Sensor4"],
outputCol="features")
# COMMAND ----------
def regresionModel(stages, params, train, test):
pipeline = Pipeline(stages=stages)
with mlflow.start_run(run_name="Sensor Regression") as ml_run:
for k,v in params.items():
mlflow.log_param(k, v)
model = pipeline.fit(train)
predictions = model.transform(test)
# Select (prediction, true label) and compute test error
evaluator = RegressionEvaluator(
labelCol="Sensor5", predictionCol="prediction", metricName="mse")
mse = evaluator.evaluate(predictions)
evaluator = RegressionEvaluator(
labelCol="Sensor5", predictionCol="prediction", metricName="r2")
r2 = evaluator.evaluate(predictions)
#Log MLFlow Metrics and Model
mlflow.log_metric("mse", mse)
mlflow.log_metric("r2", r2)
mlflow.spark.log_model(model, "model")
print("Documented with MLflow Run id %s" % ml_run.info.run_uuid)
return mse, r2, ml_run.info
# COMMAND ----------
numTreesList = [10, 25]
maxDepthList = [5, 10]
for numTrees, maxDepth in [(numTrees,maxDepth) for numTrees in numTreesList for maxDepth in maxDepthList]:
params = {"numTrees":numTrees, "maxDepth":maxDepth, "model": "Radom Forest Regressor - SparkML"}
rf = RandomForestRegressor(featuresCol="features", labelCol="Sensor5", numTrees=numTrees, maxDepth=maxDepth)
mse, r2, ml_run_info = regresionModel([assembler, rf], params, train_data, test_data)
print("Trees: %s, Depth: %s, MSE: %s, R2: %s\n" % (numTrees, maxDepth, mse, r2))
# COMMAND ----------
# MAGIC %md
# MAGIC ### ML Model with Scikit Learn
# MAGIC <img src="https://mcg1stanstor00.blob.core.windows.net/images/demos/Ignite/skl.jpg" alt="SciKit Learn" width="150">
# COMMAND ----------
import pandas as pd
from sklearn.linear_model import LinearRegression, Lasso
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import RandomForestRegressor
#Setup Test/Train datasets
data = dataDf.toPandas()
x = data.drop(["Device", "Time", "Sensor5"], axis=1)
y = data[["Sensor5"]]
train_x, test_x, train_y, test_y = train_test_split(x,y,test_size=0.20, random_state=30)
#Train Models
device = "Device001"
resultsPdf = pd.DataFrame()
for numTrees, maxDepth in [(numTrees,maxDepth) for numTrees in numTreesList for maxDepth in maxDepthList]:
with mlflow.start_run(run_name="Sensor Regression"):
mlflow.log_param("maxDepth", maxDepth)
mlflow.log_param("numTrees", numTrees)
mlflow.log_param("model", "Radom Forest Regressor - scikit")
# Fit, train, and score the model
model = RandomForestRegressor(max_depth = maxDepth, n_estimators = numTrees)
model.fit(train_x, train_y)
preds = model.predict(test_x)
# Get Metrics
mse = mean_squared_error(test_y, preds)
r2 = r2_score(test_y, preds)
# Log Metrics and Model
mlflow.log_metric('mse', mse)
mlflow.log_metric('r2', r2)
mlflow.sklearn.log_model(model, "model")
# Build Metrics Table
results = [[device, maxDepth, numTrees, mse, r2]]
runResultsPdf = pd.DataFrame(results, columns =['Device', 'MaxDepth', 'NumTrees', 'MSE', 'r2'])
resultsPdf = resultsPdf.append(runResultsPdf)
last_run_id = mlflow.active_run().info.run_id
display(resultsPdf)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Model Deployment
# MAGIC <img src="https://raw.githubusercontent.com/iheartdatascience/ignite2020/master/model_deployment.jpg" alt="Model Deployment" width="800">
# MAGIC </br></br>
# MAGIC Using MLFlow APIs, models can be deployed to AML and turned into web services, or they can be deployed as MLFlow model objects
# MAGIC </br>and used in streaming or batch pipelines as Python functions or Pandas UDFs.
# COMMAND ----------
# MAGIC %md
# MAGIC ### Deploy Model for Batch Scoring
# MAGIC <img src="https://mcg1stanstor00.blob.core.windows.net/images/demos/Ignite/deploylake.jpg" alt="Model Deployment" width="800">
# MAGIC </br></br>
# MAGIC Using MLFlow APIs, the Scikit Learn MLFlow Model will be exported out of AML and put in the Data Lake where it can be more widely accessed.
# COMMAND ----------
model_uri = "runs:/"+last_run_id+"/model"
# COMMAND ----------
# MAGIC %md
# MAGIC #### Use Apache Spark for Batch Scoring
# MAGIC <img src="https://raw.githubusercontent.com/iheartdatascience/ignite2020/master/batch_scoring.jpg" alt="Model Deployment" width="800">
# MAGIC </br></br>
# MAGIC The MLFlow model will be loaded and used as a Spark Pandas UDF to score new data.
# COMMAND ----------
from pyspark.sql.types import ArrayType, FloatType
#Create a Spark UDF for the MLFlow model
pyfunc_udf = mlflow.pyfunc.spark_udf(spark, model_uri)
#Load Scoring Data into Spark Dataframe
scoreDf = spark.table("sensor").where(col('Device') == 'Device100')
#Make Prediction
preds = (scoreDf
.withColumn('Sensor5-prediction', pyfunc_udf('Sensor1', 'Sensor2', 'Sensor3', 'Sensor4'))
)
display(preds)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Deploy Model as a Web Service in AML
# MAGIC <img src="https://mcg1stanstor00.blob.core.windows.net/images/demos/Ignite/deploywebservice.jpg" alt="Model Deployment" width="800">
# MAGIC </br></br>
# MAGIC The MLFlow model will conainerized and deployed as a web service with AML and Azure Container Instances
# COMMAND ----------
# workspace_name = "<WORKSPACE_NAME>"
# workspace_location="<WORKSPACE_LOCATION>"
# resource_group = "<RESOURCE_GROUP>"
# subscription_id = "<SUBSCRIPTION_ID>"
workspace = Workspace.create(name = workspace_name,
subscription_id = subscription_id,
resource_group = resource_group,
location = workspace_location,
exist_ok=True)
# COMMAND ----------
experimentName = "ml101-webinar"
azure_service, azure_model = mlflow.azureml.deploy(model_uri=model_uri,
service_name=experimentName + "-service",
workspace=workspace,
synchronous=True)
# COMMAND ----------
# MAGIC %md
# MAGIC ##### Score Using Web Service URI
# COMMAND ----------
# Create input data for the API
sample_json = {
"columns": [
"Sensor1",
"Sensor2",
"Sensor3",
"Sensor4"
],
"data": [
[65.7845, 16613.676, 101.69767, 60.329124]
]
}
print(sample_json)
# COMMAND ----------
##Get the Web Service URI
uri = azure_service.scoring_uri
# COMMAND ----------
import requests
import json
# Function for calling the API
def service_query(input_data):
response = requests.post(
url=uri, data=json.dumps(input_data),
headers={"Content-type": "application/json"})
prediction = response.text
print(prediction)
return prediction
# API Call
service_query(sample_json)
# COMMAND ----------