Python 如何使用Boto3 SDK为SageMaker培训作业指定源目录和入口点?用例是通过Lambda调用开始培训
我一直在SageMaker笔记本实例上使用SageMaker Python SDK运行培训作业,并在本地使用IAM凭据。他们工作得很好,但我希望能够通过AWS Lambda+Gateway开始培训工作 Lambda不支持SageMaker SDK(高级SDK),因此我不得不在Lambda处理程序中从Python 如何使用Boto3 SDK为SageMaker培训作业指定源目录和入口点?用例是通过Lambda调用开始培训,python,amazon-web-services,tensorflow,boto3,amazon-sagemaker,Python,Amazon Web Services,Tensorflow,Boto3,Amazon Sagemaker,我一直在SageMaker笔记本实例上使用SageMaker Python SDK运行培训作业,并在本地使用IAM凭据。他们工作得很好,但我希望能够通过AWS Lambda+Gateway开始培训工作 Lambda不支持SageMaker SDK(高级SDK),因此我不得不在Lambda处理程序中从boto3使用SageMaker客户端,例如 sagemaker=boto3.client('sagemaker')) 假设这个boto3服务级别SDK会给我100%的控制权,但我找不到用于指定源目
boto3
使用SageMaker客户端,例如
sagemaker=boto3.client('sagemaker'))
假设这个boto3服务级别SDK会给我100%的控制权,但我找不到用于指定源目录和入口点的参数或配置名称。我正在运行一个定制的培训任务,需要在飞行中生成一些数据(使用Keras生成器)
下面是我的SageMaker SDK调用的一个示例
tf_estimator = TensorFlow(base_job_name='tensorflow-nn-training',
role=sagemaker.get_execution_role(),
source_dir=training_src_path,
code_location=training_code_path,
output_path=training_output_path,
dependencies=['requirements.txt'],
entry_point='main.py',
script_mode=True,
instance_count=1,
instance_type='ml.g4dn.2xlarge',
framework_version='2.3',
py_version='py37',
hyperparameters={
'model-name': 'my-model-name',
'epochs': 1000,
'batch-size': 64,
'learning-rate': 0.01,
'training-split': 0.80,
'patience': 50,
})
通过调用fit()
输入\u通道={
“训练”:训练输入路径,
}
tf_估计器拟合(输入=输入通道)
是一个S3URI,用于查找mysource\u dir
,其中包含要 进行训练src.zip.gz
进入点是培训开始的地方。TensorFlow容器只运行
python main.py
是一个S3前缀,如果要运行,可以将培训源代码上载到该前缀 本培训使用本地模型和脚本在本地进行code\u location
是一个S3URI,培训作业将在其中上载模型工件output\u path
sagemaker=boto3.client('sagemaker'))
sagemaker.create_training_job(
TrainingJobName='tf-training-job-from-lambda',
Hyperparameters={}#与上面相同的字典,
算法规范={
“培训图像”:763104351884.dkr.ecr.us-west-1.amazonaws.com/tensorflow培训:2.3.1-gpu-py37-cu110-ubuntu18.04,
“TrainingInputMode”:“文件”,
“EnableSageMakerMetricsTimeSeries”:真
},
RoleArn='我的执行角色在这里',
InputDataConfig=[
{
“ChannelName”:“train”,
“数据源”:{
“S3DataSource”:{
“S3DataType”:“S3Prefix”,
“S3Uri”:训练输入路径,
“S3DataDistributionType”:“FullyReplicated”
}
},
“压缩类型”:“无”,
“RecordWrapperType”:“无”,
'InputMode':'File',
}
],
OutputDataConfig={
“S3OutputPath”:训练输出路径,
}
资源配置={
“InstanceType”:“ml.g4dn.2xlarge”,
“InstanceCount”:1,
“VolumeSizeInGB”:16
}
停止条件={
“MaxRuntimeInSeconds”:600#10分钟用于测试
}
)
从上面的配置中,SDK接受培训输入和输出位置,但哪个配置字段允许用户指定源代码目录和入口点?您可以将源目录传递给超参数,如下所示:
response = sm_boto3.create_training_job(
TrainingJobName=f"{your job name}"),
HyperParameters={
'model-name': 'my-model-name',
'epochs': 1000,
'batch-size': 64,
'learning-rate': 0.01,
'training-split': 0.80,
'patience': 50,
"sagemaker_program": "script.py", # this is where you specify your train script
"sagemaker_submit_directory": "s3://" + bucket + "/" + project + "/" + source, # your s3 URI like s3://sm/tensorflow/source/sourcedir.tar.gz
},
AlgorithmSpecification={
"TrainingImage": training_image,
...
},
注意:否则请确保它是xxx.tar.gz。否则Sagemaker将抛出错误
指