File size: 626 Bytes
2abfccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
import io
import json
from petrel_client.client import Client

client = Client('./conf/petreloss.conf')

# Pytorch save & load

data = torch.tensor([0, 1, 2, 3])
tensor_url = 's3://bucket1/tensor_data'

with io.BytesIO() as f:
    torch.save(data, f)
    client.put(tensor_url, f.getvalue())

with io.BytesIO(client.get(tensor_url)) as f:
    data2 = torch.load(f)

assert torch.equal(data, data2)


# Json dumps & loads

data = [0, 1, 2, 3]
json_data_url = 's3://bucket1/json_data'

client.put(json_data_url, json.dumps(data).encode('utf-8'))

data2 = json.loads(client.get(json_data_url))

assert data == data2