【问题标题】:How to use numpy memmap inside keras generator to not exceed RAM memory?如何在 keras 生成器中使用 numpy memmap 不超过 RAM 内存?
【发布时间】:2020-12-14 12:09:30
【问题描述】:

我正在尝试在生成器中实现 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', dtype='float64', shape=(samples_per_epoch, 96, 100, 17), mode='r+', order='F')
  y_output = np.memmap('output', dtype='float64', shape=(samples_per_epoch, 1), mode='r+', 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, tempo, 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, 96, 10, 10, 17), y_output[0:lenth_y, :]
    #restart counter to yeild data in the next epoch as well
    if counter >= number_of_batches:
        counter = 0

尽管如此,它仍将块保存在 RAM 内存中,因此在一些 epoch 之后它会超过其限制。

谢谢

【问题讨论】:

    标签: python numpy keras generator numpy-memmap


    【解决方案1】:

    按照这里的方法:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 1970-01-01
      • 2017-12-21
      • 2016-01-24
      • 2020-03-27
      • 2019-09-24
      • 2019-03-09
      • 2023-03-15
      • 2014-12-17
      相关资源
      最近更新 更多