deep-learning - 如何在 Pytorch 中处理大型 JSON 文件?
问题描述
我正在研究时间序列问题。不同的训练时间序列数据存储在一个大小为 30GB 的大型 JSON 文件中。在 tensorflow 中,我知道如何使用 TF 记录。pytorch中是否有类似的方法?
解决方案
我想IterableDataset
(docs)是你需要的,因为:
- 您可能想在没有随机访问的情况下遍历文件;
- jsons 中的样本数量不是预先计算的。
我做了一个最小的使用示例,假设数据集文件的每一行都是 json 本身,但是您可以更改逻辑。
import json
from torch.utils.data import DataLoader, IterableDataset
class JsonDataset(IterableDataset):
def __init__(self, files):
self.files = files
def __iter__(self):
for json_file in self.files:
with open(json_file) as f:
for sample_line in f:
sample = json.loads(sample_line)
yield sample['x'], sample['time'], ...
...
dataset = JsonDataset(['data/1.json', 'data/2.json', ...])
dataloader = DataLoader(dataset, batch_size=32)
for batch in dataloader:
y = model(batch)
推荐阅读
- bash - 如何在 Bash 中找到包含某些文件扩展名的所有文件夹?
- python - 如何比较数据框中不同年份的月度值变化,Pandas/Python
- android - 我应该采用哪种格式和字体大小?
- javascript - 为什么我的 Firestore 文档包含对象的名称?
- android - 使用 Gson 和 Volley 删除额外的括号
- python - 从科学格式的文本文件中读取两列并用 Python 绘图
- java - 无法在 Android O 中构建 NotificationCompat.Builder
- javascript - 用不同的变量命名变量
- php - SQL:如何在两个表中使用 REPLACE INTO 并且只有特定值匹配?
- django - 如何在 django-heroku 中部署媒体文件?