

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

# 要監控屬性偏離的參數
<a name="clarify-config-json-monitor-model-explainability-parameters"></a>

Amazon SageMaker Clarify 可解釋性監控會重複使用 [分析組態檔案](clarify-processing-job-configure-analysis.md) 的分析組態中使用的參數子集。下列參數必須在 JSON 檔案中提供，且必須在 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelExplainabilityAppSpecification](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelExplainabilityAppSpecification) 的 `ConfigUri` 參數中提供路徑。
+ `"version"` –(選用) 組態檔案的結構描述版本。如果未提供，則會使用最新的支援版本。
+ `"headers"` –(選用) 資料集中的功能名稱清單。可解釋性分析不需要標籤。
+ `"methods"` – 用於分析和報告的方法及其參數的清單。如果省略了任何區段，則不列入計算。
  + `"shap"` –(選用) SHAP 值計算的區段。
    + `"baseline"` –(選用) 資料列清單(至少一個)，或 Amazon Simple Storage Service Amazon S3 物件 URI。用作核心 SHAP 演算法中的基準資料集 (也稱為背景資料集)。此格式應與資料集格式相同。每一列應該只包含功能欄 (或值)。在將每一列傳送至模型之前，省略必須排除的任何欄。
    + `"num_samples"` – 要在核心 SHAP 演算法中使用的樣本數。此數字決定產生的合成資料集的大小來計算 SHAP 值。如果未提供，則 SageMaker Clarify 工作會根據功能計數來選擇值。
    + `"agg_method"` – 全域 SHAP 值的彙總方法。有效值如下：
      + `"mean_abs"` – 所有執行個體的絕對 SHAP 值的平均值。
      + `"median"` – 所有執行個體的 SHAP 值的中間值。
      + `"mean_sq"` – 所有執行個體的平方 SHAP 值的平均值。
    + `"use_logit"` –(選用) 布林值，指出 logit 函式是否要套用至模型預測。如果 `"use_logit"` 是 `true`，則 SHAP 值有對數機率單位。預設值為 `false`。
    + `"save_local_shap_values"` –(選用) 布林值，指示是否要將本機 SHAP 值儲存在輸出位置。使用 `true` 以儲存。使用 `false` 不要儲存。預設值為 `false`。
+ `"predictor"` – (即時端點為選用，批次轉換為必要) 模型參數的區段，如果 `"shap"` 和 `"post_training_bias"` 區段存在，則為必要。
  + `"model_name"` – 由 `CreateModel` API 建立的模型名稱，容器模式為 `SingleModel`。
  + `"instance_type"` – 陰影端點的執行個體類型。
  + `"initial_instance_count"` – 陰影端點的執行個體計數。
  + `"content_type"` –(選用) 用於透過陰影端點取得推論的模型輸入格式。有效值為 `"text/csv"` (對於 CSV)、`"application/jsonlines"` (對於 JSON 行)、`application/x-parquet` (對於 Apache Parquet)，以及 `application/x-image` 以啟用電腦視覺可解釋性。預設值與 `dataset_type` 格式相同。
  + `"accept_type"` –(選用) 用於透過陰影端點取得推論的模型*輸出*格式。有效值為 `"text/csv"` (對於 CSV)、`"application/jsonlines"` (對於 JSON 行)。如果省略，SageMaker Clarify 會使用擷取資料的回應資料類型。
  + `"content_template"` –(選用) 用來建構資料集執行個體之模型輸入的範本字串。只有在 `"content_type"` 為 `"application/jsonlines"` 時才會使用。該範本應只有一個預留位置 `$features`，它在執行期時被功能清單取代。例如，指定 `"content_template":"{\"myfeatures\":$features}"`，如果執行個體 (無標籤) 是 `1,2,3`，則模型輸入變為 JSON 行 `'{"myfeatures":[1,2,3]}'`。
  + `"label_headers"` –(選用) 資料集中 `"label"` 接收的值清單。將模型端點或批次轉換工作傳回的分數與其對應的標籤值相關聯。如果有提供，則分析報告會使用標題，而不是像 `“label0”` 的預留位置。

