簡體   English   中英

對於無法容納在RAM中的訓練集,在Theano中管理內存的正確方法是什么?

[英]What is the right way to manage memory in Theano for training sets that cannot fit in RAM?

TL; DR:如何在不占用更多內存的情況下為Theano功能提供更多數據?

我遇到的問題是,使用Theano在GPU上訓練我的ML算法會導致GPU最終用盡內存。 我稍微偏離了教程,因為我的數據集太大而無法完全讀入內存(這對於視頻算法來說也是一個問題,對吧?),所以我只是通過Theano而不是使用索引輸入和更新方案。直接運行ndarrays。

讓我舉一個例子說明我的意思。 在Theano的Logistic回歸教程中,它說要做的事情是:

train_model = theano.function(
    inputs=[index],
    outputs=cost,
    updates=updates,
    givens={
        x: train_set_x[index * batch_size: (index + 1) * batch_size],
        y: train_set_y[index * batch_size: (index + 1) * batch_size]
    }
)

這需要將test_set_xtest_set_y加載到內存中,本教程使用SharedVariable來存儲完整的數據集。

對我來說,數據集非常龐大 (很多千兆字節),這意味着無法一次將其全部加載到內存中,因此我修改了我的數據集以直接獲取數據:

train_model = theano.function(
    inputs=[input, classes], 
    outputs=cost, 
    updates=updates
)

然后我做些模糊的事情:

for count, data in enumerate(extractor):
    observations, labels = data
    batch_cost = train_model(observations, labels)
    logger.debug("Generation %d: %f cost", count, batch_cost)

我想我可能從根本上誤解了如何正確地將數據傳遞到GPU而沒有一些討厭的python垃圾收集骯臟。 看起來這只是在內部模型中占用越來越多的內存,因為在(大)批次之后訓練后,我得到如下錯誤:

Error when tring to find the memory information on the GPU: initialization error
Error freeing device pointer 0x500c88000 (initialization error). Driver report 0 bytes free and 0 bytes total 
CudaNdarray_uninit: error freeing self->devdata. (self=0x10cbbd170, self->devata=0x500c88000)
Exception MemoryError: 'error freeing device pointer 0x500c88000 (initialization error)' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection

如何在不占用更多內存的情況下為Theano函數提供更多數據?

如果數據集不適合存儲在內存中,則想法是取一部分並在每次需要時加載

如果您的數據不適合gpu內存,如經典的lasagne教程中所示,您可以迭代部分數據集 ,稱為minibatches

然后,如果您的數據不適合您的RAM,則需要在每次需要時加載微型批處理。 最好的方法是在分析當前的一個(cpu工作)時, 讓一個單獨的進程加載下一個minibatch (cpu工作)

您可以從AlexNet啟發自己:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM