【问题标题】:Dot product of hdf5 arrayhdf5数组的点积
【发布时间】:2016-07-10 13:05:29
【问题描述】:

我正在制作神经网络,其输入数组是 hdf5 数组 X,权重矩阵是 W1。我正在尝试对这两个进行点积,如下所示,并将其存储在其他 hdf5 数组中。

f = h5py.File('z2.hdf5')
self.f['z2'] = np.dot(X,self.W1)

但上面的行给了我 MemoryError。如何解决? hdf5数组如何进行点积?

【问题讨论】:

  • 尝试了解您的数据有多大,如果它确实不适合内存,请尝试类似 dask.pydata.org
  • 它适合内存。问题出在 np.dot() 上。它不能处理巨大的数组。 np.transpose(X) 当 X 的大小非常大时也不起作用 @kakk11
  • 数组有多大,np.dot 显然会创建一个新矩阵,从而要求更多内存...
  • 大约 1.2 GB,我用 hdf5 数组存储它。它是我的扩展。现在我如何用其他矩阵计算它的点积? @kakk11

标签: python hdf5 h5py


【解决方案1】:

来自http://dask.pydata.org/en/latest/array-overview.html

"""Dask Array 使用阻塞算法实现 NumPy ndarray 接口的一个子集,将大数组分割成许多小数组。这让我们可以使用我们所有的内核在大于内存的数组上进行计算。"""

""" dask.array 库支持来自 numpy 的以下接口:

...

张量收缩/点积/矩阵乘法,tensordot"""

用于说明的工作示例,尝试不同的维度来查看 numpy 与 dask 的性能。

import dask as dk
import tables
import numpy as np
from time import time

outpath = "/tmp/"
lenx = 300
leny = 100000
fname = "t{0:03d}_{1:03d}.h5".format(int(lenx/100),int(leny/100))

def write_test_file():
    h5file = tables.open_file(outpath+fname,"w")
    pres = np.random.random((lenx,leny))
    atom = tables.Float64Atom()
    filters = tables.Filters(complevel=6, complib='zlib', shuffle=True)
    print("Writing data")
    t01 = time()
    h5file.create_carray(h5file.root,'pressure',atom,(lenx,leny),filters=filters,obj=pres)
    h5file.flush()
    del pres
    t02 = time()
    lines = np.random.random((leny,lenx))
    h5file.create_carray(h5file.root,"lines",atom,(leny,lenx),filters=filters,obj=lines)
    t03 = time()
    print("Data written",t03-t02,t02-t01)
    h5file.close()

def numpy_dot_test():
    print("Open data")
    t1 = time()
    h5open = tables.open_file(outpath+fname,mode="r")
    pressureObject = h5open.get_node("/", "pressure")
    print(pressureObject.shape)
    linesObject=h5open.get_node("/","lines")
    print(linesObject.shape)
    t2 = time()
    ohoo = np.array(linesObject).dot(np.array(pressureObject))
    t3 = time()
    print(ohoo.shape,np.mean(ohoo))
    print("matmul time:",t3-t2,t2-t1)
    h5open.close()

def dask_dot_test():
    import h5py
    import dask.array as da
    h5open2 = h5py.File(outpath+fname)
    t21=time()
    d1=da.from_array(h5open2["/pressure"],chunks=(100,lenx))
    d2=da.from_array(h5open2["/lines"],chunks=(leny,100))
    t22=time()
    print('d1,d2',d1.shape,d2.shape)
    d1.dot(d2).to_hdf5(outpath+'output.h5','testout')
    t23=time()
    print('ohoo',t23-t22,t22-t21)
    h5open2.close()


write_test_file()
    ## numpy_dot_test()
dask_dot_test()

【讨论】:

  • 好吧,我的 X 为 1.2 GB,w 为 7500 x 10 矩阵。我正在尝试做点积,即 f['z2'] = X.dot(w) 并且已经花费了将近 1 个小时并且没有显示任何结果。这里有什么问题?计算时间与块有什么关系吗? @kakk11
  • 是的,分块很重要。我添加了一个使用 dask 的工作示例,请查看它是否对您有帮助,如果没有,请分享更多您的代码。
  • 嗯,我是初学者,在你的例子中我不知道很多函数。但是,我被困在 "dask_array.compute()" 。它不起作用,它冻结了外壳,我也无法杀死它。我可以给你看代码吗?
  • 是的,您可以显示代码。我知道 compute 命令为什么会挂起的一个原因是它首先创建了太多的计算任务,然后计算图把它逼疯了。您可能可以通过更改分块来摆脱它。
  • 我在使用 dask_array.compute() 时遇到内存错误。我已经分享了我的代码here。您的任何建议或帮助将不胜感激
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2013-07-16
  • 1970-01-01
相关资源
最近更新 更多