如何在keras生成器中使用numpy memmap来不超过RAM内存?

问题描述

我试图在生成器内部实现numpy.memmap方法,以使用keras训练神经网络,以便不超过内存RAM限制。我将此post用作参考,但是没有成功。这是我的尝试:

def My_Generator(path,batch_size,tempo,janela):
  samples_per_epoch  = sum(1 for line in np.load(path))
  number_of_batches = samples_per_epoch/batch_size
  #data = np.memmap(path,dtype='float64',mode='r+',shape=(samples_per_epoch,18),order='F')
  data = np.load(path)
  # create a memmap array to store the output
  X_output = np.memmap('output',96,100,17),order='F')
  y_output = np.memmap('output',1),order='F')
  holder = np.zeros([batch_size,18],dtype='float64')
  counter=0

  while 1:
    holder[:] = data[counter:batch_size+counter]
    X,y = input_3D(holder,janela) 
    lenth_X = len(X)
    lenth_y = len(y)
    print(lenth_X,lenth_y)
    y = y.reshape(-1,1)
    X_output[0:lenth_X,:] = X
    y_output[0:lenth_y,:] = y
    counter += 1
    yield X_output[0:lenth_X,:].reshape(-1,10,y_output[0:lenth_y,:]
    #restart counter to yeild data in the next epoch as well
    if counter >= number_of_batches:
        counter = 0

尽管如此,它仍将块保留在RAM内存中,因此在经过某些时间后,它会超过其限制。

谢谢

解决方法

按照此处的方法进行操作:

https://stackoverflow.com/a/61472122/2962979

您也许可以通过每次重构memmap对象来解决您的问题。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...