

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

# 使用 SageMaker Python 软件开发工具包配置数据输入模式
<a name="model-access-training-data-using-pysdk"></a>

SageMaker Python SDK 为[用于启动训练作业的机器学习框架提供了通用 E [stimator 类](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator)及其变体](https://sagemaker.readthedocs.io/en/stable/frameworks/index.html)。在配置 SageMaker AI `Estimator` 类或`Estimator.fit`方法时，您可以指定其中一种数据输入模式。以下代码模板显示指定输入模式的两种方法。

**使用 Estimator 类指定输入模式**

```
from sagemaker.estimator import Estimator
from sagemaker.inputs import TrainingInput

estimator = Estimator(
    checkpoint_s3_uri='{{s3://amzn-s3-demo-bucket/checkpoint-destination/}}',
    output_path='{{s3://amzn-s3-demo-bucket/output-path/}}',
    base_job_name='{{job-name}}',
    input_mode='{{File}}'  # Available options: File | Pipe | FastFile
    ...
)

# Run the training job
estimator.fit(
    inputs=TrainingInput(s3_data="{{s3://amzn-s3-demo-bucket/my-data/train}}")
)
```

*欲了解更多信息，请参阅 Python [SDK 文档中的 sagemaker.estimator.estimator.estimat](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator) or 类。SageMaker *

**通过 `estimator.fit()` 方法指定输入模式**

```
from sagemaker.estimator import Estimator
from sagemaker.inputs import TrainingInput

estimator = Estimator(
    checkpoint_s3_uri='{{s3://amzn-s3-demo-bucket/checkpoint-destination/}}',
    output_path='{{s3://amzn-s3-demo-bucket/output-path/}}',
    base_job_name='{{job-name}}',
    ...
)

# Run the training job
estimator.fit(
    inputs=TrainingInput(
        s3_data="{{s3://amzn-s3-demo-bucket/my-data/train}}",
        input_mode='{{File}}'  # Available options: File | Pipe | FastFile
    )
)
```

欲了解更多信息，请参阅 [sagemaker.estimator。 Estimator.fit](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator.fit)类方法和 [sagemaker.inputs。 ](https://sagemaker.readthedocs.io/en/stable/api/utility/inputs.html#sagemaker.inputs.TrainingInput)TrainingInput*SageMaker Python 软件开发工具包文档*中的类。

**提示**  
要详细了解如何 SageMaker 使用 Python SDK [估算器使用你的 VPC 配置配置 Amazon FSx for Lustre 或 Amazon EFS，请参阅 AI Python SDK 文档*SageMaker 中的*使用文件系统](https://sagemaker.readthedocs.io/en/stable/overview.html?highlight=VPC#use-file-systems-as-training-inputs)作为训练输入。

**提示**  
建议将数据输入模式与 Amazon S3、Amazon EFS 和适用于 Lustre 的 FSx 集成，从而优化数据来源的配置以获得最佳实践。您可以使用 SageMaker AI 托管存储选项和输入模式从战略上提高数据加载性能，但它不受严格限制。您可以直接在训练容器中编写自己的数据读取逻辑。例如，您可以设置为从不同的数据来源读取，编写自己的 S3 数据加载器类，或者在训练脚本中使用第三方框架的数据加载函数。但是，您必须确保指定 SageMaker 人工智能可以识别的正确路径。

**提示**  
如果您使用自定义训练容器，请务必安装有助于为[SageMaker 训练作业设置环境的 SageMaker 培训工具包](https://github.com/aws/sagemaker-training-toolkit)。否则，您必须在 Dockerfile 中明确指定环境变量。有关更多信息，请参阅[使用自己的算法和模型创建容器](https://docs.amazonaws.cn/sagemaker/latest/dg/docker-containers-create.html)。

有关如何使用低级 SageMaker API 设置数据输入模式的更多信息[Amazon SageMaker AI 如何提供培训信息](your-algorithms-training-algo-running-container.md)，请参阅 [`CreateTrainingJob`](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateTrainingJob.html)API 和`TrainingInputMode`中的[`AlgorithmSpecification`](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_AlgorithmSpecification.html)。