In [84]:
require 'nn';
require 'csvigo';
In [85]:
-- reload the array
data = torch.Tensor{ csvigo.load{path='ex1data1.txt', mode='raw'} }
Out[85]:
In [86]:
data
In [87]:
trainset = {}
trainset.data = data[1][{{},{1}}]
trainset.label = data[1][{{},{2}}]
In [88]:
trainset
In [89]:
print(#trainset.data)
In [90]:
setmetatable(trainset,
{__index = function(t,i)
return {t.data[i], t.label[i]}
end}
);
function trainset:size()
return self.data:size(1)
end
In [91]:
print(trainset:size())
In [92]:
print(trainset[33])
Out[92]:
In [93]:
net = nn.Sequential()
net:add(nn.Linear(1, 1))
In [94]:
criterion = nn.MSECriterion()
In [95]:
trainer = nn.StochasticGradient(net, criterion)
trainer.learningRate = 0.001
trainer.maxIteration = 100
In [96]:
trainer:train(trainset)
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
Out[96]:
In [97]:
predicted = net:forward(trainset.data[33])
In [100]:
print(predicted, trainset.label[33])
Out[100]:
In [99]:
correct = 0
for i=1,trainset:size() do
local predicted = net:forward(trainset.data[i])
sqr = (predicted - trainset.label[i])*(predicted - trainset.label[i])
correct = correct + sqr
end
print(correct / trainset:size())
Out[99]:
In [101]:
net
Out[101]:
In [104]:
net.modules[1].weight
Out[104]:
In [105]:
net.modules[1].bias
Out[105]:
In [ ]: