In [1]:
require 'nn';
require 'rnn';
matio = require 'matio'
In [2]:
data = matio.load('ex4data1.mat')
trainset = {}
trainset.data = data.X
trainset.label = data.y[{ {}, 1 }]
In [3]:
setmetatable(trainset,
{__index = function(t,i)
return {t.data[i], t.label[i]}
end}
);
function trainset:size()
return self.data:size(1)
end
In [4]:
mean = {}
stdv = {}
for i=1,400 do
mean[i] = trainset.data[{ {},{i} }]:mean()
stdv[i] = trainset.data[{ {}, {i} }]:std()
--print(i .. 'th mean: ' .. mean[i])
--print(i .. 'th std dev: ' .. stdv[i])
trainset.data[{ {},{i} }]:add(-mean[i])
if stdv[i] ~= 0 then
trainset.data[{ {},{i} }]:div(stdv[i])
end
end
In [5]:
batchSize = 5000
rho = 5
hiddenSize = 10
nIndex = 20
nClass = 10
In [6]:
rnn = nn.Sequential()
r = nn.Recurrent(
hiddenSize, nn.Linear(nIndex, hiddenSize),
nn.Linear(hiddenSize, hiddenSize), nn.Sigmoid(),
rho
)
rnn:add(r)
rnn:add(nn.Linear(hiddenSize, nClass))
rnn:add(nn.LogSoftMax())
rnn = nn.Sequencer(rnn)
In [7]:
criterion = nn.SequencerCriterion(nn.ClassNLLCriterion())
In [17]:
lr = 0.1
i = 1
In [20]:
prev = 100
for epoch = 1,1e2 do
local inputs, targets = {}, {}
for step=1,rho do
table.insert(inputs, trainset.data[{{},{i, i+nIndex-1}}])
table.insert(targets, trainset.label[{{}}])
i = i+20
if i+nIndex-1 > 400 then
i = 1
break
end
end
local outputs = rnn:forward(inputs)
local err = criterion:forward(outputs, targets)
if epoch%1 == 0 then print(epoch, err/rho, i) end
local gradOutputs = criterion:backward(outputs, targets)
rnn:backward(inputs, gradOutputs)
rnn:updateParameters(lr)
rnn:zeroGradParameters()
if prev<err and lr > 0.1 then
print("prev: ", prev, "cur: ", err, "lr", lr, "->", lr*0.5)
lr = lr * 0.5
end
prev = err
end
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
Out[20]:
In [21]:
correction = {}
trainsize = 100 -- trainset:size()
for i=20,38 do
correction[i] = 0
end
for i=1,trainsize do
local answer = trainset.label[i]
local inputs = {}
for step=0,38 do
table.insert(inputs, trainset.data[{{i},{step*10+1, step*10+20}}])
end
local prediction = rnn:forward(inputs)
for d=20,38 do
guess = prediction[d][{1,{}}]
local confidences, indices = torch.max(guess)
-- if i%100 == 1 then print(answer, guess, indices[1]) end
if (answer == indices) then
correction[d] = correction[d] + 1
end
end
end
for i=20,38 do
print(i, " = ", correction[i], 100*correction[i]/trainsize .. '%')
end
Out[21]:
Out[21]:
In [ ]: