In this module, you create an Amazon Personalize solution. A solution refers to the combination of an Amazon Personalize recipe, customized parameters, and one or more solution versions (trained models). Once you create a solution with a solution version, you can create a campaign to deploy the solution version and get recommendations. To create a solution in Amazon Personalize, you do the following:

  1. Choose a recipe – A recipe is an Amazon Personalize term specifying an appropriate algorithm to train for a given use case. For more details, see Choosing a Recipe.
  2. Configure a solution – Customize solution parameters and recipe-specific hyperparameters so the model meets your specific business needs. For more details, see Configuring a Solution.
  3. Create a solution version (train a model) – Train the machine learning model Amazon Personalize will use to generate recommendations for your customers. For more details, see Creating a Solution Version.
  4. Evaluate the solution version – Use the metrics Amazon Personalize generates from the new solution version to evaluate the performance of the model. For more details, see Evaluating the Solution Version.

Time to Complete Module: 20 Minutes


  • Step 1. Choose a recipe

    Amazon Personalize provides three types of recipes, but in this tutorial, you only use the User_Personalization recipe.
     
    The User-Personalization (aws-user-personalization) recipe is optimized for all User_Personalization recommendation scenarios. When recommending items, this recipe uses automatic item exploration.
     
    With automatic exploration, Amazon Personalize automatically tests different item recommendations, learns from how users interact with these recommended items, and boosts recommendations for items that drive better engagement and conversion. This improves item discovery and engagement when the interaction data is updated frequently and there are new users and new items.

    You can balance how much to explore (where items with less interactions data or relevance are recommended more frequently) against how much to exploit (where recommendations are based on what we know or relevance). Amazon Personalize automatically adjusts future recommendations based on implicit user feedback.

    Run the following code to choose the User-Personalization recipe.

    # aws-user-personalization selected for demo purposes
    recipe_arn = "arn:aws:personalize:::recipe/aws-user-personalization"
  • Step 2. Configure the solution

    In this step, you create the solution (model), then configure it for model training. Configuring the solution allows you to customize the solution parameters and recipe-specific hyperparameters so the model meets your specific business needs.

    create_solution_response = personalize.create_solution(
        name = "personalize-demo-soln-user-personalization",
        datasetGroupArn = dataset_group_arn,
        recipeArn = recipe_arn
    )
    
    solution_arn = create_solution_response['solutionArn']
    print(json.dumps(create_solution_response, indent=2))
  • Step 3. Create a solution version (train a model)

    In Step 2, you defined a solution (model), now you can train the model. In Amazon Personalize, training the model is referred to as creating a solution version. You can think of each version as a trained model using the current datasets.
     
    To create the solution version, run the the following code block:
    create_solution_version_response = personalize.create_solution_version(
        solutionArn = solution_arn
    )
    
    solution_version_arn = create_solution_version_response['solutionVersionArn']
    print(json.dumps(create_solution_version_response, indent=2))
    To train the model, run the the following code block and wait for solution version to print an ACTIVE status. This step takes approximately 40-50 minutes.
    %%time
    max_time = time.time() + 3*60*60 # 3 hours
    while time.time() < max_time:
        describe_solution_version_response = personalize.describe_solution_version(
            solutionVersionArn = solution_version_arn
        )
        status = describe_solution_version_response["solutionVersion"]["status"]
        print("SolutionVersion: {}".format(status))
        
        if status == "ACTIVE" or status == "CREATE FAILED":
            break
            
        time.sleep(60)
  • Step 4. Evaluate the solution version

    When you create a solution version, Amazon Personalize generates metrics that you can use to evaluate the performance of the model before you create a campaign and provide recommendations. Metrics allow you to view the effects of modifying a solution's hyperparameters. You can also use metrics to compare the results between solutions that use the same training data but were created with different recipes.

    Amazon Personalize provides the following metrics. For each metric, higher numbers are better than lower numbers.

    • coverage - the proportion of unique recommended items from all queries out of the total number of unique items in the interactions and items datasets.
    • mean_reciprocal_rank_at_K - the mean of the reciprocal ranks of the first relevant recommendation out of the top K recommendations over all queries. This metric is appropriate if you're interested in the single highest ranked recommendation.
    • normalized_discounted_cumulative_gain_at_K - discounted gain assumes that recommendations lower on a list of recommendations are less relevant than higher recommendations. Therefore, each recommendation is discounted (given a lower weight) by a factor dependent on its position. To produce the cumulative discounted gain (DCG) at K, each relevant discounted recommendation in the top K recommendations is summed together. The normalized discounted cumulative gain (NDCG) is the DCG divided by the ideal DCG such that NDCG is between 0 - 1. (The ideal DCG is where the top K recommendations are sorted by relevance.) Amazon Personalize uses a weighting factor of 1/log(1 + position), where the top of the list is position 1. This metric rewards relevant items that appear near the top of the list, because the top of a list usually draws more attention.
    • precision_at_K - The number of relevant recommendations out of the top K recommendations divided by K. This metric rewards precise recommendation of the relevant items.

    For more information on these metrics, see Evaluating a Solution Version.

    Run the following code to evaluate this solution version:

    get_solution_metrics_response = personalize.get_solution_metrics(
        solutionVersionArn = solution_version_arn
    )
    
    print(json.dumps(get_solution_metrics_response, indent=2))

    Consider the following example output and take a closer look at what these metrics mean for the model:

    "metrics": {
        "coverage": 0.0762,
        "mean_reciprocal_rank_at_25": 0.2545,
        "normalized_discounted_cumulative_gain_at_10": 0.2257,
        "normalized_discounted_cumulative_gain_at_25": 0.2929,
        "normalized_discounted_cumulative_gain_at_5": 0.2112,
        "precision_at_10": 0.0339,
        "precision_at_25": 0.03,
        "precision_at_5": 0.0536
    }

    The normalized discounted cumulative gain above indicates that at 5 items, there is a 21% chance in a recommendation being a part of a user's interaction. Around 7% of the recommended items are unique, and there is a precision of about 5.3% in the top 5 recommended items.

    Keep in mind that this model uses rating data for interactions because MovieLens is an explicit dataset based on ratings. The timestamps also were from the time that the movie was rated, not watched, so the order is not the same as the order a viewer would watch movies. 


In this module, you created and configured an Amazon Personalize solution for model training. You also evaluated the solution version for model performance.

In the next module, you deploy your model by creating an Amazon Personalize campaign.