S3 文件下载和 estimator.fit() 在此博客 post 中如何工作?

How do S3 file download and estimator.fit() work in this blog post?

理解困难

Q2) 如何从S3下载文件?

来自 The Machine Learning Workflow with SageMaker

以及我们为什么要使用这段代码?

estimator.fit(train_data_location)

正在从 S3 下载文件:

Q2 部分的这个代码块定义了从 S3 下载文件的函数。用户实例化一个 S3 客户端,然后将 S3 URL 传递给 s3.Bucket.download_file() 方法。

def download_from_s3(url):
    """ex: url = s3://sagemakerbucketname/data/validation.tfrecords"""
    url_parts = url.split("/")  # => ['s3:', '', 'sagemakerbucketname', 'data', ...
    bucket_name = url_parts[2]
    key = os.path.join(*url_parts[3:])
    filename = url_parts[-1]
    if not os.path.exists(filename):
        try:
            # Create an S3 client
            s3 = boto3.resource('s3')
            print('Downloading {} to {}'.format(url, filename))
            s3.Bucket(bucket_name).download_file(key, filename)
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == "404":
                print('The object {} does not exist in bucket {}'.format(
                    key, bucket_name))
            else:
                raise

Estimator.fit() 解释:

estimator.fit(train_data_location) 行启动了 SageMaker 的训练过程。当 运行 时,SageMaker 将提供必要的基础设施,从用户指定的位置(此处为 train_data_location 到 Amazon S3 的路径)获取数据并将其分发到训练集群中,执行训练过程,return 生成的模型,并拆除训练基础设施。

您可以在 SageMaker 控制台中找到此训练作业的结果。