【问题标题】:How to make a multi-dims dataset in HDF5?如何在 HDF5 中制作多维度数据集?
【发布时间】:2018-03-25 07:23:47
【问题描述】:

例如,我想做两个数据集,一个是Input,另一个是Output

InputOutput 中的数据是多维度的。

比如

但我注意到h5pyinput_nodeoutput_node 是固定的。

Input =  f.create_dataset('Input',  (3,input_node ),dtype='float', chunks=True)
Output = f.create_dataset('Output', (3,output_node),dtype='float', chunks=True)

但是hdf5不能处理这个,这段代码可以证明这一点

import h5py

X = [[1,2,3,4],[1,2],[1,2,3,4,5,6]]

with h5py.File('myfile.hdf5', "w") as ofile:
    ofile.create_dataset("X", data=X)

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

那么如何在h5py中制作多维度数据集呢?

【问题讨论】:

    标签: python numpy hdf5 h5py


    【解决方案1】:

    我不太明白你的{...} 表示什么。在 Python 中,这些用于字典和集合。 [] 用于列表,() 用于元组。数组形状表示为元组。

    无论如何,您的代码会产生

    In [68]: X
    Out[68]: 
    array([ list([0.6503719194043309, 0.8703218883225239, -1.4139639093161405, 2.3288987644271835, -1.7957516518177206]),
           list([-0.1781710442823114, 0.9591992379396287, -0.6319292685053243]),
           list([0.7104492662861611, -0.8951817329357393, -0.8925882332063567, 1.5587934871464815]),
           list([-1.2384976614455354, 0.9044140291496179, 1.1277220227448401]),
           list([1.1386910680393805, -0.1775792543137636, 1.0567836199711476]),
           list([2.7535019220459707, 0.29518918092088386, -0.32166742909305196, 1.5269788560083497, 0.29633276686886767]),
           list([1.6397535315116918, -0.8839570613086122, -0.4491121599234047, -2.4461439611764333, -0.6884616200199412, -1.1920165045444608]),
           list([1.3240629024597295, 1.170019287452736, 0.5999977019629572, -0.38338543090263366, 0.6030856099472732]),
           list([-0.013529997305716175, -0.7093551284624415, -1.8611980839518099, 0.9165791506693297]),
           list([2.384081118320432, -0.6158201308053464, 0.8802896893269192, -0.7636283160361232])], dtype=object)
    In [69]: y
    Out[69]: array([1, 1, 0, 0, 0, 1, 1, 0, 1, 0])
    

    y 是一个简单的数组。 h5py 保存应该没问题。

    X 是一个对象 dtype 数组,包含不同大小的列表

    In [72]: [len(l) for l in X]
    Out[72]: [5, 3, 4, 3, 3, 5, 6, 5, 4, 4]
    

    h5py 无法保存这种数组。充其量您可以将每个元素写入不同的dataset。它将每个保存为一个数组。

    ....
       for i, item in enumerate(X):
          ofile.create_dataset('name%s'%i, data=item)
    

    【讨论】:

    • 如果将数据保存在hdf5的不同数据集中,那么当使用这个文件作为神经网络的输入时会导致问题
    猜你喜欢
    • 2013-03-25
    • 1970-01-01
    • 2017-12-01
    • 2017-09-06
    • 2015-06-07
    • 2019-06-09
    • 2022-11-14
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多