Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 12366

ML 04 – Develop and Consume Azure ML Experiment as a Web Service

$
0
0

This post will answer:

  • How to develop simplest Azure Machine Learning (AML) experiment?
  • How to publish AML Experiment as a scalable Web Service?
  • How to consume the Web Service from an R/Python script?

How to develop simplest Azure Machine Learning (AML) experiment?

In our initial post on ML series, using the R studio, we have developed a sample ML solution with linear R model. In this post we will develop the same solution completely in AML with minimum effort.

Referring to our previous post, create a new "blank experiment" and add (drag/drop) the following modules on the empty canvas:

  • "Enter Data Manually": used to enter training data
  • "Linear Regression": Linear Regression Model
  • "Train Model": Module to train the Model with provided data

Below is the screen shot from the canvas with the above modules connected to each other. To connect the modules with each other, just press the mouse left button on the modules output port, and drag/drop it on to the input port of the other module. You will see a directed arrow will be drawn. Referring to the below screenshot, if you can't find the above modules, just type their names inside the "Search experiment items" search box, it will be filtered out for you.

On the right side of the window you will see a child window (like the toolbox) with the caption "Properties". One by one, clicking on (making it selected) each of the three module, you can see corresponding properties of that module in the properties window. Starting from the first module "Enter Data Manually", we will update their properties.

First we will generate a training dataset. Using the same method mentioned in previous post, with an Excel sheet generate data series with random noise. Plotting this data should look like a noisy line with slope = 1 as shown below. This data consists of two columns: X and Y. For each value in X column you will see the corresponding Y value. You should observe that almost all Y values are (+/- 50) same to the X value.

Aim is to develop an ML solution to predict corresponding Y value of any X value which does not exists in our training dataset.

 How to generate training data video:

(Please visit the site to view this video) 

 

Module

Properties

Enter Data Manually

In previous step, we generated data for training and Copy it to the clipboard. Now Paste this data into the properties window of the "Enter Data Manually" module. Don't forget to select "HasHeaders" checkbox.

Linear Regression

Keep the default parameters of the "Linear Regression" module as it is.

Train Model

Click on the "Launch Column selector" button in the properties window of Train Model. Select or Type Y as the label column name.

 

After settings the properties of the modules, now it is time to train our model by pressing the run button. On the bottom of the page.

It will take around a minute to run the solution and after a successful run you will see green checkmarks on the right side of each module.

How to publish AML Experiment as a scalable Web Service?

How will we use this ML solution? Will it take ~1 minute each time to train the model for such small amount of data for training? No, we don't need to train our model every time. Once it is trained, if there is no major change in your training dataset, then you can keep the trained model as it is. To be able to use this trained model, you need to publish it as a web service. To do so, first click on the "Set up web Service" button near the "RUN" button. In the menu windows, select "Create Predictive Experiment". With this command it will create a new experiment by adding "Web Service Input" and "Web Service Output" modules.

 

I also made small modification by manually adding two "Project Columns" modules. You can omit them, there will not be a problem in your solution. Reason for putting these two modules is to filter out the Y column in the training dataset. As we already trained our model, we don't need Y column anymore, and we don't want it to be visible in our new web service as an input parameter. Because we want to predict this value, so there is no need to provide it as an input parameter. Same for the output. We filter out the output data by just allowing "Scored values" – predicted values to be visible in the web service output.

Now you have to "Run" this experiment once again to be able to publish it as a web service. After a successful run, you will see "Deploy Web Service" button near the "RUN" button, press it. After few seconds your web service will be ready.

Press the "TEST" button which will pop out an input parameters dialog box. Type any "X" value that you want to predict its corresponding "Y" value. i.e. 3578, which is not in our training set.

After few seconds you will see the output of this web service on the bottom of the page as:

 

How to consume the Web Service from an R script?

Just click on the "Response/Request" link near the "TEST" button. It will open a new page with sample codes. Copy/Paste the R code into a new page in RStudio.

In this sample code you have to update 2 lines. One is the key (password) to access the web service that we created. Copy the key in the web service management page:

And type this key into the following line by deleting the "abc123"

api_key = "abc123" # Replace this with the API key for the web service

Also you should update the web service input parameters. Following line

"Values" = list( list( "0" ), list( "0" ) )

with

"Values" = list( list( "3578" ), list( "6000" ) )

In this sample two different input values provided, 3578 and 6000. You can provide as much as you want by putting comma between them.

 

If you have just installed the RStudio (we mentioned how to install it in the first ML post) on your machine, then you will get two error message when you run the sample code:

  1. Error in library("rjson") : there is no package called 'rjson'
  2. Error in library("RCurl") : there is no package called 'RCurl'

Our sample code uses these packages and they are not installed to RStudio by default. You can install them from Tools->Install Packages menu.

Just type the missing package name in the text box and press install. Do it both for "RCurl" and "RJson"

After all, you can press CTRL+ALT+R button combination to run the R code. In the console window you will see the output of the web service with predicted values of the input Xs.

$Results$output1$value$Values

[1] "3606.16504920019" "6030.31943595806"


Viewing all articles
Browse latest Browse all 12366

Trending Articles