Skip to content

Instantly share code, notes, and snippets.

@dsdenes
Created August 17, 2016 16:20
Show Gist options
  • Save dsdenes/b739d48befb37c9204e3d21367c7d455 to your computer and use it in GitHub Desktop.
Save dsdenes/b739d48befb37c9204e3d21367c7d455 to your computer and use it in GitHub Desktop.
require 'rnn'
batchSize = 1000
rho = 999999
hiddenSize = 7
nIndex = 10
iters = 10
learningRate = 0.1
inputSize = 5
dataSize = 18563460
nn.FastLSTM.bn = true
rnn = nn.FastLSTM(inputSize, inputSize)
rnn = nn.Sequencer(rnn)
sequence = torch.Tensor(dataSize, inputSize)
criterion = nn.MSECriterion()
criterion = nn.SequencerCriterion(criterion)
print("Model :")
print(rnn)
offsets = torch.LongTensor(batchSize):random(1, dataSize)
-- training
minErr = inputSize -- report min error
minK = 0
avgErrs = torch.Tensor(iters):fill(0)
for k = 1, iters do
-- 1. create a sequence of rho time-steps
local inputs, targets = {}, {}
for step=1,rho do
-- batch of inputs
inputs[step] = inputs[step] or sequence.new()
inputs[step]:index(sequence, 1, offsets)
-- batch of targets
offsets:add(1) -- increase indices by 1
offsets[offsets:gt(opt.dataSize)] = 1
targets[step] = targets[step] or sequence.new()
targets[step]:index(sequence, 1, offsets)
end
-- 2. forward sequence through rnn
rnn:zeroGradParameters()
local outputs = rnn:forward(inputs)
local err = criterion:forward(outputs, targets)
print('Iter: ' .. k .. ' Err: ' .. err)
avgErrs[k] = err
if avgErrs[k] < minErr then
minErr = avgErrs[k]
minK = k
end
-- 3. backward sequence through rnn (i.e. backprop through time)
local gradOutputs = criterion:backward(outputs, targets)
local gradInputs = rnn:backward(inputs, gradOutputs)
-- 4. update
rnn:updateParameters(learningRate)
end
print('min err: ' .. minErr .. ' on iteration ' .. minK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment