Notebooks
A
Amazon Web Services
Recommender System With Implicit Bpr.Ipynb

Recommender System With Implicit Bpr.Ipynb

data-scienceinferencearchivedamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningimplicit_bprsagemakerjupyter-notebooktrainingmlops

Implementing a Recommender System for Implicit Feedback Datasets with Bayesian Personalized Ranking


This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable



Introduction of recommender systems:

A Recommender system is an automated form of software mechanism that leverages the algorithms and data to improve the discovery of the products or items that are most relevant to a particular user. Their task is not only to reveal to users the items they are looking for but also personalize their experience based on various techniques, such as:

  1. Leveraging past interactions/purchases/likes of this user.
  2. A recommendation based on other user's similarity to this user and what they liked/purchased.
  3. A recommendation based on their demographic data.

These days, recommender systems are employed in diverse domains to promote items such as products on e-commerce sites like Amazon.com, movie recommendations on Netflix, music/artist/album recommendations, or even on social media.

Here is an example of a recommendation:

image


Classes of recommender systems:

  1. Content-based recommender systems: Rely on the product's features, attributes, and descriptions to recommend other products that are similar to their past purchases or any form of explicit feedback data.
  2. Personalized Ranking-based recommender systems: Recommend the top-n items for a particular user along with a ranking and a score.
  3. Collaborating Filtering-based recommender systems: Rely on the user's prior item interactions or ratings to make a recommendation
    1. User-based: Operate by discovering the other identical/like-minded users.
    2. Item-based: Works based on the similarity between items assessed using user's ratings of those items/ interactions.
  4. Location/Demographics: The user's demographic knowledge is applied to acquire a classifier that can outline particular demographics to ratings or purchasing capacities.
  5. Hybrid: A blend of more than one of the above strategies.

Explicit vs. Implicit Users Feedback:

  1. Explicit Feedback: A dataset collected primarily based on the user's behavior or explicitly posted by the user in the system, so-called explicit feedback. Examples include movie ratings on Netflix, provided explicitly by the users, or ratings of products by users on Amazon.com.
  2. Implicit Feedback: Rather than relying on explicit user feedback, the system can indirectly utilize user behavior and interactions to learn about their interests and choices. This implicit feedback is handy in many different domains. For instance, the system may see a user purchasing or browsing an item as an endorsement for that item or even the number of times they played a particular song.

Bayesian Personalized Ranking (BPR) and its significance:

Bayesian Personalized Ranking (BPR) is an implicit feedback algorithm optimization technique that deals with one-class collaborative filtering problem by transforming them into a ranking task. The general strategy for item recommenders is to predict a personalized score for an item revealing its inclination. Then the items are arranged by ordering them according to that score.

Typically with the traditional recommender system, as the name suggests, the critical function is to recommend items to the users; however, when all those recommended items are alike, it raises the user's uncertainty. While employing the BPR algorithm, the recommendation comprises different types and their relevance score based on the implicit dataset. This increases the chances of the user liking at least one of those items. This increases the opportunities of the user getting recommendations containing a diverse selection of items. It also positively influences the user experience, which interprets into more challenging to measure metrics such as customer satisfaction and retention.

Table of contents


About this Notebook

  1. Pre-requisites: This example notebook requires a subscription to Implicit BPR at AWS Marketplace listing which represents an implementation of an algorithm utilizing Bayesian Personalized Ranking.

  2. You will demonstrate how to employ an Implicit BPR Algorithm by using Amazon SagaMaker to collect, analyze, clean, prepare, train and deploy the model to perform both the Batch and Real-time Inferences on the Online Retail Data Set.

  3. This Online Retail Data Set holds all the transactions occurring for a UK-based and registered, non-store online retail between 01/12/2009 and 09/12/2011. The company mainly sells unique all-occasion gift-ware and many customers of the company are wholesalers.

  4. Inside the AWS SageMaker Studio, start the "Data Science" kernel, which powers all of our notebook interactions

    1. Click on "No Kernel" in the Upper Right
      image

    2. Select the "Data Science Kernel"
      image

    3. Confirm the Kernel is Started in Upper Right
      image

  5. If you want to run this notebook on AWS SageMaker Notebook Instances

    1. Please use Classic Jupyter mode to be able correctly to render visualization. Pick instance type 'ml.c5.2xlarge' or larger.
      image

    2. Set kernel to 'conda_python3'
      image

  6. You can run this notebook one cell at a time (By using Shift+Enter for running a cell) OR run all the cells at once by selecting "Run All Cells"
    image

