

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用推論管道執行即時預測
<a name="inference-pipeline-real-time"></a>

您可以使用推論管道中已訓練的模型，直接進行即時預測，而不需要執行外部預先處理。您設定管道時，您可以選擇使用 Amazon SageMaker AI 中已有的內建特徵轉換器。或者，您可以只使用幾行 scikit-learn 或 Spark 程式碼，以實作您自己的轉換邏輯。

[MLeap](https://combust.github.io/mleap-docs/) (機器學習管道的序列化格式和執行引擎) 支援 Spark、scikit-learn 和 TensorFlow，以訓練管道並匯出至稱為 MLeap 套件的序列化管道。您可以將這些套件還原序列化到 Spark，以進行批次模式評分，或還原序列化到 MLeap 執行期，以強化即時 API 服務。

管道中的容器會監聽 `SAGEMAKER_BIND_TO_PORT` 環境變數指定的連接埠 (而不是 8080)。在推論管道中執行時，SageMaker AI 會自動提供此環境變數給容器。如果此環境變數不存在，容器預設使用連接埠 8080。若要表示您的容器符合此需求，請使用下列命令，將標籤新增到您的 Dockerfile：

```
LABEL com.amazonaws.sagemaker.capabilities.accept-bind-to-port=true
```

如果您的容器需要監聽第二個連接埠，請選擇 `SAGEMAKER_SAFE_PORT_RANGE` 環境變數指定的範圍內的連接埠。以 **"XXXX-YYYY"** 格式的包含範圍指定值，其中 `XXXX` 和 `YYYY` 是多位數整數。您在多容器管道中執行容器時，SageMaker AI 會自動提供此值。

**注意**  
若要在包含 [SageMaker AI 內建演算法](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html)的管道中使用自訂 Docker 映像檔時，您需要 [Amazon Elastic Container Registry (Amazon ECR) 政策](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)。您的 Amazon ECR 儲存庫必須授與 SageMaker AI 提取映像的許可。如需詳細資訊，請參閱[推論管道 Amazon ECR 許可的疑難排解](inference-pipeline-troubleshoot.md#inference-pipeline-troubleshoot-permissions)。

## 建立和部署推論管道端點
<a name="inference-pipeline-real-time-sdk"></a>

以下程式碼使用 SageMaker AI SDK，依序使用 SparkML 和 XGBoost 模型建立及部署即時推論管道模型。

```
from sagemaker.model import Model
from sagemaker.pipeline_model import PipelineModel
from sagemaker.sparkml.model import SparkMLModel

sparkml_data = 's3://{}/{}/{}'.format(s3_model_bucket, s3_model_key_prefix, 'model.tar.gz')
sparkml_model = SparkMLModel(model_data=sparkml_data)
xgb_model = Model(model_data=xgb_model.model_data, image=training_image)

model_name = 'serial-inference-' + timestamp_prefix
endpoint_name = 'serial-inference-ep-' + timestamp_prefix
sm_model = PipelineModel(name=model_name, role=role, models=[sparkml_model, xgb_model])
sm_model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge', endpoint_name=endpoint_name)
```

## 從推論管道端點要求即時推論
<a name="inference-pipeline-endpoint-request"></a>

下列範例說明如何呼叫推論端點，並以 JSON 格式傳送請求承載，以進行即時預測：

```
import sagemaker
from sagemaker.predictor import json_serializer, json_deserializer, Predictor

payload = {
        "input": [
            {
                "name": "Pclass",
                "type": "float",
                "val": "1.0"
            },
            {
                "name": "Embarked",
                "type": "string",
                "val": "Q"
            },
            {
                "name": "Age",
                "type": "double",
                "val": "48.0"
            },
            {
                "name": "Fare",
                "type": "double",
                "val": "100.67"
            },
            {
                "name": "SibSp",
                "type": "double",
                "val": "1.0"
            },
            {
                "name": "Sex",
                "type": "string",
                "val": "male"
            }
        ],
        "output": {
            "name": "features",
            "type": "double",
            "struct": "vector"
        }
    }

predictor = Predictor(endpoint=endpoint_name, sagemaker_session=sagemaker.Session(), serializer=json_serializer,
                                content_type='text/csv', accept='application/json')

print(predictor.predict(payload))
```

您從 `predictor.predict(payload)` 得到的回應是模型的推論結果。

## 即時推論管道範例
<a name="inference-pipeline-example"></a>

您可以[使用 SKLearn 預測器來執行此範例筆記本](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/scikit_learn_randomforest/Sklearn_on_SageMaker_end2end.ipynb)，其中會顯示如何部署端點、執行推論請求，然後還原序列化回應。在 [Amazon SageMaker 範例 GitHub 儲存庫中](https://github.com/awslabs/amazon-sagemaker-examples)，找到此筆記本和更多範例。