1、像堆栈的方式保存和读取,可以一个batch保存一次,存储在一个文件中,使用的时候再依次读取出来。
import pickle as pkl
# 保存
for i, batch in enumerate(train_iter):
out = ...
with open('model/train.pkl', 'ab') as f:
pkl.dump(out, f)
# 多次读取
with open('model/train.pkl', 'rb') as f:
while True:
try:
content = pkl.load(f)
except EOFError:
break
# 一次性读取
f = open('model/train.pkl', 'rb')
content = pkl.load(f, encoding='latin1')