

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

# 設定 SageMaker Clarify 處理工作
<a name="clarify-processing-job-configure-parameters"></a>

若要使用 SageMaker Clarify 分析資料和模型的偏差和可解釋性，您必須設定 SageMaker Clarify 處理工作。本指南說明如何指定處理工作的輸入資料集名稱、分析組態檔案名稱和輸出位置。若要設定處理容器、工作輸入、輸出、資源和其他參數，您有兩種選擇。您可以使用 SageMaker AI `CreateProcessingJob` API，或使用 SageMaker AI Python SDK API `SageMaker ClarifyProcessor`，

如需有關所有處理工作的常用參數的資訊，請參閱 [Amazon SageMaker API 參考](https://docs.aws.amazon.com/sagemaker/latest/APIReference/Welcome.html?icmpid=docs_sagemaker_lp)。

## 使用 SageMaker API 設定 SageMaker Clarify 處理工作
<a name="clarify-processing-job-configure-parameters-API"></a>

下列指示說明如何使用 `CreateProcessingJob` API 提供 SageMaker Clarify 特定組態的每個部分。

1. 在 `AppSpecification` 參數內輸入 SageMaker Clarify 容器影像的統一研究識別碼 (URI)，如下列程式碼範例所示。

   ```
   {
       "ImageUri": "{{the-clarify-container-image-uri}}"
   }
   ```
**注意**  
URI 必須識別預先建置的 SageMaker Clarify 容器影像。不支援 `ContainerEntrypoint` 和 `ContainerArguments`。如需 SageMaker Clarify 容器映像的詳細資訊，請參閱[預先建置的 SageMaker Clarify 容器](clarify-processing-job-configure-container.md)。

1. 指定分析的組態和 `ProcessingInputs` 參數內輸入資料集的參數。

   1. 指定 JSON 分析組態檔案的位置，其中包括偏差分析和可解釋性分析的參數。`ProcessingInput` 物件的 `InputName` 參數必須為 **analysis\_config**，如下列程式碼範例所示。

      ```
      {
          "InputName": "analysis_config",
          "S3Input": {
              "S3Uri": "{{s3://your-bucket/analysis_config.json}}",
              "S3DataType": "S3Prefix",
              "S3InputMode": "File",
              "LocalPath": "/opt/ml/processing/{{input/config}}"
          }
      }
      ```

      如需分析組態檔案結構描述的詳細資訊，請參閱[分析組態檔案](clarify-processing-job-configure-analysis.md)。

   1. 指定輸入資料集的位置。`ProcessingInput` 物件的 `InputName` 參數必須是 `dataset`。如果您已在分析組態檔案中提供 “dataset\_uri”，則此參數為選用。`S3Input` 組態中需要下列值。

      1. `S3Uri` 可以是 Amazon S3 物件或 S3 字首。

      1. `S3InputMode` 必須是類型 **File**。

      1. `S3CompressionType` 必須是類型 `None` (預設值)。

      1. `S3DataDistributionType` 必須是類型 `FullyReplicated` (預設值)。

      1. `S3DataType` 可以是 `S3Prefix` 或 `ManifestFile`。若要使用 `ManifestFile`，`S3Uri` 參數應指定資訊清單檔案的位置，其按照 SageMaker API 參考部分 [S3Uri](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_S3DataSource.html#sagemaker-Type-S3DataSource-S3Uri) 的結構描述。此資訊清單檔案必須列出包含工作輸入資料的 S3 物件。

      下列程式碼顯示輸入組態的範例。

      ```
      {
          "InputName": "dataset",
          "S3Input": {
              "S3Uri": "{{s3://your-bucket/your-dataset.csv}}",
              "S3DataType": "S3Prefix",
              "S3InputMode": "File",
              "LocalPath": "/opt/ml/processing/{{input/data}}"
          }
      }
      ```

1. 指定 `ProcessingOutputConfig` 參數內處理工作輸出的組態。`Outputs` 組態中需要單一 `ProcessingOutput` 物件。輸出組態中需要下列項目：

   1. `OutputName` 必須為 **analysis\_result**。

   1. `S3Uri` 必須是輸出位置的 S3 字首。

   1. `S3UploadMode` 必須設定為 **EndOfJob**。

   下列程式碼顯示輸出組態的範例。

   ```
   {
       "Outputs": [{ 
           "OutputName": "analysis_result",
           "S3Output": { 
               "S3Uri": "{{s3://your-bucket/result/}}",
               "S3UploadMode": "EndOfJob",
               "LocalPath": "/opt/ml/processing/{{output}}"
            }
        }]
   }
   ```

1. 為您在 `ProcessingResources` 參數內的處理工作中使用的資源指定組態 `ClusterConfig`。`ClusterConfig` 物件內需要以下參數。

   1. `InstanceCount` 指定叢集中執行處理工作的運算執行個體數。指定大於 `1` 的值以啟用分散式處理。

   1. `InstanceType` 指的是執行處理工作的資源。由於 SageMaker AI SHAP 分析需要密集運算，因此使用對運算最佳化的執行個體類型應可改善分析的執行時期。SageMaker Clarify 處理工作不使用 GPU。

   下列程式碼顯示資源組態的範例。

   ```
   {
       "ClusterConfig": {
            "InstanceCount": {{1}},
            "InstanceType": "{{ml.m5.xlarge}}",
            "VolumeSizeInGB": {{20}}
        }
   }
   ```

1. 為您在 `NetworkConfig` 物件內的處理工作中使用的網路指定組態。組態中需要下列值。

   1. `EnableNetworkIsolation` 必須設定為 `False` (預設值)，以便 SageMaker Clarify 可以在必要時調用端點進行預測。

   1. 如果您提供給 SageMaker Clarify 工作的模型或端點位於 Amazon Virtual Private Cloud (Amazon VPC) 內，則 SageMaker Clarify 工作也必須位於同一個 VPC 中。使用 [VpcConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_VpcConfig.html) 指定 VPC。此外，VPC 必須具有連至 Amazon S3 儲存貯體、SageMaker AI 服務和 SageMaker AI 執行時期服務的端點。

      如果已啟用分散式處理，您還必須允許同一個處理工作中不同執行個體之間的通訊。為安全群組設定規則，允許相同安全群組成員彼此間的傳入連線。如需詳細資訊，請參閱[允許 Amazon SageMaker Clarify 任務存取您 Amazon VPC 中的資源](clarify-vpc.md)。

   下列程式碼顯示網路組態的範例。

   ```
   {
       "EnableNetworkIsolation": False,
       "VpcConfig": {
           ...
       }
   }
   ```

1. 使用 `StoppingCondition` 參數來設定工作執行的時間上限。SageMaker Clarify 工作可以執行的最長時間為 `7` 天或 `604800` 秒。如果工作無法在此時間限制內完成，則會停止且不會提供分析結果。例如，下列組態會將工作可執行的時間上限限制為 3600 秒。

   ```
   {
       "MaxRuntimeInSeconds": 3600
   }
   ```

1. 指定 `RoleArn` 參數的 IAM 角色。該角色必須與 Amazon SageMaker AI 有信任關係。它可用來執行下表所列的 SageMaker API 作業。我們建議您使用 Amazon SageMaker AI FullAccess 受管政策，授予 SageMaker AI 的完整存取權。如需此政策的詳細資訊，請參閱[AWS 受管政策：AmazonSageMakerFullAccess](security-iam-awsmanpol.md#security-iam-awsmanpol-AmazonSageMakerFullAccess)。如果您對授予完整存取權有疑慮，所需的最低權限取決於您提供的是模型還是端點名稱。使用端點名稱允許將較少的權限授予 SageMaker AI。

   下表包含 SageMaker Clarify 處理工作所使用的 API 作業。**模型名稱**和**端點名稱**下的 **X** 註明每個輸入所需的 API 作業。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/clarify-processing-job-configure-parameters.html)

   如需所需許可的相關資訊，請參閱[Amazon SageMaker AI API 許可：動作、許可與資源參考](api-permissions-reference.md)。

   如需將角色傳遞至 SageMaker AI 的詳細資訊，請參閱[傳遞角色](sagemaker-roles.md#sagemaker-roles-pass-role)。

   取得處理工作組態的個別部分之後，將其合併以設定工作。

## 使用適用於 Python 的 AWS SDK 設定 SageMaker Clarify 處理任務
<a name="clarify-processing-job-configure-parameters-SDK"></a>

下列程式碼範例示範如何使用[適用於 Python 的AWS SDK](https://aws.amazon.com/sdk-for-python/) 來啟動 SageMaker Clarify 處理工作。

```
sagemaker_client.create_processing_job(
    ProcessingJobName="{{your-clarify-job-name}}",
    AppSpecification={
        "ImageUri": "t{{he-clarify-container-image-uri}}",
    },
    ProcessingInputs=[{
            "InputName": "analysis_config",
            "S3Input": {
                "S3Uri": "s3://{{your-bucket/analysis_config.json}}",
                "S3DataType": "S3Prefix",
                "S3InputMode": "File",
                "LocalPath": "/opt/ml/processing/{{input/config}}",
            },
        }, {
            "InputName": "dataset",
            "S3Input": {
                "S3Uri": "s3://{{your-bucket/your-dataset.csv}}",
                "S3DataType": "S3Prefix",
                "S3InputMode": "File",
                "LocalPath": "/opt/ml/processing/{{input/data}}",
            },
        },
    ],
    ProcessingOutputConfig={
        "Outputs": [{ 
            "OutputName": "analysis_result",
            "S3Output": { 
               "S3Uri": "s3://{{your-bucket/result/}}",
               "S3UploadMode": "EndOfJob",
               "LocalPath": "/opt/ml/processing/{{output}}",
            },   
        }],
    },
    ProcessingResources={
        "ClusterConfig": {
            "InstanceCount": {{1}},
            "InstanceType": "{{ml.m5.xlarge}}",
            "VolumeSizeInGB": {{20}},
        },
    },
    NetworkConfig={
        "EnableNetworkIsolation": False,
        "VpcConfig": {
            ...
        },
    },
    StoppingCondition={
        "MaxRuntimeInSeconds": {{3600}},
    },
    RoleArn="arn:aws:iam::{{<your-account-id>:role/service-role/AmazonSageMaker-ExecutionRole}}",
)
```

如需範例筆記本，其中包含使用適用於 Python 的 AWS SDK 執行 SageMaker Clarify 處理任務的說明，請參閱[使用適用於 Python 的 AWS SDK 搭配 SageMaker Clarify 的公平性和可解釋性](http://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-clarify/fairness_and_explainability/fairness_and_explainability_boto3.ipynb)。筆記本中使用的任何 S3 儲存貯體都必須與存取該儲存貯體的筆記本執行個體位於相同的 AWS 區域。

## 使用 SageMaker Python SDK 來設定 SageMaker Clarify 處理工作
<a name="clarify-processing-job-configure-parameters-SM-SDK"></a>

您也可以使用 SageMaker Python SDK API 中的 [SageMaker ClarifyProcessor](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.clarify.SageMakerClarifyProcessor) 來設定 SageMaker Clarify 處理工作。如需詳細資訊，請參閱[執行 SageMaker Clarify 處理任務以進行偏差分析和解釋性](clarify-processing-job-run.md)。

**Topics**
+ [預先建置的 SageMaker Clarify 容器](clarify-processing-job-configure-container.md)
+ [分析組態檔案](clarify-processing-job-configure-analysis.md)
+ [資料格式相容性指南](clarify-processing-job-data-format.md)