我知道在c
我们可以使用轻松构建复合数据集struct
逐块键入并分配数据。我目前正在实施类似的结构Python
with h5py
.
import h5py
import numpy as np
# we create a h5 file
f = h5py.File("test.h5") # default is mode "a"
# We define a compound datatype using np.dtype
dt_type = np.dtype({"names":["image","feature"],
"formats":[('<f4',(4,4)),('<f4',(10,))]})
# we define our dataset with 5 instances
a = f.create_dataset("test", shape=(5,), dtype=dt_type)
要写入数据,我们可以这样做......
# "feature" array is 1D
a['feature']
输出是
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)
# Write 1s to data field "feature"
a["feature"] = np.ones((5,10))
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)
问题是当我将二维数组“图像”写入文件时。
a["image"] = np.ones((5,4,4))
ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.
我阅读了文档并进行了研究。不幸的是,我没有找到好的解决方案。我了解我们申请group/dataset
模仿这种复合数据,但我真的想保留这种结构。有没有好的方法可以做到这一点?
任何帮助,将不胜感激。谢谢。