

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

# 手动创建跟踪实体
<a name="lineage-tracking-manual-creation"></a>

您可以为任何属性手动创建跟踪实体，以建立模型管理、重现工作流程并保存工作历史记录。有关 Amazon A SageMaker I 自动创建的跟踪实体的信息，请参阅[Amazon SageMaker AI 创建的追踪实体](lineage-tracking-auto-creation.md)。以下教程演示了在 SageMaker 训练作业和端点之间手动创建和关联工件，然后跟踪工作流程所需的步骤。

您可以为除关联之外的所有实体添加标签。标签是提供自定义信息的任意键值对。您可以按标签对列表或搜索查询进行筛选或排序。有关更多信息，请参阅中的为[AWS 资源添加标签](https://docs.aws.amazon.com/general/latest/gr/aws_tagging.html)。*AWS 一般参考*

有关演示如何创建世系实体的示例笔记本，请参阅亚马逊[ SageMaker 示例 GitHub ](https://github.com/awslabs/amazon-sagemaker-examples)存储库中的 [Amazon SageMaker AI Lineag](https://github.com/aws/amazon-sagemaker-examples/tree/master/sagemaker-lineage) e 笔记本。

**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 资源名称 (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 A SageMaker I 自动创建的世系实体的数量没有限制。