Compare commits

...

8 Commits
master ... cli

Author SHA1 Message Date
Jordan Edwards
ebd8d7c98a Update myenv.yml 2018-09-24 19:43:51 -07:00
Jordan Edwards
cc34a28873 Update myenv.yml 2018-09-24 19:43:39 -07:00
Jordan Edwards
302133852c Update myenv.yml 2018-09-24 14:01:11 -07:00
Jordan Edwards
9c2ab1826c Add files via upload 2018-09-24 13:39:24 -07:00
Jordan Edwards
e5aaf4be0a Create train.py 2018-09-24 13:38:42 -07:00
Jordan Edwards
f6f35767d4 Create myenv.yml 2018-09-24 13:38:10 -07:00
Jordan Edwards
12f0306f3f Create score.py 2018-09-24 13:33:16 -07:00
Jordan Edwards
2817a77b69 Create readme.md
Creating a readme which outlines the content here for the CLI
2018-09-24 13:32:21 -07:00
5 changed files with 87 additions and 0 deletions

15
cli/myenv.yml Normal file
View File

@@ -0,0 +1,15 @@
name: project_environment
dependencies:
- python=3.6.2
- scikit-learn
- numpy
- pip:
- numpy==1.14.2
- pandas
- scipy==1.0.0
- scikit-learn==0.19.1
# Required packages for AzureML execution, history, and data preparation.
- --index-url https://azuremlsdktestpypi.azureedge.net/sdk-release/Preview/E7501C02541B433786111FE8E140CAA1
- --extra-index-url https://pypi.python.org/simple
- azureml-defaults

4
cli/readme.md Normal file
View File

@@ -0,0 +1,4 @@
# CLI Example Content
This content can be used in conjunction with our [CLI reference guide.](https://docs.microsoft.com/en-us/azure/machine-learning/service/reference-azure-machine-learning-cli)
Example content includes training scripts, conda environment files and scoring files.

24
cli/score.py Normal file
View File

@@ -0,0 +1,24 @@
import pickle
import json
import numpy
from sklearn.externals import joblib
from sklearn.linear_model import Ridge
from azureml.core.model import Model
def init():
global model
# note here "sklearn_regression_model.pkl" is the name of the model registered under
# this is a different behavior than before when the code is run locally, even though the code is the same.
model_path = Model.get_model_path('sklearn_regression_model.pkl')
# deserialize the model file back into a sklearn model
model = joblib.load(model_path)
# note you can pass in multiple rows for scoring
def run(raw_data):
try:
data = json.loads(raw_data)['data']
data = numpy.array(data)
result = model.predict(data)
except Exception as e:
result = str(e)
return json.dumps({"result": result.tolist()})

Binary file not shown.

44
cli/train.py Normal file
View File

@@ -0,0 +1,44 @@
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license.
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from azureml.core.run import Run
from sklearn.externals import joblib
import os
import numpy as np
os.makedirs('./outputs', exist_ok=True)
X, y = load_diabetes(return_X_y=True)
run = Run.get_submitted_run()
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=0)
data = {"train": {"X": X_train, "y": y_train},
"test": {"X": X_test, "y": y_test}}
# list of numbers from 0.0 to 1.0 with a 0.05 interval
alphas = np.arange(0.0, 1.0, 0.05)
for alpha in alphas:
# Use Ridge algorithm to create a regression model
reg = Ridge(alpha=alpha)
reg.fit(data["train"]["X"], data["train"]["y"])
preds = reg.predict(data["test"]["X"])
mse = mean_squared_error(preds, data["test"]["y"])
run.log('alpha', alpha)
run.log('mse', mse)
model_file_name = 'ridge_{0:.2f}.pkl'.format(alpha)
# save model in the outputs folder so it automatically get uploaded
with open(model_file_name, "wb") as file:
joblib.dump(value=reg, filename=os.path.join('./outputs/',
model_file_name))
print('alpha is {0:.2f}, and mse is {1:0.2f}'.format(alpha, mse))