mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-23 20:00:06 -05:00
update samples from Release-57 as a part of SDK release
This commit is contained in:
Binary file not shown.
@@ -86,7 +86,37 @@
|
||||
"source": [
|
||||
"In this example, we will be using and registering two models. \n",
|
||||
"\n",
|
||||
"You wil need to have a `first_model.pkl` file and `second_model.pkl` file in the current directory. The below call registers the files as Models with the names `my_first_model` and `my_second_model` in the workspace."
|
||||
"First we will train two simple models on the [diabetes dataset](https://scikit-learn.org/stable/datasets/index.html#diabetes-dataset) included with scikit-learn, serializing them to files in the current directory."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import joblib\n",
|
||||
"import sklearn\n",
|
||||
"\n",
|
||||
"from sklearn.datasets import load_diabetes\n",
|
||||
"from sklearn.linear_model import BayesianRidge, Ridge\n",
|
||||
"\n",
|
||||
"x, y = load_diabetes(return_X_y=True)\n",
|
||||
"\n",
|
||||
"first_model = Ridge().fit(x, y)\n",
|
||||
"second_model = BayesianRidge().fit(x, y)\n",
|
||||
"\n",
|
||||
"joblib.dump(first_model, \"first_model.pkl\")\n",
|
||||
"joblib.dump(second_model, \"second_model.pkl\")\n",
|
||||
"\n",
|
||||
"print(\"Trained models using scikit-learn {}.\".format(sklearn.__version__))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now that we have our trained models locally, we will register them as Models with the names `my_first_model` and `my_second_model` in the workspace."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -102,12 +132,12 @@
|
||||
"from azureml.core.model import Model\n",
|
||||
"\n",
|
||||
"my_model_1 = Model.register(model_path=\"first_model.pkl\",\n",
|
||||
" model_name=\"my_first_model\",\n",
|
||||
" workspace=ws)\n",
|
||||
" model_name=\"my_first_model\",\n",
|
||||
" workspace=ws)\n",
|
||||
"\n",
|
||||
"my_model_2 = Model.register(model_path=\"second_model.pkl\",\n",
|
||||
" model_name=\"my_second_model\",\n",
|
||||
" workspace=ws)"
|
||||
" model_name=\"my_second_model\",\n",
|
||||
" workspace=ws)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -149,25 +179,24 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%writefile score.py\n",
|
||||
"import pickle\n",
|
||||
"import joblib\n",
|
||||
"import json\n",
|
||||
"import numpy as np\n",
|
||||
"from sklearn.externals import joblib\n",
|
||||
"from sklearn.linear_model import Ridge\n",
|
||||
"\n",
|
||||
"from azureml.core.model import Model\n",
|
||||
"\n",
|
||||
"def init():\n",
|
||||
" global model_1, model_2\n",
|
||||
" # note here \"my_first_model\" is the name of the model registered under the workspace\n",
|
||||
" # this call should return the path to the model.pkl file on the local disk.\n",
|
||||
" # Here \"my_first_model\" is the name of the model registered under the workspace.\n",
|
||||
" # This call will return the path to the .pkl file on the local disk.\n",
|
||||
" model_1_path = Model.get_model_path(model_name='my_first_model')\n",
|
||||
" model_2_path = Model.get_model_path(model_name='my_second_model')\n",
|
||||
" \n",
|
||||
" # deserialize the model files back into a sklearn model\n",
|
||||
" # Deserialize the model files back into scikit-learn models.\n",
|
||||
" model_1 = joblib.load(model_1_path)\n",
|
||||
" model_2 = joblib.load(model_2_path)\n",
|
||||
"\n",
|
||||
"# note you can pass in multiple rows for scoring\n",
|
||||
"# Note you can pass in multiple rows for scoring.\n",
|
||||
"def run(raw_data):\n",
|
||||
" try:\n",
|
||||
" data = json.loads(raw_data)['data']\n",
|
||||
@@ -177,7 +206,7 @@
|
||||
" result_1 = model_1.predict(data)\n",
|
||||
" result_2 = model_2.predict(data)\n",
|
||||
"\n",
|
||||
" # you can return any data type as long as it is JSON-serializable\n",
|
||||
" # You can return any JSON-serializable value.\n",
|
||||
" return {\"prediction1\": result_1.tolist(), \"prediction2\": result_2.tolist()}\n",
|
||||
" except Exception as e:\n",
|
||||
" result = str(e)\n",
|
||||
@@ -208,10 +237,10 @@
|
||||
"source": [
|
||||
"from azureml.core import Environment\n",
|
||||
"\n",
|
||||
"env = Environment.from_conda_specification(name='deploytocloudenv', file_path='myenv.yml')\n",
|
||||
"\n",
|
||||
"# This is optional at this point\n",
|
||||
"# env.register(workspace=ws)"
|
||||
"env = Environment(\"deploytocloudenv\")\n",
|
||||
"env.python.conda_dependencies.add_pip_package(\"joblib\")\n",
|
||||
"env.python.conda_dependencies.add_pip_package(\"numpy\")\n",
|
||||
"env.python.conda_dependencies.add_pip_package(\"scikit-learn=={}\".format(sklearn.__version__))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -281,25 +310,15 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from azureml.core.webservice import AciWebservice, Webservice\n",
|
||||
"from azureml.exceptions import WebserviceException\n",
|
||||
"from azureml.core.webservice import AciWebservice\n",
|
||||
"\n",
|
||||
"aci_service_name = \"aciservice-multimodel\"\n",
|
||||
"\n",
|
||||
"deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)\n",
|
||||
"aci_service_name = 'aciservice-multimodel'\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" # if you want to get existing service below is the command\n",
|
||||
" # since aci name needs to be unique in subscription deleting existing aci if any\n",
|
||||
" # we use aci_service_name to create azure aci\n",
|
||||
" service = Webservice(ws, name=aci_service_name)\n",
|
||||
" if service:\n",
|
||||
" service.delete()\n",
|
||||
"except WebserviceException as e:\n",
|
||||
" print()\n",
|
||||
"\n",
|
||||
"service = Model.deploy(ws, aci_service_name, [my_model_1, my_model_2], inference_config, deployment_config)\n",
|
||||
"\n",
|
||||
"service = Model.deploy(ws, aci_service_name, [my_model_1, my_model_2], inference_config, deployment_config, overwrite=True)\n",
|
||||
"service.wait_for_deployment(True)\n",
|
||||
"\n",
|
||||
"print(service.state)"
|
||||
]
|
||||
},
|
||||
@@ -317,13 +336,11 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"test_sample = json.dumps({'data': [\n",
|
||||
" [1,2,3,4,5,6,7,8,9,10], \n",
|
||||
" [10,9,8,7,6,5,4,3,2,1]\n",
|
||||
"]})\n",
|
||||
"\n",
|
||||
"test_sample_encoded = bytes(test_sample, encoding='utf8')\n",
|
||||
"prediction = service.run(input_data=test_sample_encoded)\n",
|
||||
"test_sample = json.dumps({'data': x[0:2].tolist()})\n",
|
||||
"\n",
|
||||
"prediction = service.run(test_sample)\n",
|
||||
"\n",
|
||||
"print(prediction)"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -2,3 +2,5 @@ name: multi-model-register-and-deploy
|
||||
dependencies:
|
||||
- pip:
|
||||
- azureml-sdk
|
||||
- numpy
|
||||
- scikit-learn
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
name: project_environment
|
||||
dependencies:
|
||||
- python=3.6.2
|
||||
- pip:
|
||||
- azureml-defaults
|
||||
- scikit-learn
|
||||
- numpy
|
||||
- inference-schema[numpy-support]
|
||||
Binary file not shown.
Reference in New Issue
Block a user