forked from facebookarchive/MazeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.lua
More file actions
270 lines (233 loc) · 8.61 KB
/
Copy pathmodel.lua
File metadata and controls
270 lines (233 loc) · 8.61 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
-- Copyright (c) 2015-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
require('nn')
require('nngraph')
paths.dofile('LinearNB.lua')
local function share(name, mod)
if g_shareList[name] == nil then g_shareList[name] = {} end
table.insert(g_shareList[name], mod)
end
local function nonlin()
if g_opts.nonlin == 'tanh' then
return nn.Tanh()
elseif g_opts.nonlin == 'relu' then
return nn.ReLU()
elseif g_opts.nonlin == 'none' then
return nn.Identity()
else
error('wrong nonlin')
end
end
local function build_lookup_bow(input, context, hop)
local A_LT = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(context)
share('A_LT', A_LT)
g_modules['A_LT'] = A_LT
local A_V = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(A_LT)
local Ain = nn.Sum(3)(A_V)
local B_LT = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(context)
share('B_LT', B_LT)
g_modules['B_LT'] = B_LT
local B_V = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(B_LT)
local Bin = nn.Sum(3)(B_V)
local hid3dim = nn.View(1, -1):setNumInputDims(1)(input)
local MMaout = nn.MM(false, true)
local Aout = MMaout({hid3dim, Ain})
local Aout2dim = nn.View(-1):setNumInputDims(2)(Aout)
local P = nn.SoftMax()(Aout2dim)
g_modules[hop]['prob'] = P.data.module
local probs3dim = nn.View(1, -1):setNumInputDims(1)(P)
local MMbout = nn.MM(false, false)
return MMbout({probs3dim, Bin})
end
local function build_memory(input, context)
local hid = {}
hid[0] = input
for h = 1, g_opts.nhop do
g_modules[h] = {}
local Bout = build_lookup_bow(hid[h-1], context, h)
local C = nn.LinearNB(g_opts.hidsz, g_opts.hidsz)(hid[h-1])
share('proj', C)
local D = nn.CAddTable()({C, Bout})
hid[h] = nonlin()(D)
end
return hid
end
function build_conv(input)
local out_dim
local d = g_opts.conv_sz
local conv1 = nn.SpatialConvolution(g_opts.hidsz, g_opts.convdim, 3, 3, 1, 1)(input)
share('conv1', conv1)
local nonl1 = nonlin()(conv1)
d = d - 2
if d > 13 then
nonl1 = nn.SpatialMaxPooling(2, 2, 2, 2)(nonl1)
d = math.floor(d / 2)
end
local conv2 = nn.SpatialConvolution(g_opts.convdim, g_opts.convdim, 3, 3, 1, 1)(nonl1)
share('conv2', conv2)
local nonl2 = nonlin()(conv2)
d = d - 2
if d > 7 then
nonl2 = nn.SpatialMaxPooling(2, 2, 2, 2)(nonl2)
d = math.floor(d / 2)
end
local conv3 = nn.SpatialConvolution(g_opts.convdim, g_opts.convdim, 3, 3, 1, 1)(nonl2)
share('conv3', conv3)
local nonl3 = nonlin()(conv3)
d = d - 2
assert(d > 1 and d < 6)
out_dim = d * d * g_opts.convdim
local fc0 = nn.View(out_dim):setNumInputDims(3)(nonl3)
local fc1 = nn.Linear(out_dim, g_opts.hidsz)(fc0)
share('fc1', fc1)
return nonlin()(fc1)
end
local function build_lookup_conv(input, context, hop)
local A_LT_conv = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(context)
share('A_LT_conv', A_LT_conv)
local A_V = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(A_LT_conv)
local Ain = nn.Sum(3)(A_V)
local B_LT_conv = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(context)
share('B_LT_conv', B_LT_conv)
local B_V = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(B_LT_conv)
local Bin = nn.Sum(3)(B_V)
local hid3dim = nn.View(1, -1):setNumInputDims(1)(input)
local MMaout = nn.MM(false, true)
local Aout = MMaout({hid3dim, Ain})
local Aout2dim = nn.View(-1):setNumInputDims(2)(Aout)
local P = nn.SoftMax()(Aout2dim)
g_modules[hop]['prob_conv'] = P.data.module
local attention = nn.Replicate(g_opts.hidsz, 2, 1)(P)
local in_attend = nn.CMulTable()({Bin, attention})
local in_attend2 = nn.View(g_opts.conv_sz, g_opts.conv_sz, g_opts.hidsz):setNumInputDims(2)(in_attend)
local in_conv = nn.Transpose({2,4})(in_attend2)
local conv_out = build_conv(in_conv)
return conv_out
end
local function build_memory_conv(input, context, context_conv)
local hid = {}
hid[0] = input
for h = 1, g_opts.nhop do
g_modules[h] = {}
local Bout = build_lookup_bow(hid[h-1], context, h)
local Bout_conv = build_lookup_conv(hid[h-1], context_conv, h)
local C = nn.LinearNB(g_opts.hidsz, g_opts.hidsz)(hid[h-1])
share('proj', C)
local D = nn.CAddTable()({C, Bout, Bout_conv})
hid[h] = nonlin()(D)
end
return hid
end
local function build_model_memnn()
local input = nn.Identity()()
local context = nn.Identity()()
local hid = build_memory(input, context)
return {input, context}, hid[#hid]
end
local function build_model_conv()
local input = nn.Identity()()
local context = nn.Identity()()
-- process 2D spatial information
local in_emb = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(input)
local in_A = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(in_emb)
local in_bow = nn.Sum(3)(in_A)
local in_bow2d = nn.View(g_opts.conv_sz, g_opts.conv_sz, g_opts.hidsz):setNumInputDims(2)(in_bow)
local in_conv = nn.Transpose({2,4})(in_bow2d)
local conv_out = build_conv(in_conv)
local cont_emb2d = nn.LookupTable(g_opts.nwords, g_opts.hidsz)(context)
local cont_emb = nn.View(-1, g_opts.max_attributes, g_opts.hidsz):setNumInputDims(2)(cont_emb2d)
local cont_bow = nn.Sum(3)(cont_emb) -- sum over attributes
local cont_fc = nn.View(-1):setNumInputDims(2)(cont_bow)
local cont_out = nonlin()(nn.Linear(g_opts.memsize * g_opts.hidsz, g_opts.hidsz)(cont_fc))
local output = nonlin()(nn.CAddTable()({
nn.Linear(g_opts.hidsz, g_opts.hidsz)(conv_out),
nn.Linear(g_opts.hidsz, g_opts.hidsz)(cont_out)
}))
return {input, context}, output
end
function g_build_model()
if g_opts.model == 'mlp' then
return g_build_model_mlp()
end
local input, output
g_shareList = {}
g_modules = {}
if g_opts.model == 'memnn' then
input, output = build_model_memnn()
elseif g_opts.model == 'conv' then
input, output = build_model_conv()
else
error('wrong model name')
end
local hid_act = nonlin()(nn.Linear(g_opts.hidsz, g_opts.hidsz)(output))
local action = nn.Linear(g_opts.hidsz, g_opts.nactions)(hid_act)
local action_prob = nn.LogSoftMax()(action)
local hid_bl = nonlin()(nn.Linear(g_opts.hidsz, g_opts.hidsz)(output))
local baseline = nn.Linear(g_opts.hidsz, 1)(hid_bl)
local model = nn.gModule(input, {action_prob, baseline})
for _, l in pairs(g_shareList) do
if #l > 1 then
local m1 = l[1].data.module
for j = 2,#l do
local m2 = l[j].data.module
m2:share(m1,'weight','bias','gradWeight','gradBias')
end
end
end
return model
end
function g_build_model_mlp()
local MH = g_opts.conv_sz
local MW = g_opts.conv_sz
local memsize = g_opts.memsize
local na = g_opts.nactions
local nwords = g_opts.nwords
local input = nn.Identity()()
local baseline, action
g_modules = {}
local in_dim = MH*MW*nwords + memsize*nwords + 1
if g_opts.nlayers > 1 then
local a = nn.Sequential()
local atab = nn.LookupTable(in_dim, g_opts.hidsz)
g_modules.atab = atab
a:add(atab)
a:add(nn.Sum(2))
a:add(nn.Add(g_opts.hidsz)) -- bias
a:add(nonlin())
for l = 3, g_opts.nlayers do
a:add(nn.Linear(g_opts.hidsz, g_opts.hidsz))
a:add(nonlin())
end
local hidstate = a(input)
action = nn.Linear(g_opts.hidsz, na)(hidstate)
baseline = nn.Linear(g_opts.hidsz, 1)(hidstate)
else
local a = nn.Sequential()
local atab = nn.LookupTable(in_dim, na)
g_modules.atab = atab
a:add(atab)
a:add(nn.Sum(2))
action = a(input)
local btab = nn.LookupTable(in_dim,1)
g_modules.btab = btab
local b = nn.Sequential()
b:add(btab)
b:add(nn.Sum(2))
baseline = b(input)
end
local action_prob = nn.LogSoftMax()(action)
local model = nn.gModule({input}, {action_prob, baseline})
return model
end
function g_init_model()
g_model = g_build_model()
g_paramx, g_paramdx = g_model:getParameters()
if g_opts.init_std > 0 then
g_paramx:normal(0, g_opts.init_std)
end
g_bl_loss = nn.MSECriterion()
end