Consuming and Publishing Models on Azure Marketplace
The Azure Machine Learning Studio makes it easy to create new predictive models. But what if there was a way to harness the power of machine learning without having to understand the data science behind it? This is the promise of the Azure Machine Learning Marketplace. Think of it as the place where you can go find “baked” APIs that solve interesting problems thanks to machine learning. This chapter will introduce this marketplace, showing existing solutions from Microsoft and its partners. We will also show step by step how you can sell your own predictive models on Azure Marketplace.
What Are Machine Learning APIs?
The Machine Learning APIs enable developers or organizations to consume machine learning solutions instead of creating them. This is particularly important since the supply of good data scientists is very low at the moment. With these APIs, organizations can leverage the power of machine learning without hiring data scientists. The marketplace also allows data scientists to amplify their reach by selling their solutions to many more customers.
These APIs are exposed to the public in several locations today. They are exposed in the Azure Machine Learning Gallery, as shown in Figure 13-1. They can also be found in the Microsoft Azure Marketplace. The Microsoft Azure Marketplace is an online store for thousands of applications, add-ons, APIs, and data that are preconfigured to run on Windows Azure. Azure Marketplace does not sell only Microsoft products; it also has offerings from many vendors, including competitors such as Oracle, Box, MungoBD, Cloudera, and others. The store has many categories of products for virtual machines, application services, web applications, and many more. In this book, we will only focus on machine learning APIs.
Figure 13-1. Machine Learning APIs showcased at http://gallery.azureml.net
As you can see in Figure 13-1, there are many ready-to-consume APIs, including the following:
How to Use an API from Azure Marketplace
In this section, you will see how to use machine learning models from the Azure Marketplace. Specifically, you will see how to use the Recommendations API that was introduced in the previous section. We will demonstrate the Recommendations API with a sample application that is available on Azure Marketplace.
The goal of the Recommendations API is to generate a model that will use prior transaction data to recommend items that are of interest to customers. For instance, the recommendations model should be able to suggest to your customers which items they are most likely to want to purchase once they visit a particular product page. It can also recommend to the customer which other products they may want to visit, as shown in Figure 13-2.
Figure 13-2. Sample Usage of the Recommendations API
To provide a concrete example from retail, the Recommendations API will allow you create a Customized Recommendations Model where, given an item (or a set of items) that the customer is interested in, it will predict which other products the customer is likely to want to purchase.
As outlined in Figure 13-3, in order to generate this predictive model, the Recommendations API needs you to provide two pieces of data:
<Item Id>,<Item Name>,<Item Category>[,<Description>]
<User Id>,<Item Id>[,<Time>,<Event>]
Figure 13-3. Recommendations API Diagram
Once this data is fed into the Recommendations API, it can create your customized recommendations model. Figure 13-3 shows the outline of the recommendation API.
You can obtain the sample application from the Recommendations API page as follows:
Note To run the sample application, you must first sign up for the Recommendations API. To do this, go to the Recommendations API page at https://datamarket.azure.com/dataset/amla/recommendations. Sign up for the $0 subscription that provides up to 10,000 transactions per month for free!
Second, you also need an integrated development environment (IDE) for C# code. We use Visual Studio in this chapter.
After extracting the files from the sample application ZIP file, you can open the project file named AzureMLRecoSampleApp.csproj in Visual Studio or your preferred IDE. Follow these steps to build and test a recommender with this sample application.
Figure 13-4. Illustrating the sample application in Visual Studio
Figure 13-5. Output of the application while the recommendation model is being built
Figure 13-6. Output of the application showing recommendations from the recommendation model
Now let’s see how the sample application works since it shows how you can call the Recommender API from your own C# application. Lines 32 to 47 in the Main() function initialize the SampleApp application with your username and account key. On line 51, the application creates a new container for the recommendation model with the following code:
var modelId = sage.CreateModel(modelName);
Lines 55 to 63 load the catalog and user data with the following code:
Console.WriteLine(" Importing catalog and usage data...");
var resourcesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"Resources");
Console.WriteLine(" import catalog...");
var report =
sage.ImportFile(modelId, Path.Combine(resourcesDir,"catalog_small.txt"), Uris.ImportCatalog);
Console.WriteLine(" {0}", report);
Console.WriteLine(" import usage...");
report = sage.ImportFile(modelId, Path.Combine(resourcesDir, "usage_small.txt"), ris.ImportUsage);
Console.WriteLine(" {0}", report);
On line 68 the application triggers a build to create a recommendation model with the following code:
var buildId = sage.BuildModel(modelId,"build of " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"));
Full details of the BuildModel function are on lines 198 to 232. The while loop on lines 76 to 90 checks the status of the model building. It checks the build status every 5 seconds until the job completes. Figure 13-5 shows the output of the application on the console.
When the model is built successfully, you can start testing with a few examples. In this case, the sample application tests the model with two examples from the catalog file (namely the books Clara Callan and Restraint of Beasts). On line 120 the application uses the following function call to get recommendations based on a single item:
sage.InvokeRecommendations(modelId, seedItems,false);
In this case, the sample application asks for a recommendation based on the book Clara Callan. In response, the model recommends two books, Spadework and Restraint of Beasts.
Line 125 also calls the InvokeRecommendations function. This time it passes true as the last function parameter. Setting this last argument to true allows you to get recommendations using many items as the seed. In this case, the sample application uses the books Clara Callan and Restraint of Beasts as the seed. For this set of items, the recommendation model returns one item, the book Spadework. The results are shown in the output listing in Figure 13-6.
sage.InvokeRecommendations(modelId, seedItems, true);
More details of the function InvokeRecommendations are available in lines 366 to 390 of the sample application. It is worth noting that you cannot build the sample application more than once with the same model name. On line 43 the model name is set to the constant string "demo_model". To rerun the sample application you need to change the name of the model in line 43; otherwise an exception will be thrown.
const string modelName = "demo_model";
Publishing Your Own Models in Azure Marketplace
In this section, you will see how to publish machine learning models in Azure Marketplace using the predictive maintenance model from Chapter 11 as an example. You can publish your models in Azure Marketplace with this three-step process.
Your submitted API will be reviewed by Microsoft before publication. The approval will take a few business days. Let’s see how to implement each of the these three steps in more detail.
Creating and Publishing a Web Service for Your Machine Learning Model
In this example, you will use the predictive maintenance model we discussed in Chapter 11. The following two steps are required to deploy your model in production. With these steps you basically create a web service that can be called as an API.
Let’s review these steps in detail and see how they apply to your finished model built in Chapter 11. You can start this section by opening the predictive maintenance model in Azure Machine Learning Studio.
Note The model we will discuss in this section is published as Predictive Maintenance Model in the Azure Machine Learning Gallery. You can access the Gallery at http://gallery.azureml.net/. Feel free to download this experiment to your workspace in Azure Machine Learning.
To create a scoring experiment, follow these steps in Azure Machine Learning Studio.
Figure 13-7. The scoring experiment ready for deployment as a web service
Publishing Your Experiment as a Web Service
At this point, your model is now ready to be deployed as a web service. To do so, follow these steps.
Figure 13-8. The results of the deployed model showing the API key and URLs for the service
Obtaining the API Key and the Details of the OData Endpoint
The dashboard in Figure 13-8 shows the API key for your new web service. You will need this to deploy your model in the marketplace. Next, you will need to get the URL of your OData endpoint. To obtain this from the same dashboard, click the first link named API help page for the Request/response service. This will open the page shown in Figure 13-9. Copy the OData endpoint address.
Figure 13-9. API documentation of the deployed predictive model
Publishing Your Model as an API in Azure Marketplace
Having deployed your model as a web service, you are ready to publish it to Azure Marketplace. To do this simply follow these steps.
Figure 13-10. Creating a new data service in Azure Marketplace
Figure 13-11. Completed form for Data Service tab
Congratulations, you have just published your first predictive model in Azure Marketplace. Once approved, you can start earning money from your own predictive model.
Summary
As you saw in this chapter, Azure Marketplace is a great opportunity for you to make money from your predictive models. Azure Marketplace is an online site for buying and selling data and Machine Learning solutions. This chapter introduced this marketplace, showing existing Machine Learning APIs from Microsoft and its partners. You saw how to consume services from Azure Marketplace. Specifically, you reviewed the Recommendations API, a potent recommendation engine available as an API service from Azure Marketplace. You also saw step by step how to sell your own predictive models on Azure Marketplace.
18.191.168.203