forked from facebookarchive/SCRNNs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn.lua
365 lines (324 loc) · 11.9 KB
/
rnn.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
--
-- Copyright (c) 2015, 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.
--
-- Author: Sumit Chopra <[email protected]>
-- Michael Mathieu <[email protected]>
-- Marc'Aurelio Ranzato <[email protected]>
-- Tomas Mikolov <[email protected]>
-- Armand Joulin <[email protected]>
-- This file contains a class RNN. It implements modular rnn. The main functions
-- are newInputTrain (to train) and test (to do only inference).
require 'torch'
require 'sys'
require 'nn'
local RNN = torch.class("RNN")
-- config:
-- n_hidden : number of hidden units (size of the state)
-- initial_val : value of the initial state before any input
-- backprop_freq: number of steps between two backprops and parameter updates
-- backprop_len : number of backward steps during each backprop
-- (should be >= backprop_freq)
-- nets : table containing the networks:
-- encoder : the encoder which produces a hidden state using current
-- input and previous hidden state
-- decoder : transformation applied to the current hidden state
-- to produce output vector (the next symbol)
--
-- y1 y2 y3
-- ^ ^ ^
-- decoder decoder decoder
-- ^ ^ ^
-- ... h0 -> encoder -> h1 -> encoder -> h2 -> encoder -> h3
-- ^ ^ ^
-- x1 x2 x3
function RNN:__init(config, nets, criterion)
self.n_hidden = config.n_hidden
self.nets = {encoder = nets.encoder:clone()}
if nets.decoder ~= nil then
self.nets.decoder = nets.decoder:clone()
self.criterion = criterion:clone()
else
assert(nets.decoder_with_loss ~= nil)
self.nets.decoder_with_loss = nets.decoder_with_loss:clone()
end
self.type = torch.Tensor():type()
self.initial_val = config.initial_val
self.initial_state_dim = config.initial_state_dim
self.backprop_freq = config.backprop_freq
self.batch_size = config.batch_size
self.cuda_device = config.cuda_device
if self.cuda_device then
self:cuda()
end
self:unroll(config.backprop_len)
self:recomputeParameters()
self:reset()
-- set the clipping function
local scale_clip = function(dat, th)
local dat_norm = dat:norm()
if dat_norm > th then
dat:div(dat_norm/th)
end
end
local hard_clip = function(vec, th)
local tmp = vec:float()
local tmpp = torch.data(tmp)
for i = 0, tmp:size(1) - 1 do
if tmpp[i] < - th then
tmpp[i] = - th
else
if tmpp[i] > th then
tmpp[i] = th
end
end
end
vec[{}] = tmp[{}]
end
if config.clip_type == 'scale' then
self.clip_function = scale_clip
elseif config.clip_type == 'hard' then
self.clip_function = hard_clip
else
error('wrong clip type: ' .. config.clip_type)
end
self:set_internal_layers()
end
function RNN:set_internal_layers()
self.ilayers = {}
self.ilayers.emb = self.nets.encoder.modules[1].modules[1]
self.ilayers.proj = self.nets.encoder.modules[1].modules[2]
end
-- Reset network (training parameters and hidden state)
function RNN:reset()
self.i_input = 0
self.dw:zero()
self.state = nil
end
-- ship the model to gpu
function RNN:cuda()
self.type = 'torch.CudaTensor'
if self.criterion then
self.criterion = self.criterion:cuda()
else
self.nets.decoder_with_loss:cuda()
end
for _, v in pairs(self.nets) do
v = v:cuda()
end
if self.unrolled_nets then
for i = 1,#self.unrolled_nets do
for _, v in pairs(self.unrolled_nets[i]) do
v = v:cuda()
end
end
end
end
-- returns a clone of the RNN (with the same parameter values but in
-- different storages)
function RNN:clone()
local f = torch.MemoryFile("rw"):binary()
f:writeObject(self)
f:seek(1)
local clone = f:readObject()
f:close()
return clone
end
-- call this function if you change the network architecture after creating it
-- (probably a bad idea)
function RNN:recomputeParameters()
local dummy = nn.Sequential()
for k,v in pairs(self.nets) do
-- If we are using nn.HSM, the parameters are updated at each
-- accGradParameters (direct_update mode). Therefore we do not
-- add the parameters to the vector of parameter
if torch.typename(v) ~= 'nn.HSM' then
dummy:add(v)
end
end
self.w, self.dw = dummy:getParameters()
self.mom = torch.Tensor(self.dw:size()):zero():type(self.type)
self:unroll(#self.unrolled_nets)
end
-- the user shouldnt have to manually call this function
function RNN:unroll(n)
self.unrolled_nets = {}
for i = 1, n do
self.unrolled_nets[i] = {}
self.unrolled_nets[i].decoder_gradInput =
torch.Tensor():type(self.type)
for k,v in pairs(self.nets) do
if (k ~= 'decoder') and (k ~= 'decoder_with_loss') then
self.unrolled_nets[i][k] = v:clone("weight", "gradWeight",
"bias", "gradBias")
end
end
end
end
-- returns a tensor filled with initial state
function RNN:get_initial_state(bsize)
local initial_state
if self.initial_state_dim ~= nil then
initial_state =
torch.Tensor(torch.LongStorage(self.initial_state_dim)):type(self.type)
else
initial_state = torch.Tensor(bsize, self.n_hidden):type(self.type)
end
initial_state:fill(self.initial_val)
return initial_state
end
-- Runs forward pass in the set of networks nets, with previous state prev_state
function RNN:elemForward(nets, input, prev_state, target)
local bsize = input:size(1)
prev_state = prev_state or self:get_initial_state(bsize)
-- store the local inputs and previous state
nets.input = input
nets.prev_state = prev_state
local out_encoder = nets.encoder:forward{input, prev_state}
local out_decoder, err, n_valid = nil, nil, nil
if self.nets.decoder ~= nil then --using the main net (not unrolled)
assert(self.nets.decoder_with_loss == nil)
out_decoder = self.nets.decoder:forward(out_encoder)
if target then
err, n_valid = self.criterion:forward(out_decoder, target)
end
else
assert(self.nets.decoder_with_loss ~= nil)
err, n_valid = self.nets.decoder_with_loss:forward(out_encoder, target)
end
n_valid = n_valid or input:size(1)
return out_decoder, out_encoder, err, n_valid
end
-- Runs backward pass on the decode+criterion (or decode_with_loss) modules
function RNN:elemDecodeBackward(nets, target, learning_rate)
if self.nets.decoder ~= nil then
assert(self.nets.decoder_with_loss == nil)
local decoder_output = self.nets.decoder.output
local derr_do = self.criterion:backward(decoder_output, target)
local gradInput = self.nets.decoder:backward(nets.encoder.output,
derr_do)
nets.decoder_gradInput:resizeAs(gradInput):copy(gradInput)
else
assert(self.nets.decoder_with_loss ~= nil)
local gradInput =
self.nets.decoder_with_loss:updateGradInput(nets.encoder.output,
target)
nets.decoder_gradInput:resizeAs(gradInput):copy(gradInput)
-- This assumes the module has direct_update mode. Only HSM does:
assert(torch.typename(self.nets.decoder_with_loss) == 'nn.HSM')
-- self.nets.decoder_with_loss:zeroGradParameters()
self.nets.decoder_with_loss:accGradParameters(
nets.encoder.output, target, -learning_rate, true)
self.nets.decoder_with_loss.class_grad_bias:zero()
self.nets.decoder_with_loss.cluster_grad_bias:zero()
end
end
-- function to update the parameters
function RNN:updateParams(w, params)
if params.momentum then
self.mom:mul(params.momentum)
self.mom:add(self.dw)
w:add(- params.learning_rate, self.mom)
else
w:add(- params.learning_rate, self.dw)
end
end
-- function to clip the gradients of the parameters
function RNN:clipGradParams(gclip)
for k,v in pairs(self.nets) do
local lw, ldw = v:parameters()
if ldw then
for i = 1, #ldw do
if ldw[i] then
self.clip_function(ldw[i], gclip)
end
end
end
end
end
-- function to clip the gradients of the hidden states
function RNN:clipGradHiddens(vec, gclip)
self.clip_function(vec, gclip)
end
-- Main train function:
-- input : input word or minibatch
-- label : target word or minibatch
-- params:
-- learning_rate : learning rate
-- gradient_clip : if not nil, if the norm of the gradient is larger than
-- this number, project the gradients on the sphere
-- with this radius
-- It returns the sum of the errors and the number of terms in this sum
function RNN:newInputTrain(input, label, params)
self.i_input = self.i_input + 1
local last_nets = self.unrolled_nets[1]
for i = 1, #self.unrolled_nets-1 do
self.unrolled_nets[i] = self.unrolled_nets[i+1]
end
self.unrolled_nets[#self.unrolled_nets] = last_nets
local _output, next_state, err, n_valid = self:elemForward(last_nets, input,
self.state, label)
self:elemDecodeBackward(last_nets, label, params.learning_rate)
self.state = next_state
if self.i_input % self.backprop_freq == 0 then
local inc_gi_state_i = nil
local unroll_bound = math.max(1, #self.unrolled_nets - self.i_input + 1)
local j = 1
for i = #self.unrolled_nets, unroll_bound, -1 do
local nets = self.unrolled_nets[i]
local prev_state_i, input_i = nets.prev_state, nets.input
local gi_decoder_net = nets.decoder_gradInput
-- get gradients from decoder
if not inc_gi_state_i then
inc_gi_state_i = gi_decoder_net
elseif j <= self.backprop_freq then
inc_gi_state_i:add(gi_decoder_net)
j = j + 1
else
-- do nothing, since gradients from decoder have already
-- been accounted for
end
-- clip the gradients wrt hidden states
if params.gradInput_clip then
self:clipGradHiddens(inc_gi_state_i, params.gradInput_clip)
end
-- bprop through encoder
if i ~= 1 then
local gi_encoder_net =
nets.encoder:backward({input_i, prev_state_i}, inc_gi_state_i)
inc_gi_state_i = gi_encoder_net[2]
end
end
-- clip the gradients if specified
if params.gradient_clip then
self:clipGradParams(params.gradient_clip)
end
-- update the parameters
self:updateParams(self.w, params)
-- zero the gradients for the next time
self.dw:zero()
end
return err, n_valid
end
-- Runs only forward on inputs (1d or 2d sequence of inputs) and compares
-- with labels
-- It returns the sum of the errors and the number of terms in this sum
function RNN:test(inputs, labels)
local total_err = 0
local total_n_valid = 0
for i = 1,inputs:size(1) do
local _output, next_state, err, n_valid = self:elemForward(
self.unrolled_nets[1], inputs[i], self.state, labels[i])
self.state = next_state
if type(err) ~= 'number' then
err = err[1]
end
total_err = total_err + err
total_n_valid = total_n_valid + n_valid
end
return total_err, total_n_valid
end