Torch7 - Logistic Regression using GPU - CUDA/cudnn
<p>I have used the logistic regression from the Torch7 example in their website but the code doesn't use GPUs: 
<a href="https://github.com/torch/demos/blob/master/logistic-regression/example-logistic-regression.lua" rel="nofollow">https://github.com/torch/demos/blob/master/logistic-regression/example-logistic-regression.lua</a></p>

<p>My code is almost identical as well. But the difference is I'm trying to use my GPUs as well. I have CUDA and the 'cudnn' library installed correctly. I have tried using 'cudnn' on a very simple example and it worked:
<a href="https://github.com/soumith/cudnn.torch" rel="nofollow">https://github.com/soumith/cudnn.torch</a></p>

<p>But it doesn't work when I tried to convert my Logistic Regression model. I don't really understand the error but it converts the model fine but throws an error during optimization with SGD. I am new to Torch so some help would be really great! Here is a snippet of my code:</p>

<pre><code>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_ &gt; (#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 -----")
</code></pre>

<p>This is the error I am getting:</p>

<pre><code>============================================================
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 &lt;...ham/torch/install/share/lua/5.1/cudnn/SpatialSoftMax.lua:56&gt;
[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
</code></pre>