Logistic regression model

reference


In [ ]:
require 'torch'
require 'nn'
require 'optim'
require 'csvigo'

In [ ]:
loaded = csvigo.load{path='ex2data1.txt', mode='raw'}

In [ ]:
data = torch.Tensor{ loaded }[1]:t()[{{1,2}}]:t()

In [ ]:
label = torch.Tensor{ loaded }[1]:t()[3] + 1

Define the model


In [ ]:
linLayer = nn.Linear(2,2)
softMaxLayer = nn.LogSoftMax()
model = nn.Sequential()
model:add(linLayer)
model:add(softMaxLayer)

Define a loss function


In [ ]:
criterion = nn.ClassNLLCriterion()

Train the model (using SGD)


In [ ]:
x, dl_dx = model:getParameters()

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

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

   local inputs = data[_nidx_]
   local target = label[_nidx_]

   dl_dx:zero()

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

   return loss_x, dl_dx
end

In [ ]:
sgd_params = {
   learningRate = 1e-3,
   learningRateDecay = 1e-4,
   weightDecay = 0,
   momentum = 0
}

In [ ]:
epochs = 1e3

In [248]:
print('')
print('============================================================')
print('Training with SGD')
print('')

In [ ]:
for i = 1,epochs do
   current_loss = 0
   for i = 1,(#data)[1] do
      _,fs = optim.sgd(feval,x,sgd_params)
      current_loss = current_loss + fs[1]
   end

   current_loss = current_loss / (#data)[1]
   if i % 100 == 0 then
        print('epoch = ' .. i .. 
         ' of ' .. epochs .. 
         ' current loss = ' .. current_loss)
    end
end

Test the trained model


In [ ]:
function maxIndex(a,b)
    if a>=b then return 1
    else return 2 end
end

In [ ]:
function predictOut(a, b)
    local input = torch.Tensor(2)
    input[1] = a
    input[2] = b
    local logProbs = model:forward(input)
    local probs = torch.exp(logProbs)
    local prob1, prob2 = probs[1], probs[2]
    return maxIndex(prob1, prob2), prob1, prob2
end

In [ ]:
corr = 0
for i = 1, (#data)[1] do
    local prediction = predictOut(data[i][1], data[i][2])
    
    -- print(prediction, label[i])
    if prediction == label[i] then
        corr = corr + 1
    end
end
print (corr / (#data)[1])