理解上的困难
Q2) 如何从S3下载文件?
另外,为什么我们要使用这段代码?
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: # 创建一个S3客户端 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的路径)获取数据,并在训练集群中分发,执行训练过程,返回生成的模型,最后拆除训练基础设施。
您可以在SageMaker控制台中找到此训练作业的结果。