提问人:user3252955 提问时间:3/16/2022 最后编辑:user3252955 更新时间:3/16/2022 访问量:380
在 AWS SageMaker 中将从 S3 读取的字节数组格式转换为 numpy 数组或张量
Convert bytes array format read from S3 to numpy array or tensor in AWS SageMaker
问:
我已经阅读了一些X_train和y_train,并将它们以内存字节数组的形式上传到 s3,如下所示:
X_train
并且是一维数组,例如:y_train
X_train:
array([[ 2. ],[12.9],[ 1.3],[ 5.1],[ 9.6],[ 8.2],
...
y_train:
array([[ 43525.],[135675.],[ 46205.],[ 66029.],[112635.],
...
import io
import sagemaker
import sagemaker.amazon.common as smcl
sm_session = sagemaker.Session()
bucket = sm_session.default_bucket()
buffer = io.BytesIO()
# writing train data to the form of tensors:
smcl.write_numpy_to_dense_tensor(buffer, X_train, y_train.reshape(-1))
buffer.seek(0)
# Uploading to s3
file_name = 'Train_data'
folder_name = 'Test_folder'
path_to_train_data = os.path.join(folder_name,'train',file_name)
boto3.resource('s3').Bucket(bucket).Object(path_to_train_data).upload_fileobj(buffer)
我想从 s3 中读回它们并将它们调整为原始形式:
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket)
buf = io.BytesIO()
bucket.download_fileobj(key_from_s3, buf)
filecontent_bytes = buf.getvalue()
的输出如下所示:fileconent_byte
b'\n#\xd7\xce(\x00\x00\x00\n\x12\n\x06values\x12\x08\x12\x06\n\x04\x00\x00\x00@\x12\x12\n\...
如何将它们转换为原始形式? 谢谢。
答:
0赞
Ram Vegiraju
3/16/2022
#1
您需要在此处正确解码字节数组。根据您要查找的格式,您需要在此处使用适当的库。例如,对于 numpy 数组,代码块将如下所示。
import numpy as np
s = b'hello world'
np.frombuffer(s, dtype='S1', count=5, offset=6)
array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
文档:https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
另外,需要澄清的是,您并不总是需要缓冲数据格式,具体取决于您使用的算法,有时还允许使用 CSV 或 libsvm,请务必检查您使用的算法可以使用哪些数据格式。
评论
0赞
user3252955
3/16/2022
感谢您的回复。但这不是我问题的答案。我想将数组转换回其原始形式。
评论