其他參數應在 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelExplainabilityJobInput](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelExplainabilityJobInput) API 的 `EndpointInput` (用於即時端點)或 `BatchTransformInput` (用於批次轉換工作)中提供。
+ `FeaturesAttribute` – 如果端點或批次工作輸入資料格式為 `"application/jsonlines"`，則此參數是必要項目。如果資料集格式是 JSON 行，它是用於定位功能欄的 JMESPath。
+ `ProbabilityAttribute` – 機率的模型輸出中的索引或 JMESPath 位置。例如，如果模型輸出是含有標籤和機率清單的 JSON 行，則會選擇對應於最大機率的標籤進行偏差運算。

## CSV 和 JSON 行資料集的 JSON 組態檔案範例
<a name="clarify-config-json-monitor-model-explainability-parameters-examples"></a>

以下是 JSON 檔案的範例，用來設定 CSV 和 JSON 行資料集以監控功能屬性偏離。

**Topics**
+ [CSV 資料集](#clarify-config-json-monitor-model-explainability-parameters-example-csv)
+ [JSON 行資料集](#clarify-config-json-monitor-model-explainability-parameters-example-jsonlines)

### CSV 資料集
<a name="clarify-config-json-monitor-model-explainability-parameters-example-csv"></a>

考慮有三個數值特徵欄的資料集，如下列範例所示。

```
0.5814568701544718, 0.6651538910132964, 0.3138080342665499
0.6711642728531724, 0.7466687034026017, 0.1215477472819713
0.0453256543003371, 0.6377430803264152, 0.3558625219713576
0.4785191813363956, 0.0265841045263860, 0.0376935084990697
```

假設模型輸出有兩欄，其中第一個是預測標籤，第二個是機率，如下列範例所示。

```
1, 0.5385257417814224
```

下列 JSON 組態檔案會顯示如何設定此 CSV 資料集。

```
{
                    
    "headers": [
        "feature_1",
        "feature_2",
        "feature_3"
    ],
    "methods": {
        "shap": {
            "baseline": [
                [0.4441164946610942, 0.5190374448171748, 0.20722795300473712]
            ],
            "num_samples": 100,
            "agg_method": "mean_abs"
        }
    },
    "predictor": {
        "model_name": "my_model",
        "instance_type": "ml.m5.xlarge",
        "initial_instance_count": 1
    }
}
```

預測標籤由 `"ProbabilityAttribute"` 參數選取。使用從零開始的編號，因此 1 表示模型輸出的第二欄。

```
"EndpointInput": {
    ...
    "ProbabilityAttribute": 1
    ...
}
```

### JSON 行資料集
<a name="clarify-config-json-monitor-model-explainability-parameters-example-jsonlines"></a>

考慮有四個功能欄和一個標籤欄的資料集，其中第一個功能和標籤是二進位，如下列範例所示。

```
{"features":[0, 0.5814568701544718, 0.6651538910132964, 0.3138080342665499], "label":0}
{"features":[1, 0.6711642728531724, 0.7466687034026017, 0.1215477472819713], "label":1}
{"features":[0, 0.0453256543003371, 0.6377430803264152, 0.3558625219713576], "label":1}
{"features":[1, 0.4785191813363956, 0.0265841045263860, 0.0376935084990697], "label":1}
```

模型輸入與資料集格式相同，模型輸出為 JSON 行，如下列範例所示。

```
{"predicted_label":1, "probability":0.5385257417814224}
```

在下面的範例中，JSON 組態檔案會顯示如何設定此 JSON 行資料集。

```
{
    "headers": [
        "feature_1",
        "feature_2",
        "feature_3"
    ],
    "methods": {
        "shap": {
            "baseline": [
                {"features":[0.4441164946610942, 0.5190374448171748, 0.20722795300473712]}
            ],
            "num_samples": 100,
            "agg_method": "mean_abs"
        }
    },
    "predictor": {
        "model_name": "my_model",
        "instance_type": "ml.m5.xlarge",
        "initial_instance_count": 1,
        "content_template":"{\"features\":$features}"
    }
}
```

然後，`EndpointInput` 中的 `"features"` 參數值 (針對即時端點) 或 `BatchTransformInput` (針對批次轉換工作) 用於訂位資料集中的功能，而 `"probability"` 參數值會從模型輸出中選取機率值。

```
"EndpointInput": {
    ...
    "FeaturesAttribute": "features",
    "ProbabilityAttribute": "probability",
    ...
}
```