python加载数据集_python – 用于读取行的最佳HDF5数据集块形...

找到正确的块缓存大小

起初我不想讨论一些一般事情.

知道每个单独的块只能作为一个整体进行读取或写入是非常重要的. h5py的标准块高速缓存大小可以避免过多的磁盘I / O,每个默认值只有1 MB,并且在很多情况下应该增加,这将在后面讨论.

举个例子:

>我们有一个dset的形状(639038,10000),float32(25,5 GB未压缩)

>我们不想写我们的数据列明智的dset [:,i] = arr并按行读取arr = dset [i,:]

>我们为这类工作选择一个完全错误的块状,即(1,10000)

在这种情况下,读取速度不会很差(尽管块大小有点小),因为我们只读取我们正在使用的数据.但是当我们写这个数据集时会发生什么?如果我们访问一列,则写入每个块的一个浮点数.这意味着我们实际上每次迭代都会写入整个数据集(25,5 GB)并每隔一段时间读取整个数据集.这是因为如果你修改了一个块,你必须先读取它,如果它没有被缓存(我认为这里的块高速缓存大小低于25.5 GB).

那么我们可以在这里改进什么?

在这种情况下,我们必须在写/读速度和块高速缓存使用的内存之间进行折衷.

一个假设,它将提供体面/读写速度:

>我们选择一个大小为(100,1000)

>如果我们不想迭代第一维,我们至少需要(1000 * 639038 * 4 – > 2.55 GB)高速缓存以避免如上所述的额外IO开销和(100 * 10000 * 4 – > 0,4 MB).

>因此,我们应该在此示例中提供至少2,6 GB的块数据缓存.这可以通过h5py-cache https://pypi.python.org/pypi/h5py-cache/1.0轻松完成

结论

通常没有正确的块大小或形状,它在很大程度上取决于使用哪个任务.如果不仔细考虑块缓存,就不要选择块大小或形状.在随机读/写方面,RAM的速度比最快的SSD快.

关于你的问题

我只是读取随机行,不正确的chunk-cache-size是你真正的问题.

将以下代码的性能与您的版本进行比较:

import h5py as h5

import time

import numpy as np

import h5py_cache as h5c

def ReadingAndWriting():

File_Name_HDF5='Test.h5'

shape = (639038, 10000)

chunk_shape=(100, 1000)

Array=np.array(np.random.rand(shape[0]),np.float32)

#We are using 4GB of chunk_cache_mem here

f = h5c.File(File_Name_HDF5, 'w',chunk_cache_mem_size=1024**2*4000)

d = f.create_dataset('Test', shape ,dtype='f',chunks=chunk_shape,compression="lzf")

#Writing columns

t1=time.time()

for i in xrange(0,shape[1]):

d[:,i:i+1]=np.expand_dims(Array, 1)

f.close()

print(time.time()-t1)

# Reading random rows

# If we read one row there are actually 100 read, but if we access a row

# which is already in cache we would see a huge speed up.

f = h5c.File(File_Name_HDF5,'r',chunk_cache_mem_size=1024**2*4000)

d = f["Test"]

for j in xrange(0,639):

t1=time.time()

# With more iterations it will be more likely that we hit a already cached row

inds=np.random.randint(0, high=shape[0]-1, size=1000)

for i in xrange(0,inds.shape[0]):

Array=np.copy(d[inds[i],:])

print(time.time()-t1)

f.close()

if __name__ == "__main__":

ReadingAndWriting()

最简单的花式切片形式

我在评论中写道,我在最近的版本中看不到这种行为.我错了.比较以下内容:

import h5py as h5

import time

import numpy as np

import h5py_cache as h5c

def Writing():

File_Name_HDF5='Test.h5'

shape = (63903, 10000)

Array=np.array(np.random.rand(shape[0]),np.float32)

# Writing_1 normal indexing

f = h5c.File(File_Name_HDF5, 'w',chunk_cache_mem_size=1024**3)

d = f.create_dataset('Test', shape ,dtype='f',chunks=(10000,shape[1]/50))

t1=time.time()

for i in xrange(0,shape[1]):

d[:,i:i+1]=np.expand_dims(Array,1)

f.close()

print(time.time()-t1)

# Writing_2 simplest form of fancy indexing

f = h5c.File(File_Name_HDF5, 'w',chunk_cache_mem_size=1024**3)

d = f.create_dataset('Test', shape ,dtype='f',chunks=(10000,shape[1]/50))

t1=time.time()

for i in xrange(0,shape[1]):

d[:,i]=Array

f.close()

print(time.time()-t1)

if __name__ == "__main__":

Writing()

这在我的SSD上为第一个版本提供了10,8秒,对于第二个版本提供了55秒.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值