【问题标题】:h5f.create_dataset causes MemoryErrorh5f.create_dataset 导致 MemoryError
【发布时间】:2017-04-24 23:36:44
【问题描述】:

我目前正在尝试使用 h5py 存储一个大的 numpy.ndarray。

    print len(train_input_data_interweawed_normalized)
    print train_input_data_interweawed_normalized[0].shape
    raw_input("Something")
    print "Storing Train input"
    h5f = h5py.File(fbank+'train_input_'+str(dim)+'_'+str(total_frames_with_deltas)+'_window_height_'+str(window_height)+'.h5', 'w')
    h5f.create_dataset('train_input', data=np.concatenate(train_input_data_interweawed_normalized,axis=1))
    ##Program chrash here
    h5f.close()

打印输出:

4834302
(45, 1, 8, 3)

但不知何故,程序出现错误消息 MemoryError..

这是什么意思?..没有足够的内存? 根据 htop 的 ram 使用量在它崩溃之前是 11 gb / 15 gb。

所以不可能吗?

还有什么?

【问题讨论】:

  • h5f 调用之外测试np.concatenate() 步骤。它必须在将其写入文件之前创建那个大数组。当你在做的时候,向我们展示这些打印语句的结果。是的,内存错误通常意味着您正在尝试创建一个对您的内存来说太大的数组。 htop 内存测量只是故事的一部分。我们需要知道这个数组应该有多大。
  • 是的.. 似乎是np.concatenate() 引起了问题.. 是否有可能以某种方式连接并分步保存?
  • 看起来train_input_data_interweawed_normalized 是一个很长的列表,包含相对较小的数组(1080 个元素)。而concatenate 正在尝试制作 (45, 4834302, 8, 3) 4d 数组,5G 元素数组。难怪它会出现内存错误。它可能有足够的空间来容纳那些散落在各处的元素,但它不能将它们全部放在一个连续的内存块中。
  • 查看有关分块存储和读/写切片的文档。如果您重新排列数据以便连接或其替代出现在第一个维度上,例如 (40...., 45, 8, 3) 数组,则可能会更好。我没有处理过这样的大数组和文件,所以不能从经验中说出来。

标签: python numpy memory h5py


【解决方案1】:

连接过程还需要至少所有列表元素的大小作为一个连续的内存块。如果您只有 16GB 的 RAM,则内存分配可能会失败。

连接数据然后将其保存到 HDF5 文件在这里没有任何意义。为什么要将数据放在数组列表中?

以下示例显示了如何将列表内容写入您所需大小的 HDF5-Dataset,而无需连接或内存复制。

#get the dimensions
dim_1=len(train_input_data_interweawed_normalized)
dim_2=train_input_data_interweawed_normalized[0].shape

h5f=h5py.File(fbank+'train_input_'+str(dim)+'_'+str(total_frames_with_deltas)+'_window_height_'+str(window_height)+'.h5','w')
# create the dataset (change the datatype if your images have some other type)
#You have to adapt the chunk size to your needs (How do you want to read the data?)
dset_out = f_out.create_dataset('train_input', (dim_2[0],dim_1,dim_2[2],dim_2[3]), chunks=(dim_2[0], 100, dim_2[2], dim_2[3]),dtype='float32')
for i in range(0,dim_1):    
    dset_out[:,i:i+1,:,:]=train_input_data_interweawed_normalized[i]
f_out.close()

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2018-06-23
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多