【问题标题】:h5py - reshape dataset like numpy.reshape()h5py - 重塑数据集,如 numpy.reshape()
【发布时间】:2019-10-05 00:34:07
【问题描述】:

我想重塑我的 h5py 数据集,就像我可以使用 numpy.reshape() 一样。 以下代码仅在我在代码开头使用 numpy.array() 时才有效。但这仅适用于小数据集,如果我采用更大的数据集,则会破坏我的记忆。

import h5py
import numpy as np

#load data
h5py_data_path = 'any\path\to\h5pyData\training.data.h5'
t_data = h5py.File(h5py_data_path,'r')
training_data = t_data['training.data']
######################################
#### Don't want to have this (blows up my memory) ####
training_data = np.array(training_data)
######################################

print('training_data    ',training_data.shape)
#out: training_data     (10203, 5, 341)

#reshape data
######################################
#### That works, but only with upper Numpy Code ####
training_data = training_data.reshape(training_data.shape[0], 1, 5, 341)
######################################

print('training_data    ',training_data.shape)
#out: training_data     (10203, 1, 5, 341)

h5py 中是否有任何本地方式可以以任何其他工作方式重塑它?

【问题讨论】:

  • h5py 文档的哪一部分您不明白?
  • training_data[0:n] 将数据集的一部分加载到内存中。
  • HDF5 文件是一个磁盘数据结构。因此,没有原生的 .reshape() 方法。但是,有一个 .resize() 方法可以添加到现有数据集。当您以 numpy 数组的形式访问数据集时,您将获得磁盘数据的视图(在内存中)。在您的示例中,您正在向数组添加一个维度(从 (10203, 5, 341) 到 (10203, 1, 5, 341))。你的意图是什么?如果你真的需要重塑你的训练数据,你可以读取数据集,重塑它并写入新的数据集。新数据集可以放在当前文件中,也可以放在新文件中。
  • training_data = t_data['training_data'].value.reshape(shape values) 有效,但仍然存在内存问题。将数据重新准备成新的形状似乎很不错……
  • 是的,这是一个需要重塑的大数组。注意 .value 在 h5py 中已弃用。现在推荐的方法就像 numpy 数组切片:training_data = t_data['training_data'][:](访问整个数组)。它将接受 .reshape() 方法。

标签: python python-3.x numpy h5py


【解决方案1】:

虽然有一个很好的功能,但 H5py 文档是明确的:数据集排名(维数)在创建时是固定的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2018-04-08
    • 2013-04-19
    • 2017-06-12
    相关资源
    最近更新 更多