In [54]:
require 'nn'
require 'csvigo'
In [74]:
loaded = csvigo.load{path='ex2data1.txt', mode='raw'}
Out[74]:
In [75]:
trainset = {}
trainset.data = torch.Tensor(loaded)[{ {},{1,2} }]
trainset.label = torch.Tensor(loaded)[{ {},3 }]
trainset.label = trainset.label + 1
In [76]:
trainset
Out[76]:
In [77]:
-- ignore setmetatable for now, it is a feature beyond the scope of this tutorial. It sets the index operator.
setmetatable(trainset,
{__index = function(t,i)
return {t.data[i], t.label[i]}
end}
);
function trainset:size()
return self.data:size(1)
end
In [78]:
print(trainset[33])
Out[78]:
In [85]:
net = nn.Sequential()
net:add(nn.Linear(2,2))
net:add(nn.Sigmoid())
In [86]:
criterion = nn.ClassNLLCriterion()
In [87]:
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.01
trainer.maxIteration = 100 -- just do 5 epochs of training.
In [88]:
trainer:train(trainset)
Out[88]:
Out[88]:
Out[88]:
Out[88]:
Out[88]:
Out[88]:
In [90]:
predicted = net:forward(trainset.data[33])
print(predicted, trainset.label[33])
Out[90]:
In [91]:
correct = 0
for i=1,100 do
local groundtruth = trainset.label[i]
local prediction = net:forward(trainset.data[i])
local confidences, indices = torch.sort(prediction, true)
if groundtruth == indices[1] then
correct = correct + 1
end
end
In [92]:
print(correct .. '%')
Out[92]:
In [93]:
class_performance = {0,0}
for i=1,100 do
local groundtruth = trainset.label[i]
local prediction = net:forward(trainset.data[i])
local confidences, indices = torch.sort(prediction, true)
if groundtruth == indices[1] then
class_performance[groundtruth] = class_performance[groundtruth]+1
end
end
In [94]:
for i=1,2 do
print(i, class_performance[i]*2 .. '%')
end
Out[94]: