

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 超级参数和 HPO
<a name="customizing-solution-config-hpo"></a>

**重要**  
默认情况下，所有新解决方案都使用自动训练。如果使用自动训练，则在解决方案处于活动状态时，就会产生训练费用。为避免产生不必要的费用，在完成后，可以[更新解决方案](updating-solution.md)以关闭自动训练。有关训练费用的信息，请参阅 [Amazon Personalize 定价](https://aws.amazon.com/personalize/pricing/)。

您可以在训练之前指定超参数，以便针对您的特定使用案例优化经过训练的模型。这与模型参数相反，后者的值在训练过程中确定。

超参数是使用 `algorithmHyperParameters` 键指定的，该键是传递到 [CreateSolution](API_CreateSolution.md) 操作的 [SolutionConfig](API_SolutionConfig.md) 对象的一部分。

`CreateSolution` 请求的精简版本如下。该示例包括 `solutionConfig` 对象。您使用 `solutionConfig` 来覆盖食谱的默认参数。

```
{
  "name": "string",
  "recipeArn": "string",
  "eventType": "string",
  "solutionConfig": {
      "optimizationObjective": {
          "itemAttribute": "string",
          "objectiveSensitivity": "string"
      },
      "eventValueThreshold": "string",
      "featureTransformationParameters": {
          "string" : "string"
      },
      "algorithmHyperParameters": {
          "string" : "string"
      },
      "hpoConfig": {
          "algorithmHyperParameterRanges": {
              ...
          },
          "hpoResourceConfig": {
              "maxNumberOfTrainingJobs": "string",
              "maxParallelTrainingJobs": "string"
          }
      },
  },
}
```

使用不同超级参数的不同食谱。有关可用的超级参数，请参阅[选择食谱](working-with-predefined-recipes.md)中的各个配方。

## 启用超参数优化
<a name="hpo-tuning"></a>

超参数优化 (HPO) 或调整是针对特定学习目标选择最佳超参数的任务。最佳超级参数是通过运行使用来自指定可能性范围的不同值的很多训练任务确定的。

使用 [User-Personalization-v2](native-recipe-user-personalization-v2.md) 和 [Personalized-Ranking-v2](native-recipe-personalized-ranking-v2.md) 时，如果开启自动训练，则 Amazon Personalize 将每 90 天自动执行一次 HPO。如果没有开启自动训练，就不会执行 HPO。对于所有其他配方，必须启用 HPO。要使用 HPO，请将 `performHPO` 设置为 `true`，并包含 `hpoConfig` 对象。

超参数可以是分类值、连续值或整数值。`hpoConfig` 对象具有与所有这些类型对应的键，您可以在其中指定超级参数及其范围。您必须在请求中提供每种类型，但是如果食谱没有某个类型的参数，则您可以将其留空。例如，User-Personalization 没有连续类型的可调整超参数。因此，对于 `continousHyperParameterRange`，您可以传递一个空数组。

以下代码显示了如何使用 SDK for Python (Boto3) 创建启用了 HPO 的解决方案。本示例中的解决方案使用 [User-Personalization 食谱](native-recipe-new-item-USER_PERSONALIZATION.md)食谱，并将 HPO 设置为 `true`。代码为 `hidden_dimension`、`categoricalHyperParameterRanges` 和 `integerHyperParameterRanges` 提供值。`continousHyperParameterRange` 为空，`hpoResourceConfig` 设置为 `maxNumberOfTrainingJobs` 和 `maxParallelTrainingJobs`。

```
import boto3

personalize = boto3.client('personalize')

create_solution_response = personalize.create_solution(
    name = "solution name",
    datasetGroupArn = 'arn:aws:personalize:region:accountId:dataset-group/datasetGroupName',
    recipeArn = 'arn:aws:personalize:::recipe/aws-user-personalization',
    performHPO = True,
    solutionConfig = {
        "algorithmHyperParameters": {
          "hidden_dimension": "55"
        },
        "hpoConfig": {
          "algorithmHyperParameterRanges": {
              "categoricalHyperParameterRanges": [
                  {
                      "name": "recency_mask",
                      "values": [ "true", "false"]
                  }
              ],
              "integerHyperParameterRanges": [
                  {
                      "name": "bptt",
                      "minValue": 2,
                      "maxValue": 22
                  }
              ],
              "continuousHyperParameterRanges": [

              ]
          },
          "hpoResourceConfig": {
              "maxNumberOfTrainingJobs": "4",
              "maxParallelTrainingJobs": "2"
          }
        }
    }
)
```

有关 HPO 的更多信息，请参阅[自动模型优化](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning.html)。

## 查看超参数
<a name="viewing-hyperparameters"></a>

您可以通过调用 [DescribeSolution](API_DescribeSolution.md) 操作来查看解决方案的超参数。下面的示例显示了 `DescribeSolution` 输出。创建解决方案版本（训练模型）后，您还可以通过 [DescribeSolutionVersion](API_DescribeSolutionVersion.md) 操作查看超参数。

```
{
  "solution": {
    "name": "hpo_coonfig_solution",
    "solutionArn": "arn:aws:personalize:region:accountId:solution/solutionName",
    "performHPO": true,
    "performAutoML": false,
    "recipeArn": "arn:aws:personalize:::recipe/aws-user-personalization",
    "datasetGroupArn": "arn:aws:personalize:region:accountId:dataset-group/datasetGroupName",
    "eventType": "click",
    "solutionConfig": {
      "hpoConfig": {
        "hpoResourceConfig": {
          "maxNumberOfTrainingJobs": "4",
          "maxParallelTrainingJobs": "2"
        },
        "algorithmHyperParameterRanges": {
          "integerHyperParameterRanges": [
            {
              "name": "training.bptt",
              "minValue": 2,
              "maxValue": 22
            }
          ],
          "continuousHyperParameterRanges": [],
          "categoricalHyperParameterRanges": [
            {
              "name": "data.recency_mask",
              "values": [
                "true",
                "false"
              ]
            }
          ]
        }
      },
      "algorithmHyperParameters": {
        "hidden_dimension": "55"
      }
    },
    "status": "ACTIVE",
    "creationDateTime": "2022-07-08T12:12:48.565000-07:00",
    "lastUpdatedDateTime": "2022-07-08T12:12:48.565000-07:00"
  }
}
```