

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

# 编译模型（亚马逊 A SageMaker I SDK）
<a name="neo-job-compilation-sagemaker-sdk"></a>

 您可以使用适用于 [Python 的 Amazon SageMaker AI SDK](https://sagemaker.readthedocs.io/en/stable/) 中的 [https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html?#sagemaker.estimator.Estimator.compile_model](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html?#sagemaker.estimator.Estimator.compile_model)API 来编译经过训练的模型，并针对特定的目标硬件对其进行优化。应该在模型训练期间，在使用的估算器对象上调用 API。

**注意**  
使用 MXNet 或编译模型`500`时，必须将`MMS_DEFAULT_RESPONSE_TIMEOUT`环境变量设置为 PyTorch。不需要环境变量 TensorFlow。

 以下是如何使用 `trained_model_estimator` 对象编译模型的示例：

```
# Replace the value of expected_trained_model_input below and
# specify the name & shape of the expected inputs for your trained model
# in json dictionary form
expected_trained_model_input = {'data':[1, 784]}

# Replace the example target_instance_family below to your preferred target_instance_family
compiled_model = trained_model_estimator.compile_model(target_instance_family='ml_c5',
        input_shape=expected_trained_model_input,
        output_path='insert s3 output path',
        env={'MMS_DEFAULT_RESPONSE_TIMEOUT': '500'})
```

该代码编译模型，将优化的模型保存在`output_path`，然后创建可部署到端点的 SageMaker AI 模型。