aws-samples / amazon-personalize-samples

Notebooks and examples on how to onboard and use various features of Amazon Personalize
MIT No Attribution
572 stars 336 forks source link

Pass custom SIMS recipe solutionConfig while creating solution via python sdk #156

Closed PlutoSenthil closed 1 year ago

PlutoSenthil commented 1 year ago

I tried to create solution for SIMS recipe through lambda handler eventType="CART", performHPO=True Boto3 version: 1.26.133

I am getting error like

Traceback (most recent call last): File "test.py", line 44, in lambda_handler() File "test.py", line 32, in lambda_handler response = personalize.create_solution( File "/.venv/lib/python3.8/site-packages/botocore/client.py", line 530, in _api_call return self._make_api_call(operation_name, kwargs) File "/.venv/lib/python3.8/site-packages/botocore/client.py", line 964, in _make_api_call raise error_class(parsed_response, operation_name) botocore.errorfactory.InvalidInputException: An error occurred (InvalidInputException) when calling the CreateSolution operation: Provide a hyperparameter that is used in the algorithm: arn:aws:personalize:::algorithm/aws-similar-items

solution_config = {
    "hpoConfig": {
        "hpoResourceConfig": {
            "maxNumberOfTrainingJobs": "20",
            "maxParallelTrainingJobs": "5"
        },
        "algorithmHyperParameterRanges": {
            "integerHyperParameterRanges": [
                {
                    "name": "model.id_embedding_dim",
                    "minValue": 30,
                    "maxValue": 200
                },
                {
                    "name": "model.feature_embedding_dim",
                    "minValue": 30,
                    "maxValue": 200
                }
            ],
            "continuousHyperParameterRanges": [],
            "categoricalHyperParameterRanges": []
        }
    },
    "featureTransformationParameters": {}
}
response = personalize.create_solution(
        name="user_sims_hytune_eventtype",
        datasetGroupArn="<>",
        recipeArn="arn:aws:personalize:::recipe/aws-similar-items",
        eventType="CART",
        performHPO=True,
        solutionConfig=solution_config
    )
james-jory commented 1 year ago

The integer hyperparameter names you are specifying in the solution config are invalid. You can find the HPO tunable hyperparams for Similar-Items in the documentation here:

https://docs.aws.amazon.com/personalize/latest/dg/native-recipe-similar-items.html#similar-items-hyperparameters

Or by calling the DescribeAlgorithm API for the Similar-Items algo.

$ aws personalize describe-algorithm --algorithm-arn arn:aws:personalize:::algorithm/aws-similar-items
{
    "algorithm": {
        "name": "aws-similar-items",
        "algorithmArn": "arn:aws:personalize:::algorithm/aws-similar-items",
        "algorithmImage": {
            "name": "Item Similarity"
        },
        "defaultHyperParameters": {
            "item_id_hidden_dim": "100",
            "item_metadata_hidden_dim": "100",
            "popularity_discount_factor": "0.0"
        },
        "defaultHyperParameterRanges": {
            "integerHyperParameterRanges": [
                {
                    "name": "item_id_hidden_dim",
                    "minValue": 30,
                    "maxValue": 200,
                    "isTunable": true
                },
                {
                    "name": "item_metadata_hidden_dim",
                    "minValue": 30,
                    "maxValue": 200,
                    "isTunable": true
                }
            ],
            "continuousHyperParameterRanges": [
                {
                    "name": "popularity_discount_factor",
                    "minValue": 0.0,
                    "maxValue": 1.0,
                    "isTunable": false
                }
            ],
            "categoricalHyperParameterRanges": []
        },
        "defaultResourceConfig": {
            "maxNumberOfTrainingJobs": "20",
            "maxParallelTrainingJobs": "5"
        },
        "trainingInputMode": "File",
        "creationDateTime": "2019-06-09T17:00:00-07:00",
        "lastUpdatedDateTime": "2023-05-02T12:29:50.416000-07:00"
    }
}

I suspect you found the names model.id_embedding_dim and model.feature_embedding_dim from the DescribeSolution API response for an existing Similar-Items solution. Unfortunately those names do not match the corresponding input names for CreateSolution so you will have to map them to item_id_hidden_dim and item_metadata_hidden_dim, respectively, before using them to create another solution.