本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
TensorFlow 是一種開放原始碼的機器學習和人工智慧程式庫。Amazon SageMaker Python SDK 中的 TensorFlowProcessor
提供您使用 TensorFlow 指令碼執行處理任務的能力。使用 TensorFlowProcessor
時,您可以運用 Amazon 建置的 Docker 容器與受管 TensorFlow 環境,這樣您就不必使用自己的容器。
下列程式碼範例示範如何使用 SageMaker AI 提供的 Docker 映像來執行TensorFlowProcessor
處理任務。請注意,執行任務時,您可以在 source_dir
引數中指定一個包含指令碼和相依性的目錄,而且可以在 source_dir
目錄中擁有一個 requirements.txt
檔案來指定處理指令碼的相依性。SageMaker Processing 會在容器中為您安裝 requirements.txt
中的相依性。
from sagemaker.tensorflow import TensorFlowProcessor
from sagemaker.processing import ProcessingInput, ProcessingOutput
from sagemaker import get_execution_role
#Initialize the TensorFlowProcessor
tp = TensorFlowProcessor(
framework_version='2.3',
role=get_execution_role(),
instance_type='ml.m5.xlarge',
instance_count=1,
base_job_name='frameworkprocessor-TF',
py_version='py37'
)
#Run the processing job
tp.run(
code='processing-script.py
',
source_dir='scripts
',
inputs=[
ProcessingInput(
input_name='data',
source=f's3://{BUCKET}/{S3_INPUT_PATH}
',
destination='/opt/ml/processing/input/data'
),
ProcessingInput(
input_name='model',
source=f's3://{BUCKET}/{S3_PATH_TO_MODEL}
',
destination='/opt/ml/processing/input/model'
)
],
outputs=[
ProcessingOutput(
output_name='predictions',
source='/opt/ml/processing/output',
destination=f's3://{BUCKET}/{S3_OUTPUT_PATH}
'
)
]
)
如果您有一個 requirements.txt
檔案,該檔案應該會是您要在容器中安裝的程式庫清單。source_dir
的路徑可以是相對路徑、絕對路徑或 Amazon S3 URI 路徑。不過,如果您使用 Amazon S3 URI,那麼它必須指向一個 tar.gz 檔案。您可以在為 source_dir
指定的目錄中擁有多個指令碼。如要進一步了解 TensorFlowProcessor
類別,請參閱 Amazon SageMaker Python SDK 中的 TensorFlow 估算器