tensor
x = torch.rand(4,5)
torch.save(x.to(torch.device('cpu')), "myTensor.pth")
y = torch.load("myTensor.pth")
print(y)
import numpy as np
a = [(u'9000023330249', 1), (u'13142928', 1), (u'9000084906496', 1)]
# 保存
np.save('a.npy',a) # 保存为.npy格式
# 读取
b=np.load('a.npy')
#此时b是numpy array
b=b.tolist()
file = open('file_name.txt','w');
file.write(str(list_variable));
file.close();
list保存为json
with open(os.path.join(path, "text_results_rects-format.json"), "w") as f:
f.write(json.dumps(res))
f.flush()
numpy array
np.save("filename.npy",a)
b = np.load("filename.npy")
dict
jsObj = json.dumps(dict_)
fileObject = open('dict.json', 'w')
fileObject.write(jsObj)
fileObject.close()
如果字典中的项有numpy.array
, 需要.tolist()
一下
读就按照json
文件来读
也可以用torch
保存
保存成pkl
或pth
都行
d = {0:'a', 1:'b'}
torch.save(d, 'test.pkl')
x = torch.load('test.pth')
参考:链接