【问题标题】:goes out of memory when saving large array with HDF5 (Python, PyTables)使用 HDF5(Python、PyTables)保存大型数组时内存不足
【发布时间】:2017-09-16 10:49:19
【问题描述】:

大家好,

我有一个生成矩阵的 python 进程。它们相互堆叠并保存为张量。这是代码

import tables
h5file = tables.open_file("data/tensor.h5", mode="w", title="tensor")
atom = tables.Atom.from_dtype(n.dtype('int16'))
tensor_shape = (N, 3, MAT_SIZE, MAT_SIZE)

for i in range(N):
    mat = generate(i)
    tensor[i, :, :] = mat

问题是当它达到 8GB 时内存不足。 HDF5格式不应该永远不会出现内存不足吗?就像在需要时将数据从内存移动到磁盘?

【问题讨论】:

    标签: python hdf5 pytables bigdata


    【解决方案1】:

    当您使用 PyTables 时,HDF5 文件会保留在内存中,直到文件关闭(在此处查看更多信息:In-memory HDF5 files)。

    我建议你看看 PyTables 的 appendflush 方法,因为我认为这正是你想要的。请注意,每次循环迭代都刷新缓冲区会显着降低代码的性能,因为需要执行恒定的 I/O。

    同时将文件作为块写入(就像在 pandas 中将数据读入数据帧时一样)可能会激发您的兴趣 - 在此处查看更多信息:PyTables optimization

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2011-10-05
      相关资源
      最近更新 更多