deep-learning - 如何在 Pytorch 中处理大型 JSON 文件?

标签 deep-learning time-series pytorch

我正在研究一个时间序列问题。不同的训练时间序列数据存储在一个大小为 30GB 的大型 JSON 文件中。在 tensorflow 中,我知道如何使用 TF 记录。 pytorch中是否有类似的方法?

最佳答案

我想 IterableDataset ( docs ) 是你需要的,因为:

  1. 您可能希望在没有随机访问的情况下遍历文件;
  2. 未预先计算 json 中的样本数。

我做了一个最小用法示例,假设数据集文件的每一行本身都是一个 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)

关于deep-learning - 如何在 Pytorch 中处理大型 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55109684/

相关文章:

deep-learning - Caffe中Siamese网络的权重更新

tensorflow - 这对于我的神经网络来说是足够的拓扑吗?

r - 如何读取带有年份和周数的数据文件

python - Tensorflow:针对单个图像中不平衡类的加权稀疏_softmax_cross_entropy

python - 如何在 Keras 中创建这个自定义损失函数并确保它是可微的?

Python ggplot 问题绘制 >8 只股票和图例被截断

r - 计算一段时间内的返回

python - 如何缓存 Pytorch 模型以供未连接互联网时使用?

python-3.x - 如何在 Windows 上指定 pytorch 作为包要求?

deep-learning - 在 Pytorch 中微调预训练模型 MobileNet_V2