如何将 Pytorch 模型直接保存在 s3 Bucket 中?

spa*_*del 1 python amazon-s3 boto3 pytorch

标题说明了一切 - 我想将 pytorch 模型保存在 s3 存储桶中。我尝试的是以下内容:

import boto3

s3 = boto3.client('s3')
saved_model = model.to_json()
output_model_file = output_folder + "pytorch_model.json"
s3.put_object(Bucket="power-plant-embeddings", Key=output_model_file, Body=saved_model)
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用,因为.to_json()仅适用于张量流模型。有谁知道如何在pytorch中做到这一点?

igr*_*nis 7

尝试将模型序列化到缓冲区并将其写入 S3:

buffer = io.BytesIO()
torch.save(model, buffer)
s3.put_object(Bucket="power-plant-embeddings", Key=output_model_file, Body=buffer.getvalue())
Run Code Online (Sandbox Code Playgroud)