[英]How to use numpy memmap inside keras generator to not exceed RAM memory?
我正在嘗試在生成器中實現 numpy.memmap 方法,以使用 keras 訓練神經網絡,以免超出內存 RAM 限制。 我正在使用這篇文章作為參考,但沒有成功。 這是我的嘗試:
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 內存中,以便在一些時期后超過其限制。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.