Note: You cannot continue until the kernel is started. Please wait until the kernel is started before continuing !!!!


Step 1: Pre-requisites: subscribe to Implicit BPR Algorithm from AWS Marketplace

  1. Open Implicit BPR listing from AWS Marketplace in your browser.
  2. Read the Highlights section and then the product overview section of the listing.
  3. View usage information and then additional resources.
  4. Note the supported instance types and specify the same in the following cell.
  5. Next, click on "Continue to Subscribe". You will now see the "Subscribe to this software" page.
  6. Review End user license agreement, support terms, as well as pricing information.
  7. Next, "Accept Offer" button needs to be clicked only if your organization agrees with EULA, pricing information as well as support terms. Once Accept Offer button has been clicked, specify compatible training and inference types you wish to use.
  8. Once you click on "Continue to Configuration" button and then choose a region, you will see a Product Arn displayed. This is the algorithm ARN that you need to specify in the following cell

Notes:

  1. If the continuous configuration button is active, your account already has a subscription to this listing.
  2. Once you click on Continue to configuration button and then choose region, you will see that a product ARN will appear. This is the algorithm ARN that you need to specify in your training job.
[ ]

Step 2: Set up environment

[ ]
[ ]
[ ]
[ ]

Let us define utility functions we can use later to print the purchase information in human readable format.

[ ]

Step 3: Data collection and preparation

The Online Retail Data Set you will use is provided by UCI Machine Learning. The dataset contains all the transactions occurring for a UK-based and registered, non-store online retail from 01/12/2009 to 09/12/2011. The company sells unique all-occasion gift-ware. Many customers of the company are wholesalers.

Facts regarding the downloaded dataset:

  1. The spreadsheet holds two separate sheets inside it.
  2. The first one holds the transaction between the year 2009-2010, and the other has the data between 2010-2011.
  3. Individual sheet comprises more than 500k instances, so combine, we would have approximately 1067371 representations to explore and prepare for our use case
  4. Dataset Attribute Information:
    1. CustomerID (Nominal): Customer number. A 5-digit integral number uniquely assigned to each customer.
    2. StockCode (Nominal): Product (item) code. A 5-digit integral number uniquely assigned to each distinct product.
    3. Description (Nominal): Product (item) name.
    4. Price (Numeric): Unit price. Product price per unit in sterling (£).
    5. Quantity (Numeric): The quantities of each product (item) per transaction.
    6. Invoice (Nominal): Invoice number. A 6-digit integral number uniquely assigned to each transaction. If this code starts with the letter 'c', it indicates a cancellation.
    7. InvoiceDate (Numeric): Invoice date and time. The day and time when it generated a transaction.
    8. Country (Nominal): Country name. The name of the country where a customer resides.

Step 3.1: Ingesting the Online Retail Dataset and load them inside pandas dataframe

[ ]

Next, you will examine the downloaded datasets into the pandas dataframe and uncover the top 5 records and their datatypes. The estimated wall time for the below cell is around ~2min 20s.

[ ]

As you can see, the data set has 1067371 instances and eight different features.

Step 3.2: Exploring, cleansing and converting dataset into the format accepted by an algorithm

The user-item-interaction data is critical for getting started with the recommender system and its training for many use-cases such as Video-on-demand applications, user click-stream logs, user's purchase history, etc. No matter the use case, the algorithms all share a base of learning on user-item-interaction data, which is defined by two core attributes:

1. user_id - The user who interacted
2. item_id - The item the user interacted with

The Implicit BPR Algorithm requires the training dataset to contain 'user_id' and 'item_id' columns. In this case, it would be CustomerID, and the items they have purchased/interacted with would StockCode respectively. Additionally, they must not include any missing values, and the input file must be in a CSV format.

image

Let us evaluate and confirm that our dataset for nulls.

[ ]

You can see that the dataset has close to 243007 missing values in the CustomerID column. Next, you will clean up the dataset by eliminating those missing/null rows.

[ ]

Step 3.3: Preparing the final training dataset and upload it to Amazon S3

To better visualize our model's recommendation, let us assemble a product lookup table that we can use later to map with the inference results. We can also eliminate the optional columns which are not needed for the training. Finally, lets rename the columns "CustomerID" -> "user_id" and "StockCode" -> "item_id" as expected by the algorithm spec.

[ ]

Next, let us split the dataset into the training and the testing that you can use to train and evaluate the performance of the model

[ ]

Let us create a CSV file for both the training and the testing dataset and upload them to the S3 bucket

[ ]
[ ]

