Building the image of the model

To build the image of the model, we need to write two files: the myenv.yml file, and the core.py file, which contains the code of our model. The myenv.yml file contains the dependencies and can be easily written using the Azure ML service. From the Jupyter Notebook, we can run the following code:

from azureml.core.conda_dependencies import CondaDependencies

myenv = CondaDependencies()
#myenv.add_conda_package("keras")

with open("myenv.yml","w") as f:
f.write(myenv.serialize_to_string())

The code of our model should contain two methods: the init() method, and the run() method. In the init() method, the model is loaded and prepared. In our exercise, this simply involves assigning the wind turbine formula to a global variable. The run() method is called when we need to run the model. During this method, we expect the data to be passed as a JSON and returned as a JSON. In our exercise, we parse the JSON file and call the wind turbine model.

The following code shows the core.py file:

import json
import numpy as np
import os
import pickle

from azureml.core.model import Model

def wind_turbine_model(x):

# cut-in speed vs cut-out speed
if x<4.5 or x>21.5:
return 0.0

# standard operability
return 376.936 - 195.8161*x + 33.75734*x**2 - 2.212492*x**3 +
0.06309095*x**4 - 0.0006533647*x**5

def init():
global model
# not model for wind turbine
model = wind_turbine_model

def run(raw_data):
data = np.array(json.loads(raw_data)['data'])
# make evaluation
y = model(data)
# return the JSON
return json.dumps(y)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset