قالب وردپرس درنا توس
Home / Tips and Tricks / Build machine learning recommendations for your app with AWS Personalize – CloudSavvy IT

Build machine learning recommendations for your app with AWS Personalize – CloudSavvy IT


aws personalize

AWS Personalize is a product recommendation engine that works much like the one used for Amazon.com, built from over 20 years of personalization experience. You can implement it as your own API to drive the app’s suggestions with machine learning.

What is AWS Personalize?

“Product recommendation engine” is a general term that can be extended to apply to many things, not just online shopping. Take YouTube for example. If you sign up for a new YouTube account, you get a lot of general videos that appeal to a mass audience ̵

1; things that are trending, for the most part. But if you search for “minecraft letsplay” and watch a half-hour video, the YouTube recommendation algorithm will notice this. It will look at tags, title, channel, release date and other metadata from the video you liked, and then, using machine learning, will try to find other videos that are similar to it and had similar engagement from other users. You may get more videos from the same series, as people tend to watch things in chronological order. Maybe there is another channel that does similar content that you might also like.

All of this is driven by machine learning recommendations. The “product” can be anything – in the case of Amazon, it is the items they have for sale. For YouTube, these are videos. For Spotify, there are new songs to play. For Facebook (or any social media service, really) it’s posts from users.

This engine is packaged in a stand-alone PaaS that does not require any specific machine learning knowledge. You feed the engine user actions (clicked on this post, listened to this song for X minutes, etc.) and the engine will spit out new recommendations from your product catalog upon request. Recommendations can start a bit patchy, but when your model is sufficiently trained, they start to become very accurate.

Setting up AWS Customize

Each AWS Personalize project will have three datasets:

  • Users who track metadata about the users themselves
  • Object, which acts as a product catalog
  • Interactions that log interaction events between users and objects

The interaction list is the most important, as it tracks all events and serves as a basis for training the model. The Users and Objects lists contain additional data that helps the model create intelligent connections. For example, knowing the age of the user, Personalize may recommend different products to different age groups, based on their likelihood of being applicable.

The default option is to import historical data from a CSV file, but you can use the Event Tracker API to send real-time updates when you get everything up and running. However, you must have some training data to import – the import will fail if you have less than 1,000 entries in your interaction list. If you just want to test Personalize, you need to create some kind of dummy data that follows your schedule before proceeding with the import.

Go to the AWS Personalize Management Console to get started. Create a new dataset group, which acts as a single “App”. It will ask for a name:

new dataset group

Click next and you will automatically configure the interaction import. Give it a name (“interactions”) and define your schedule. This is in Apache Avro format and tells Personalize which fields each interaction (or product / user) has. For interactions, the most basic is a binding of USER_ID to PRODUCT_ID, which is used to look up users and products from the other tables (a many-to-many relationship link).

dataset schema

You then need to import data into Customize, from a CSV file in S3. First select or create a service role that can access this bucket. You must also attach the following bucket policy to the target sections to allow Personalize to access and replace it bucketname with the name of your bucket:

    "Version": "2012-10-17",
    "Id": "PersonalizeS3BucketAccessPolicy",
    "Statement": [
            "Sid": "PersonalizeS3BucketAccessPolicy",
            "Effect": "Allow",
            "Principal": {
                "Service": "personalize.amazonaws.com"
            "Action": [
            "Resource": [

Then you can paste in the path to the file:

import S3 file

Click Finish and you will be taken to the Data Setup panel where you will see that the interaction dataset is now configured. You must repeat this process twice more and create data sets for users and products. Everything probably takes a few minutes to import depending on the size of your data.

Once everything has been imported, you need to create a solution, which is a trained model based on your data, that can be used as a basis for campaigns that provide actual recommendations. Create one from the dashboard:

create new solution

Give it a name and choose the recipe you want to use to run the solution. You can select this manually, or you can select “AutoML”, which uses AWS’s HRNN to make predictions. If you are unsure, select AutoML.

solution configuration

Solutions will have several versions to facilitate their management. When you create the solution, the original version is also created.

When your solution version is complete with the initialization, you can create a campaign: basically an instantiated inference engine to get actual recommendations. It has a REST API endpoint that you can query and use from your application.

Create a new campaign on the “Campaigns” tab in the sidebar, give it a name and choose your solution. Once created, you should be able to test it from the AWS CLI:

aws personalize-rec get-recommendations --campaign-arn $CAMPAIGN_ARN 
--user-id $USER_ID --query "itemList[*].itemId"

This command retrieves the recommendations from your campaign for the specified user ID. If all goes well, you should see a list of item IDs recommended for the user.

To add real-time data to the solution, you must create an Event Tracker from the sidebar. This gives you a tracker ID that you can use to enter data.

example function

There are two ways to set this up: if you use AWS Amplify, the AWS web and mobile app backend framework, installation is simple and you just need to configure it from the Amplify console. If you are not, you will need to set up a Lambda function to process the data and send it to Personalize.

Source link