Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算Theano中的GPU内存使用率?_Python_Memory Management_Machine Learning_Gpgpu_Theano - Fatal编程技术网

Python 如何计算Theano中的GPU内存使用率?

Python 如何计算Theano中的GPU内存使用率?,python,memory-management,machine-learning,gpgpu,theano,Python,Memory Management,Machine Learning,Gpgpu,Theano,我正在试验不同的Theano模型,并使用不断增加序列长度的课程。如何提前预测任何给定序列长度和模型的批量大小,以填充GPU内存 更糟糕的是,如果我不小心使用了太多的内存,我会得到一个MemoryError,GPU上的内存不会被释放,需要我重新启动进程以释放内存,并在尝试新的批处理大小之前丢失网络。因为这个错误是不可恢复的,所以很难只增加批处理大小直到出现异常,然后再返回。看来Theano没有任何内置的方法来估计模型的内存大小。最好的方法是创建一个已知大小的模型子集,并使用Theano手册中描述的

我正在试验不同的Theano模型,并使用不断增加序列长度的课程。如何提前预测任何给定序列长度和模型的批量大小,以填充GPU内存


更糟糕的是,如果我不小心使用了太多的内存,我会得到一个MemoryError,GPU上的内存不会被释放,需要我重新启动进程以释放内存,并在尝试新的批处理大小之前丢失网络。因为这个错误是不可恢复的,所以很难只增加批处理大小直到出现异常,然后再返回。

看来Theano没有任何内置的方法来估计模型的内存大小。最好的方法是创建一个已知大小的模型子集,并使用Theano手册中描述的内存估计技术

我们还需要考虑对象在GPU中的表示方式(例如,我们使用的是
float32
还是
float64
,以及每个对象在GPU中占用多少字节)


一旦可以估计小模型的大小,就可以以合理的精度将这些估计值投影到更大模型的大小。您应该能够编写自己的内存估计函数,该函数可以将许多特征和观察值、张量或图形节点作为参数,并返回内存使用量。

假设您知道要存储在GPU上的元素数量,则可以轻松计算存储这些元素所需的内存量

一个简单的例子:

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,则将打印:

>>> Data will need 1.22 GBs of free memory
如果您使用的是NVIDIA图形卡,并且在您的计算机上安装了CUDA,那么您可以使用以下代码行轻松获得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))
然后输出为以下格式(此处适用于我的机器):


通过监控可用内存量并计算模型/数据的大小,您可以更好地使用GPU内存。但是,要注意这个问题,因为它可能会导致意外的记忆错误。

所以听起来最可靠的方法是合并你和威尔的建议。在运行一个新模型之前,我将测量不同长度序列的内存使用情况,并使用它来预测其他序列长度将使用多少内存,并确定最佳批量大小。谢谢
>>> 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