Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions torch/benchmark_recurrent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Results - Recurrent package
Tested on a Titan-x
```
th recurrent.lua -networkType rnn -hiddenSize 100
Setup : compile + forward/backward x 1
--- 0.036673069000244 seconds ---
Forward:
--- 100000 samples in 7.1649069786072 seconds (13956.907428054 samples/s) ---
Forward + Backward:
--- 100000 samples in 18.566176176071 seconds (5386.1375926401 samples/s) ---


th recurrent.lua -networkType rnn -hiddenSize 500
Setup : compile + forward/backward x 1
--- 0.085377931594849 seconds ---
Forward:
--- 100000 samples in 12.131023168564 seconds (8243.316253702 samples/s) ---
Forward + Backward:
--- 100000 samples in 36.012835979462 seconds (2776.7874612969 samples/s) ---


th recurrent.lua -networkType rnn -hiddenSize 1000
Setup : compile + forward/backward x 1
--- 0.20405006408691 seconds ---

Forward:
--- 100000 samples in 21.969285011292 seconds (4551.8086501038 samples/s) ---
Forward + Backward:
--- 100000 samples in 64.306167125702 seconds (1555.0607355506 samples/s) ---


th recurrent.lua -networkType lstm -hiddenSize 100
Setup : compile + forward/backward x 1
--- 0.34961485862732 seconds ---
Forward:
--- 100000 samples in 26.887173891068 seconds (3719.2429748535 samples/s) ---
Forward + Backward:
--- 100000 samples in 67.390839099884 seconds (1483.881143735 samples/s) ---


th recurrent.lua -networkType lstm -hiddenSize 500
Setup : compile + forward/backward x 1
--- 0.51357793807983 seconds ---
Forward:
--- 100000 samples in 29.653849840164 seconds (3372.2413593724 samples/s) ---
Forward + Backward:
--- 100000 samples in 102.79220104218 seconds (972.83641213161 samples/s) ---
```
83 changes: 83 additions & 0 deletions torch/recurrent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require('torch')
require('cutorch')
require('nn')
require('cunn')
require('recurrent')


-- cutorch.setDevice(2)

cmd = torch.CmdLine()
cmd:text()
cmd:text('Options')
cmd:option('-nSamples', 100000, 'Number of samples')
cmd:option('-networkType', 'lstm', 'Network type')
cmd:option('-inputSize', 100, 'Neural network input size')
cmd:option('-hiddenSize', 100, 'Neural network hidden layer size')
cmd:option('-seqLength', 30, 'Sequence length')
cmd:option('-batchSize', 20, 'Batch size')
cmd:option('-cpu', false, 'Run on CPU')
cmd:text()
for k, v in pairs(cmd:parse(arg)) do _G[k] = v end

local xValues = torch.rand(nSamples, seqLength * inputSize)
local yValues = torch.rand(nSamples, hiddenSize)
if cpu ~= true then
xValues = xValues:cuda()
yValues = yValues:cuda()
end
local xBatches = xValues:split(batchSize, 1)
local yBatches = yValues:split(batchSize, 1)

local a = torch.Timer()
local rnn
if networkType == 'rnn' then
rnn = nn.RNN(inputSize, hiddenSize)
elseif networkType == 'lstm' then
rnn = nn.LSTM(inputSize, hiddenSize)
else
error('Unkown network type!')
end
local rnn = nn.Sequential():add(rnn):add(nn.SelectTable(-1))
local criterion = nn.MSECriterion()
if cpu ~= true then
rnn:cuda()
criterion:cuda()
end

rnn:sequence()
rnn:training()
local input = xBatches[1]:split(inputSize, 2)
criterion:forward(rnn:forward(input), yBatches[1])
rnn:backward(input, criterion:backward(rnn.output, yBatches[1]))
if cpu ~= true then cutorch.synchronize() end
print("Setup : compile + forward/backward x 1")
print("--- " .. a:time().real .. " seconds ---")
rnn:evaluate()

a:reset()
for i = 1, #xBatches do
if (i % 1000 == 0) then
print(i)
end
rnn:forward(xBatches[i]:split(inputSize, 2))
end
if cpu ~= true then cutorch.synchronize() end
print("Forward:")
print("--- " .. nSamples .. " samples in " .. a:time().real .. " seconds (" .. nSamples / a:time().real .. " samples/s) ---")

rnn:training()
a:reset()
for i = 1, #xBatches do
if (i % 1000 == 0) then
print(i)
end
local input = xBatches[i]:split(inputSize, 2)
criterion:forward(rnn:forward(input), yBatches[i])
rnn:zeroGradParameters()
rnn:backward(input, criterion:backward(rnn.output, yBatches[i]))
rnn:updateParameters(0.01)
end
if cpu ~= true then cutorch.synchronize() end
print("Forward + Backward:")
print("--- " .. nSamples .. " samples in " .. a:time().real .. " seconds (" .. nSamples / a:time().real .. " samples/s) ---")