

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

# 使用 SageMaker Python SDK 選擇資料輸入模式
<a name="model-access-training-data-using-pysdk"></a>

SageMaker Python SDK 提供一般[估算器類別](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator)及[適用於機器學習 (ML) 架構的變體](https://sagemaker.readthedocs.io/en/stable/frameworks/index.html)，用於啟動訓練工作。您可以在設定 SageMaker AI `Estimator` 類別或 `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',
    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")
)
```

如需更多資訊，請參閱 *SageMaker Python SDK 文件*內的 [sagemaker.estimator.Estimator](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator) 類別。

**透過 `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 Python SDK 文件*內的[sagemaker.estimator.Estimator.fit](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.estimator.Estimator.fit) 類別方法以及 [sagemaker.inputs.TrainingInput](https://sagemaker.readthedocs.io/en/stable/api/utility/inputs.html#sagemaker.inputs.TrainingInput) 類別。

**提示**  
要進一步了解有關如何使用 SageMaker Python SDK 估算器透過 VPC 組態來配置 Amazon FSx for Lustre 或 Amazon EFS，請參閱 *SageMaker AI Python SDK 文件*內的[使用檔案系統作為訓練輸入](https://sagemaker.readthedocs.io/en/stable/overview.html?highlight=VPC#use-file-systems-as-training-inputs)一文。

**提示**  
資料輸入模式與 Amazon S3、Amazon EFS 和 FSx for Lustre 整合，是設定資料來源以獲得最佳實務的最佳建議方式。您可以使用 SageMaker AI 受管儲存選項和輸入模式，從策略上改善資料載入效能，但這並非嚴格的限制。您可以直接在訓練容器中撰寫自己的資料讀取邏輯。例如，您可以設定為從不同的資料來源讀取、撰寫自己的 S3 資料載入器類別，或在訓練指令碼中使用第三方架構的資料載入功能。但是，您必須確定您指定了 SageMaker AI 可以辨識的正確路徑。

**提示**  
如果您使用自訂訓練容器，請務必安裝 [SageMaker 訓練工具組](https://github.com/aws/sagemaker-training-toolkit)，以協助設定 SageMaker 訓練工作的環境。否則，您必須在 Dockerfile 中明確指定環境變數。如需更多資訊，請參閱[使用自有的演算法和模型建立容器](https://docs.aws.amazon.com/sagemaker/latest/dg/docker-containers-create.html)。

有關如何使用低階 SageMaker API 設定資料輸入模式的更多相關資訊[Amazon SageMaker AI 提供訓練資訊的方式](your-algorithms-training-algo-running-container.md)，請參閱 ，[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) API 和 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AlgorithmSpecification.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AlgorithmSpecification.html) 內的 `TrainingInputMode`。