Deploying Your First Web Service with Microsoft R Server

By | 2017-05-15T17:25:10+00:00 May 15th, 2017|Advanced Analytics, Data Science, Microsoft R Server|0 Comments

Incorporating advanced analytics in software allows developers to build intelligent applications tailored to the individual user. The easiest way to include machine learning in an application is by calling an API that returns a prediction. Using a standalone Microsoft R Server, organizations can write custom machine learning solutions that are hosted as web service APIs, either on-premises or in the cloud. Microsoft R Server users can utilize world-class machine learning algorithms that are built and supported by Microsoft, and practically eliminate memory constraints by enabling clusters of machines.

We will walk through the process of deploying a web service by publishing a basic logistic regression model. Here I will be writing and executing all the R code on my local machine, and then deploying from my machine to an operationalized single node R Server in Azure. If you wish to follow along it is easy to spin up an R Server image in the Azure Portal.

This walkthrough will demo part of Microsoft’s MicrosoftML R package to detect if two questions are logically the same. The dataset we will be using is provided to the public by Quora.

To begin, open an R IDE of choice, set the working directory, load the required packages, and import the data. Usually a data scientist would write code to split our entire dataset into training and test datasets, however, we have pre-split our data and saved the two datasets to Azure blob storage.

If we view our datasets we will notice there a few columns that we will not need for machine learning (id, qid1, and qid2).

Let’s remove these extra columns from our dataset.

Next we will take advantage of the MicrosoftML R library by using the featurizeText() transform. In this case, we want to remove stop words, remove punctuation, and create n-grams.

Now it is time to create a machine learning model! Since we are focusing on how to publish a web service to a Microsoft R Server, we will use the logistic regression algorithm that is available in MicrosoftML. After we create the model we score the test dataset, and save the logistic regression model as a RData file in our working directory. We save the logistic regression model so that we can later publish the model as a web service.

Additionally, we will want to evaluate our model prior to publishing it. Since this is a binary classification model the metrics below will give us a good idea of how well our model preformed.

We must provide R code when publishing a web service, in this case we will provide a function, called duplicateQuestion, that takes in two questions and scores them using our previously trained model. This function is executed each time the web service API is called.

Now that we have created a model, saved our model, and created a function we are ready to publish our web service. First we must log into the R Server using the remoteLogin() function in the mrsdeploy R package. Please note that here we use the R Server admin credentials, but there many ways to authenticate with the R Server (documentation can be found here).

Once logged into the R Server, we can now publish, update, delete, get, or list web services. Here we wish to publish a new web service called “MyFirstWebService”, and give it version “v1.0.0”. Notice, that we provide the model saved in our workspace, the function we created, and set the inputs and outputs of the web service.

Let’s test and see if the newly created web service works. Above, we assigned the web service to an R object called api therefore we can consume the web service inside our R IDE.

If we run the above code, we get the following output.

Microsoft R Server makes deploying machine learning R scripts easy. Microsoft R Server allows us to create specialized APIs that are easy to consume via a Swagger API Framework. We can run the following commands to generate a swagger.json file, which using AutoRest can generate authentication files needed to consume the web service in an application.

We have now successfully deployed a machine learning solution as a web service. Microsoft R Server allows organizations to develop, deploy, and consume world-class machine learning algorithms and solutions that can be incorporated into all types of applications or business workflows.


About the Author:

Leave A Comment