-
Notifications
You must be signed in to change notification settings - Fork 36
/
opensource_utils.lua
117 lines (95 loc) · 3.71 KB
/
opensource_utils.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'nn'
require 'cunn'
paths.dofile('LinearNB.lua')
function getFreeGPU()
-- select the most available GPU to train
local nDevice = cutorch.getDeviceCount()
local memSet = torch.Tensor(nDevice)
for i=1, nDevice do
local tmp, _ = cutorch.getMemoryUsage(i)
memSet[i] = tmp
end
local _, curDeviceID = torch.max(memSet,1)
return curDeviceID[1]
end
function build_model(opt, manager_vocab)
-- function to build up baseline model
local model
if opt.method == 'BOW' then
model = nn.Sequential()
local module_tdata = nn.LinearNB(manager_vocab.nvocab_question, opt.embed_word)
model:add(module_tdata)
model:add(nn.Linear(opt.embed_word, manager_vocab.nvocab_answer))
elseif opt.method == 'IMG' then
model = nn.Sequential()
model:add(nn.Linear(opt.vdim, manager_vocab.nvocab_answer))
elseif opt.method == 'BOWIMG' then
model = nn.Sequential()
local module_tdata = nn.Sequential():add(nn.SelectTable(1)):add(nn.LinearNB(manager_vocab.nvocab_question, opt.embed_word))
local module_vdata = nn.Sequential():add(nn.SelectTable(2))
local cat = nn.ConcatTable():add(module_tdata):add(module_vdata)
model:add(cat):add(nn.JoinTable(2))
model:add(nn.LinearNB(opt.embed_word + opt.vdim, manager_vocab.nvocab_answer))
else
print('no such methods')
end
model:add(nn.LogSoftMax())
local criterion = nn.ClassNLLCriterion()
criterion.sizeAverage = false
model:cuda()
criterion:cuda()
return model, criterion
end
function initial_params()
local gpuidx = getFreeGPU()
print('use GPU IDX=' .. gpuidx)
cutorch.setDevice(gpuidx)
local cmd = torch.CmdLine()
-- parameters for general setting
cmd:option('--savepath', 'model')
-- parameters for the visual feature
cmd:option('--vfeat', 'googlenetFC')
cmd:option('--vdim', 1024)
-- parameters for data pre-process
cmd:option('--thresh_questionword',6, 'threshold for the word freq on question')
cmd:option('--thresh_answerword', 3, 'threshold for the word freq on the answer')
cmd:option('--batchsize', 100)
cmd:option('--seq_length', 50)
-- parameters for learning
cmd:option('--uniformLR', 0, 'whether to use uniform learning rate for all the parameters')
cmd:option('--epochs', 100)
cmd:option('--nepoch_lr', 100)
cmd:option('--decay', 1.2)
cmd:option('--embed_word', 1024,'the word embedding dimension in baseline')
-- parameters for universal learning rate
cmd:option('--maxgradnorm', 20)
cmd:option('--maxweightnorm', 2000)
-- parameters for different learning rates for different layers
cmd:option('--lr_wordembed', 0.8)
cmd:option('--lr_other', 0.01)
cmd:option('--weightClip_wordembed', 1500)
cmd:option('--weightClip_other', 20)
return cmd:parse(arg or {})
end
function loadPretrained(opt)
--load the pre-trained model then evaluate on the test set then generate the csv file that could be submitted to the evaluation server
local method = 'BOWIMG'
local model_path = 'model/BOWIMG.t7'
opt.method = method
-- load pre-trained model
local f_model = torch.load(model_path)
local manager_vocab = f_model.manager_vocab
-- Some simple fix for old models.
if manager_vocab.vocab_map_question['END'] == nil then
manager_vocab.vocab_map_question['END'] = -1
manager_vocab.ivocab_map_question[-1] = 'END'
end
local model, criterion = build_model(opt, manager_vocab)
local paramx, paramdx = model:getParameters()
paramx:copy(f_model.paramx)
return {
model = model,
criterion = criterion,
manager_vocab = manager_vocab
}
end