2016-10-07 3 views
1

Я использовал логистическую регрессию из примера Torch7 на их сайте, но код не использует графические процессоры: https://github.com/torch/demos/blob/master/logistic-regression/example-logistic-regression.luaTorch7 - логистическая регрессия с помощью GPU - CUDA/cudnn

Мой код практически идентичен, а также. Но разница в том, что я тоже пытаюсь использовать свои графические процессоры. У меня CUDA и библиотека cudnn установлена ​​правильно. Я попытался использовать «cudnn» на очень простом примере, и он сработал: https://github.com/soumith/cudnn.torch

Но это не сработает, когда я попытался преобразовать мою модель логистической регрессии. Я действительно не понимаю ошибку, но она отлично преобразует модель, но при оптимизации с SGD возникает ошибка. Я новичок в факеле, поэтому какая-то помощь будет действительно замечательной! Вот отрывок из моего кода:

require 'torch' 
require 'math' 
require 'svm' 
require 'nn' 
require 'optim' 
require 'cudnn' 

TRAIN_NAME = "sample_train.txt" 
TEST_NAME = "sample_test.txt" 


-- i have: 
num_samples = 10000 
num_features = 500 
- dataset_inputs: feature tensor (dimension: num_samples X num_features) 
- dataset_outputs: labels tensor (dim: num_samples) 
num_labels = 2 

-- create the model 
linLayer = nn.Linear(num_features, num_labels) 
softMaxLayer = nn.LogSoftMax() -- the input and output are a single tensor 
model = nn.Sequential() 
model:add(linLayer) 
model:add(softMaxLayer) 
cudnn.convert(model, cudnn) -- converts the model 
print(model) 

-- loss function to be minimized: negative log-likelihood 
criterion = nn.ClassNLLCriterion() 

---------------------------------------------------------------------- 
-- Train the model (Using SGD) 

x, dl_dx = model:getParameters() 