Congratulations. You have performed the ingestion, exploration, and generation of a clean training dataset file that meets the requirement. You have also uploaded it to the S3 bucket, and can be used for training a model.


Step 4: Train the model and evaluate the performance metrics

Step 4.1: Train the model

To train a model, you create a training job. After you start the training job, SageMaker launches the ML compute instances and uses the training code you provided to train the model. It then saves the resulting model artifacts and other output in the S3 bucket.

Next, let us form and start a training job with the training dataset we uploaded to the S3 bucket and wait for the completion. The estimated wall time for the below cell is around ~4min 20s.

[ ]

Step 4.2: Evaluate and visualize the performance metrics

As you know, you are most likely interested in promoting top-N items to the users within the context of recommendation systems. So it is essential to measure the Precision and Recall metrics in the top-N items rather than all the items. Thus the idea of precision and recall at k where k is a user-defined integer to match the top-N recommendations' objective.

In other words, out of all the top N items the system would recommend, how many are relevant to the user? You can visualize the metrics p@k(10) produced from the training job inline using the Amazon SageMaker Python SDK APIs from the next cell.

[ ]

You can see from the training job logs that the algorithm could produce the Precision at 10 in a top-10 recommendation problem is ~83%. It means that ~83% of the recommendation the system made are relevant to the user.


Step 5: Perform a batch/offline inference

Batch Transform: To get the inferences on an entire dataset offline, you run a batch transform job on a trained model. Batch transform automatically manages the processing of large datasets within the limits of specified parameters. When a batch transform job starts, SageMaker initializes ML compute instances and distributes the inference or preprocessing workload between them. Batch Transform partitions the Amazon S3 objects in the input by key and maps Amazon S3 objects to instances.

In this step, you will first identify sample users and prepare an input payload. Then you will run a batch transform job, and finally, you will look at the recommendations for the sample users.

Step 5.1: Identify a customer and understand their purchase history

Let us identify sample customers who have purchased three separate items of various kinds.

[ ]

As you can see, this customer likes purchasing many different kinds of Lights, Doormats, various Bowls. Let us build a request payload for this customer and examine what new items the deployed model would recommend?

[ ]
[ ]

Step 5.2: Upload the payload to Amazon S3 and run a batch transform job

In this section, you will upload the data to S3 and run a batch transform job. The estimated wall time for the transform job is around ~6min 30s.

[ ]
[ ]

Next, lets us examine the Batch Transform output in S3.

[ ]

As you can see, the inference output not only includes the User Id, Item Id that it would recommend but also consists of the Ranking Score in an order which is most relevant to this user.

Step 5.3: Join the result with the stock lookup table to associate the item information

[ ]

You can see that the model correctly predicted the next few articles that this customer would choose to like to purchase, such as different additional Lights and Bathroom Curtain Sets, including other items Cups.


Step 6: Deploy the model and perform a real-time inference

Step 6.1: Deploy an endpoint

The estimator.deploy method creates the deployable model, configures the SageMaker hosting services endpoint, and launches the endpoint to host the model. The estimated wall time for the below cell is around ~7min 25s.

[ ]

Step 6.2: Take the example user, create the JSON payload and make an inference request

Let’s take another customer for our analysis from the original dataset and make an online inference request. You will be able to see the JSON response received from SageMaker Model Endpoint.

[ ]

As you can see, this customer prefers purchasing events related decorative items such as Paper dollies, Banners, and Assorted items. Let us build a request payload for this customer and review what new things the deployed model would recommend?

[ ]

Step 6.3: Join the result with the stock lookup table to associate the item information

[ ]

You can see that the model correctly predicted the next few articles that this customer would choose to like to purchase, such as Hanging tags, Cake stands, Garland including, Gift Tags, and various Birthday signs.

Step 7: Cleaning up the Resources

To avoid incurring unnecessary costs, delete the resources you created, such as deployed Amazon SageMaker Model endpoint and the deployed model, downloaded external datasets, and temporary ones made on this Notebook.

[ ]
[ ]

Unsubscribe the product from AWS Marketplace (optional)

Lastly, if the AWS Marketplace subscription was created just for the experiment and you would like to unsubscribe to the product, you can follow the following steps. Before you cancel the subscription, ensure that you do not have any deployable model created from the model package using the algorithm. Note - You can find this information by looking at the container name associated with the model.

  1. Navigate to Machine Learning tab on Your Software subscriptions page
  2. Locate the listing that you would need to cancel the subscription for, and then you can click** Cancel subscription** to cancel the subscription

Notebook CI Test Results

This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.

This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable