

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

# 手動建立追蹤實體
<a name="lineage-tracking-manual-creation"></a>

您可以為任何屬性手動建立追蹤實體，以建立模型治理、重現工作流程，以及維護工作歷程的記錄。如需 Amazon SageMaker AI 自動建立的追蹤實體的相關資訊，請參閱 [Amazon SageMaker AI 建立的追蹤實體](lineage-tracking-auto-creation.md)。以下教學課程展示在 SageMaker 訓練任務與端點之間手動建立和關聯成品，然後追蹤工作流程所需的步驟。

您可以將標籤新增至除了關聯以外的所有實體。標籤是提供自訂資訊的任意鍵值對。您能夠依標籤對清單執行篩選或排序，或執行搜尋查詢。如需詳細資訊，請參閱《》中的[標記 AWS 資源](https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html)*AWS 一般參考*。

如需示範如何建立歷程實體的範例筆記本，請參閱 [Amazon SageMaker 範例 GitHub 儲存庫](https://github.com/awslabs/amazon-sagemaker-examples)中的 [Amazon SageMaker AI 歷程](https://github.com/aws/amazon-sagemaker-examples/tree/master/sagemaker-lineage)筆記本。

**Topics**
+ [手動建立實體](#lineage-tracking-manual-create)
+ [手動追蹤工作流程](#lineage-tracking-manual-track)
+ [限制](#lineage-tracking-manual-track-limits)

## 手動建立實體
<a name="lineage-tracking-manual-create"></a>

下列程序展示如何在 SageMaker AI 訓練任務與端點之間建立和關聯成品。您會執行以下步驟：

**匯入追蹤實體和關聯**

1. 匯入歷程追蹤實體。

   ```
   import sys
   !{sys.executable} -m pip install -q sagemaker
   
   from sagemaker import get_execution_role
   from sagemaker.session import Session
   from sagemaker.lineage import context, artifact, association, action
   
   import boto3
   boto_session = boto3.Session(region_name=region)
   sagemaker_client = boto_session.client("sagemaker")
   ```

1. 輸入和輸出成品。

   ```
   code_location_arn = artifact.Artifact.create(
       artifact_name='source-code-location',
       source_uri='s3://...',
       artifact_type='code-location'
   ).artifact_arn
   
   # Similar constructs for train_data_location_arn and test_data_location_arn
   
   model_location_arn = artifact.Artifact.create(
       artifact_name='model-location',
       source_uri='s3://...',
       artifact_type='model-location'
   ).artifact_arn
   ```

1. 訓練模型並獲得代表訓練工作的 `trial_component_arn`。

1. 將輸入成品和輸出成品與訓練工作 (試用元件) 相關聯。

   ```
   input_artifacts = [code_location_arn, train_data_location_arn, test_data_location_arn]
   for artifact_arn in input_artifacts:
       try:
           association.Association.create(
               source_arn=artifact_arn,
               destination_arn=trial_component_arn,
               association_type='ContributedTo'
           )
       except:
           logging.info('association between {} and {} already exists', artifact_arn, trial_component_arn)
   
   output_artifacts = [model_location_arn]
   for artifact_arn in output_artifacts:
       try:
            association.Association.create(
               source_arn=trial_component_arn,
               destination_arn=artifact_arn,
               association_type='Produced'
           )
       except:
           logging.info('association between {} and {} already exists', artifact_arn, trial_component_arn)
   ```

1. 建立推論端點。

   ```
   predictor = mnist_estimator.deploy(initial_instance_count=1,
                                        instance_type='ml.m4.xlarge')
   ```

1. 建立端點內容。

   ```
   from sagemaker.lineage import context
   
   endpoint = sagemaker_client.describe_endpoint(EndpointName=predictor.endpoint_name)
   endpoint_arn = endpoint['EndpointArn']
   
   endpoint_context_arn = context.Context.create(
       context_name=predictor.endpoint_name,
       context_type='Endpoint',
       source_uri=endpoint_arn
   ).context_arn
   ```

1. 將訓練工作 (試用元件) 與端點內容相關聯。

   ```
   association.Association.create(
       source_arn=trial_component_arn,
       destination_arn=endpoint_context_arn
   )
   ```

## 手動追蹤工作流程
<a name="lineage-tracking-manual-track"></a>

您可以手動追蹤在上一節中建立的工作流程。

基於上一個範例中的端點 Amazon Resource Name (ARN)，下列程序會展是如何追蹤工作流程，返回到用於訓練已部署到端點之模型的資料集。您會執行以下步驟：

**追蹤從端點到訓練資料來源的工作流程**

1. 匯入追蹤實體。

   ```
   import sys
   !{sys.executable} -m pip install -q sagemaker
   
   from sagemaker import get_execution_role
   from sagemaker.session import Session
   from sagemaker.lineage import context, artifact, association, action
   
   import boto3
   boto_session = boto3.Session(region_name=region)
   sagemaker_client = boto_session.client("sagemaker")
   ```

1. 從端點 ARN 獲取端點內容。

   ```
   endpoint_context_arn = sagemaker_client.list_contexts(
       SourceUri=endpoint_arn)['ContextSummaries'][0]['ContextArn']
   ```

1. 透過試用元件和端點內容之間的關聯取得試用元件。

   ```
   trial_component_arn = sagemaker_client.list_associations(
       DestinationArn=endpoint_context_arn)['AssociationSummaries'][0]['SourceArn']
   ```

1. 透過試用元件和端點內容之間的關聯取得訓練資料位置成品。

   ```
   train_data_location_artifact_arn = sagemaker_client.list_associations(
       DestinationArn=trial_component_arn, SourceType='Model')['AssociationSummaries'][0]['SourceArn']
   ```

1. 透過訓練資料位置成品取得訓練資料位置。

   ```
   train_data_location = sagemaker_client.describe_artifact(
       ArtifactArn=train_data_location_artifact_arn)['Source']['SourceUri']
       print(train_data_location)
   ```

   回應：

   ```
   s3://sagemaker-sample-data-us-east-2/mxnet/mnist/train
   ```

## 限制
<a name="lineage-tracking-manual-track-limits"></a>

您可以在任何實體、實驗和歷程之間建立關聯，但下列項目除外：
+ 您無法在兩個實驗實體之間建立關聯。實驗實體由實驗、試用和試用元件組成。
+ 您可以建立與其他關聯之間的關聯。

如果您嘗試建立已存在的實體，就會發生錯誤。

**手動建立之歷程實體數量的上限**
+ 動作：3000
+ 成品：6000
+ 關聯：6000
+ 內容：500

Amazon SageMaker AI 自動建立的歷程實體數量沒有限制。