-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_cuda.lua
97 lines (62 loc) · 1.32 KB
/
test_cuda.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'nn'
require 'rnn'
require 'image'
require 'optim'
require 'loader'
opts = {
gpu = false,
dropout_rate = 0.4,
input_size = 64,
hidden_size = 400,
learning_rate = 1e-4,
momentum = 0.9
}
local class_num = 100
local net = nn.Sequential()
net:add(nn.Dropout(DROPOUT_RATE))
net:add(nn.SplitTable(1))
net:add(nn.BiSequencer(nn.LSTM(opts.input_size, opts.hidden_size)))
net:add(nn.BiSequencer(nn.LSTM(opts.hidden_size * 2, opts.hidden_size)))
output = nn.Sequential()
output:add(nn.Linear(opts.hidden_size * 2, class_num + 1))
output:add(nn.SoftMax())
net:add(nn.Sequencer(output))
if opts.gpu then
require 'cutorch'
require 'cunn'
net:cuda()
cutorch.setDevice(1)
cutorch.manualSeed(450)
else
torch.manualSeed(450)
end
timer = torch.Timer()
im = torch.randn(64, 64)
if opts.gpu then
im = im:cuda()
end
base = timer:time().real
outputTable = net:forward(im)
print(timer:time().real - base)
ims = {}
for i = 1, 10 do
table.insert(ims, torch.randn(64 * i, 64))
end
if opts.gpu then
for i = 1, 10 do
ims[i] = ims[i]:cuda()
end
end
base = timer:time().real
last = base
for i = 1, 10 do
outputTable = net:forward(ims[i])
last = timer:time().real
end
base = last
for i = 1, 10 do
outputTable = net:forward(ims[i])
print((timer:time().real - last))
last = timer:time().real
end
print(last - base)