GPUに格納する要素の数を知っているとすれば、それらの要素を格納するために必要なメモリの量を簡単に計算できます。あなたはNVIDIAのグラフィックスカードを使用している場合
>>> Data will need 1.22 GBs of free memory
、あなたは、あなたのマシンにCUDAをインストールしました:
簡単な例:
import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
dataPoints = np.random.random((5000, 256 * 256)).astype(T.config.floatX)
#float32 data type requires 4 bytes
sizeinGBs = 5000 * 256 * 256 * 4/1024./1024/1024 + (some small over-head constant)
print "Data will need %2f GBs of free memory" % sizeInGB
オーバーヘッド定数と仮定すると、0が印刷されています次のコード行を使用して、GPUの空きメモリの総量を簡単に取得できます。
import theano.sandbox.cuda.basic_ops as sbcuda
import numpy as np
import theano.tensor as T
T.config.floatX = 'float32'
GPUFreeMemoryInBytes = sbcuda.cuda_ndarray.cuda_ndarray.mem_info()[0]
freeGPUMemInGBs = GPUFreeMemoryInBytes/1024./1024/1024
print "Your GPU has %s GBs of free memory" % str(freeGPUMemInGBs)
#An operation is to be executed below
testData = shared(np.random.random((5000, 256 * 256)).astype(T.config.floatX), borrow = True)
print "The tasks above used %s GBs of your GPU memory. The available memory is %s GBs" % (str(freeGPUMemInGBs - GPUFreeMemoryInBytes/1024./1024/1024), str(GPUFreeMemoryInBytes/1024./1024/1024))
はその後、出力は(ここでは私のマシンのための)次の形式である:
>>> Your GPU has 11.2557678223 GBs of free memory
>>> The tasks above used 1.22077941895 GBs of your GPU memory. The available memory is 10.0349884033 GBs
空きメモリの量を監視し、あなたがより良いGPUのメモリを使用することができ、あなたのモデル/データのサイズを算出することにより。ただし、予期せずMemoryError
が発生する可能性があるので、memory fragmentationの問題に注意してください。
恐ろしい答え、ありがとう! – Will
したがって、最も信頼性の高い方法は、あなたとあなたの意志の両方をマージするように思えます。新しいモデルを実行する前に、さまざまな長さのシーケンスのメモリ使用量を測定し、それを使用して他のシーケンス長に使用されるメモリ量を予測し、最適なバッチサイズを決定します。ありがとう –