feval = function(x_new) 
    if x ~= x_new then 
     x:copy(x_new) 
    end 

    _nidx_ = (_nidx_ or 0) + 1 
    if _nidx_ > (#dataset_inputs)[1] then _nidx_ = 1 end 

    local inputs = dataset_inputs[_nidx_] 
    local target = dataset_outputs[_nidx_] 

    dl_dx:zero() 

    -- evaluate the loss function and its derivative wrt x, for that sample 
    local loss_x = criterion:forward(model:forward(inputs), target) 
    model:backward(inputs, criterion:backward(model.output, target)) 

    -- return loss(x) and dloss/dx 
    return loss_x, dl_dx 
end 

-- Parameters train the model using SGD 
sgd_params = { 
    learningRate = 1e-3, 
    learningRateDecay = 1e-4, 
    weightDecay = 0, 
    momentum = 0 
} 


epochs = 1e2 -- number of cycles/iterations over our training data 

print('') 
print('============================================================') 
print('Training with SGD') 
print('') 

for i = 1,epochs do 

    -- this variable is used to estimate the average loss 
    current_loss = 0 

    -- an epoch is a full loop over our training data 
    for i = 1,(#dataset_inputs)[1] do 

     _,fs = optim.sgd(feval,x,sgd_params) -- PROBLEM!! : this function call produces the error 

     current_loss = current_loss + fs[1] 
    end 

    -- report average error on epoch 
    current_loss = current_loss/(#dataset_inputs)[1] 
    print('epoch = ' .. i .. ' of ' .. epochs .. ' current loss = ' .. current_loss) 

end 


-- Then I will use the "model" to predict on test samples 


print("---- DONE -----") 

Это ошибка я получаю:

============================================================ 
Training with SGD 

/home/s43moham/torch/install/bin/luajit: /home/s43moham/torch/install/share/lua/5.1/nn/Container.lua:67: 
In 2 module of nn.Sequential: 
/home/s43moham/torch/install/share/lua/5.1/cudnn/init.lua:125: assertion failed! 
stack traceback: 
[C]: in function 'assert' 
/home/s43moham/torch/install/share/lua/5.1/cudnn/init.lua:125: in function 'toDescriptor' 
...ham/torch/install/share/lua/5.1/cudnn/SpatialSoftMax.lua:39: in function 'createIODescriptors' 
...ham/torch/install/share/lua/5.1/cudnn/SpatialSoftMax.lua:57: in function <...ham/torch/install/share/lua/5.1/cudnn/SpatialSoftMax.lua:56> 
[C]: in function 'xpcall' 
/home/s43moham/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors' 
...e/s43moham/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'forward' 
lr.lua:104: in function 'opfunc' 
/home/s43moham/torch/install/share/lua/5.1/optim/sgd.lua:44: in function 'sgd' 
lr.lua:142: in main chunk 
[C]: in function 'dofile' 
...oham/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk 
[C]: at 0x00406670 

WARNING: If you see a stack trace below, it doesn't point to the place where this error occurred. Please use only the one above. 
stack traceback: 
[C]: in function 'error' 
/home/s43moham/torch/install/share/lua/5.1/nn/Container.lua:67: in function 'rethrowErrors' 
...e/s43moham/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function 'forward' 
lr.lua:104: in function 'opfunc' 
/home/s43moham/torch/install/share/lua/5.1/optim/sgd.lua:44: in function 'sgd' 
lr.lua:142: in main chunk 
[C]: in function 'dofile' 
...oham/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk 
[C]: at 0x00406670 
+0

Вы пытались добавить 'require 'cutorch';' require 'cunn'; ' –

ответ

0

Убедитесь, чтобы преобразовать введенные данные в тип данных CUDA, а также. Попробуйте поменять из

local loss_x = criterion:forward(model:forward(inputs), target) 

с

local loss_x = criterion:forward(model:forward(inputs:cuda()), target) 

Но на самом деле вы должны преобразовать все входы, прежде чем запустить модель, так что не делает его на лету. И.Е. где-то положил inputs = inputs:cuda()

1

Я немного изменил ваш код и, похоже, сработал. Фокус в том, чтобы убедиться, что набор данных, модель и критерий все преобразованы в cuda(), прежде чем продолжить обучение.

require 'torch' 
require 'nn' 
require 'optim' 
require 'cutorch' 
require 'cunn' 
require 'cudnn' 

-- dataset_inputs: feature tensor (dimension: num_samples X num_features) 
-- dataset_outputs: labels tensor (dim: num_samples) 
num_samples = 10000 
num_features = 500 
num_labels = 2 

dataset_inputs = torch.rand(num_samples, num_features):cuda() 
dataset_outputs = torch.Tensor(num_samples):random(1, 2):cuda() 

-- create the model 
model = nn.Sequential() 
model:add(nn.Linear(num_features, num_labels)) 
model:add(nn.LogSoftMax()) 

-- convert model to cuda then cudnn 
model:cuda() 
cudnn.convert(model, cudnn) 
print(model) 

-- loss function to be minimized: negative log-likelihood 
criterion = nn.ClassNLLCriterion():cuda() 

---------------------------------------------------------------------- 
-- Train the model (Using SGD) 

x, dl_dx = model:getParameters() 

feval = function(x_new) 
    if x ~= x_new then 
     x:copy(x_new) 
    end 

    _nidx_ = (_nidx_ or 0) + 1 
    if _nidx_ > (#dataset_inputs)[1] then _nidx_ = 1 end 

    local inputs = dataset_inputs[_nidx_] 
    local target = dataset_outputs[_nidx_] 

    dl_dx:zero() 

    -- evaluate the loss function and its derivative wrt x, for that sample 
    local loss_x = criterion:forward(model:forward(inputs), target) 
    model:backward(inputs, criterion:backward(model.output, target)) 

    -- return loss(x) and dloss/dx 
    return loss_x, dl_dx 
end 

-- Parameters train the model using SGD 
sgd_params = { 
    learningRate = 1e-3, 
    learningRateDecay = 1e-4, 
    weightDecay = 0, 
    momentum = 0 
} 

epochs = 1e2 -- number of cycles/iterations over our training data 

print('') 
print('============================================================') 
print('Training with SGD') 
print('') 

for i = 1,epochs do 
    -- this variable is used to estimate the average loss 
    current_loss = 0 
    -- an epoch is a full loop over our training data 
    for i = 1, (#dataset_inputs)[1] do 
     _,fs = optim.sgd(feval, x, sgd_params) 
     current_loss = current_loss + fs[1] 
    end 

    -- report average error on epoch 
    current_loss = current_loss/(#dataset_inputs)[1] 
    print('epoch = ' .. i .. ' of ' .. epochs .. ' current loss = ' .. current_loss) 
end 

-- Then I will use the "model" to predict on test samples 
print("---- DONE -----") 
Смежные вопросы