如何处理Torch中的GPU内存泄漏问题?

我的机器的GPU有2GB的内存。第一次运行以下代码时没有错误。然而,第二次运行代码时,我遇到了内存错误。作为一个临时解决方案,我唯一能做的就是使用torch.Tensor.float()将数据转换为float32。然而,问题仍然存在,内存占用在进程完成后或进程在运行时被终止后并未释放,机器的RAM也存在这种情况。如何防止Torch中的内存泄漏或释放内存?

require 'nn'require 'image'require 'cunn'require 'paths'collectgarbage(); collectgarbage()if (not paths.filep("cifar10torchsmall.zip")) then    os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip')    os.execute('unzip cifar10torchsmall.zip')endtrainset = torch.load('cifar10-train.t7')testset = torch.load('cifar10-test.t7')classes = {'airplane', 'automobile', 'bird', 'cat',           'deer', 'dog', 'frog', 'horse', 'ship', 'truck'}setmetatable(trainset,     {__index = function(t, i)                     return {t.data[i], t.label[i]}                 end});trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.function trainset:size()     return self.data:size(1) endmean = {} -- store the mean, to normalize the test set in the futurestdv  = {} -- store the standard-deviation for the futurefor i=1,3 do -- over each image channel    mean[i] = trainset.data[{ {}, {i}, {}, {}  }]:mean() -- mean estimation    print('Channel ' .. i .. ', Mean: ' .. mean[i])    trainset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtraction    stdv[i] = trainset.data[{ {}, {i}, {}, {}  }]:std() -- std estimation    print('Channel ' .. i .. ', Standard Deviation: ' .. stdv[i])    trainset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scalingendtestset.data = testset.data:double()   -- convert from Byte tensor to Double tensorfor i=1,3 do -- over each image channel    testset.data[{ {}, {i}, {}, {}  }]:add(-mean[i]) -- mean subtraction        testset.data[{ {}, {i}, {}, {}  }]:div(stdv[i]) -- std scalingendtrainset.data = trainset.data:cuda()testset.data = testset.data:cuda()net = nn.Sequential()net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 3 input image channels, 6 output channels, 5x5 convolution kernelnet:add(nn.ReLU())                       -- non-linearity net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.net:add(nn.SpatialConvolution(6, 16, 5, 5))net:add(nn.ReLU())                       -- non-linearity net:add(nn.SpatialMaxPooling(2,2,2,2))net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)net:add(nn.ReLU())                       -- non-linearity net:add(nn.Linear(120, 84))net:add(nn.ReLU())                       -- non-linearity net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)net:add(nn.LogSoftMax())  net = net:cuda()criterion = nn.ClassNLLCriterion()criterion = criterion:cuda()pred = net:forward(trainset.data)outputEr = criterion:forward(pred, trainset.label:cuda())net:zeroGradParameters()outputGrad = criterion:backward(pred, trainset.label:cuda())collectgarbage()inputGrad = net:backward(trainset.data, outputGrad)

附带问题:为什么Torch初始化网络参数时使用双精度,尽管GPU在计算双精度操作时速度较慢,而且几乎所有神经网络应用实际上不需要64位参数值?我如何初始化一个使用float(32位)参数向量的模型?

我找到了附带问题的答案。你可以在代码开始时使用以下命令轻松地将Torch的默认数据类型设置为float:

torch.setdefaulttensortype('torch.FloatTensor')

回答:

通过将机器上的CUDA从6.5升级到7.5,我几乎解决了这个问题。现在,大多数情况下,当程序在运行时崩溃时,GPU内存会被释放。然而,有时仍然不会发生这种情况,我不得不重启机器。

此外,为了确保程序在成功运行后清除GPU内存,我会执行以下操作:

net = niltrainset = niltestset = nilpred = nilinputGrad = nilcriterion = nilcollectgarbage()

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注