mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-20 01:27:06 -05:00
Compare commits
8 Commits
azureml-sd
...
cli
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebd8d7c98a | ||
|
|
cc34a28873 | ||
|
|
302133852c | ||
|
|
9c2ab1826c | ||
|
|
e5aaf4be0a | ||
|
|
f6f35767d4 | ||
|
|
12f0306f3f | ||
|
|
2817a77b69 |
15
cli/myenv.yml
Normal file
15
cli/myenv.yml
Normal 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
4
cli/readme.md
Normal 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
24
cli/score.py
Normal 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()})
|
||||||
BIN
cli/sklearn_regression_model.pkl
Normal file
BIN
cli/sklearn_regression_model.pkl
Normal file
Binary file not shown.
44
cli/train.py
Normal file
44
cli/train.py
Normal 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))
|
||||||
Reference in New Issue
Block a user