forked from Element-Research/rnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractRecurrent.lua
184 lines (144 loc) · 5.42 KB
/
AbstractRecurrent.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
local _ = require 'moses'
assert(not nn.AbstractRecurrent, "update nnx package : luarocks install nnx")
local AbstractRecurrent, parent = torch.class('nn.AbstractRecurrent', 'nn.Container')
AbstractRecurrent.dpnn_stepclone = true
function AbstractRecurrent:__init(rho)
parent.__init(self)
self.rho = rho or 99999 --the maximum number of time steps to BPTT
self.outputs = {}
self._gradOutputs = {}
self.step = 1
-- stores internal states of Modules at different time-steps
self.sharedClones = {}
self:reset()
end
function AbstractRecurrent:getStepModule(step)
assert(step, "expecting step at arg 1")
local recurrentModule = self.sharedClones[step]
if not recurrentModule then
recurrentModule = self.recurrentModule:stepClone()
self.sharedClones[step] = recurrentModule
self.nSharedClone = _.size(self.sharedClones)
end
return recurrentModule
end
function AbstractRecurrent:maskZero(nInputDim)
self.recurrentModule = nn.MaskZero(self.recurrentModule, nInputDim)
return self
end
function AbstractRecurrent:updateGradInput(input, gradOutput)
-- updateGradInput should be called in reverse order of time
self.updateGradInputStep = self.updateGradInputStep or self.step
-- BPTT for one time-step
self.gradInput = self:_updateGradInput(input, gradOutput, self.updateGradInputStep)
self.updateGradInputStep = self.updateGradInputStep - 1
assert(self.gradInput, "Missing gradInput")
return self.gradInput
end
function AbstractRecurrent:accGradParameters(input, gradOutput, scale)
-- accGradParameters should be called in reverse order of time
assert(self.updateGradInputStep < self.step, "Missing updateGradInput")
self.accGradParametersStep = self.accGradParametersStep or self.step
-- BPTT for one time-step
local step = self.accGradParametersStep - 1
self:_accGradParameters(input, gradOutput, scale)
self.accGradParametersStep = self.accGradParametersStep - 1
end
-- goes hand in hand with the next method : forget()
-- this methods brings the oldest memory to the current step
function AbstractRecurrent:recycle(offset)
-- offset can be used to skip initialModule (if any)
offset = offset or 0
self.nSharedClone = self.nSharedClone or _.size(self.sharedClones)
local rho = math.max(self.rho + 1, self.nSharedClone)
if self.sharedClones[self.step] == nil then
self.sharedClones[self.step] = self.sharedClones[self.step-rho]
self.sharedClones[self.step-rho] = nil
assert(self._gradOutputs[self.step] == nil)
self._gradOutputs[self.step] = self._gradOutputs[self.step-rho]
self._gradOutputs[self.step-rho] = nil
end
self.outputs[self.step-rho-1] = nil
return self
end
-- this method brings all the memory back to the start
function AbstractRecurrent:forget()
-- the recurrentModule may contain an AbstractRecurrent instance (issue 107)
parent.forget(self)
-- bring all states back to the start of the sequence buffers
if self.train ~= false then
self.outputs = {}
self.sharedClones = _.compact(self.sharedClones)
self._gradOutputs = _.compact(self._gradOutputs)
end
-- forget the past inputs; restart from first step
self.step = 1
return self
end
function AbstractRecurrent:includingSharedClones(f)
local modules = self.modules
local sharedClones = self.sharedClones
self.sharedClones = nil
self.modules = {}
for i,modules in ipairs{modules, sharedClones} do
for j, module in pairs(modules or {}) do
table.insert(self.modules, module)
end
end
local r = {f()}
self.modules = modules
self.sharedClones = sharedClones
return unpack(r)
end
function AbstractRecurrent:type(type, tensorcache)
self:includingSharedClones(function()
return parent.type(self, type, tensorcache)
end)
return self
end
function AbstractRecurrent:training()
return self:includingSharedClones(function()
return parent.training(self)
end)
end
function AbstractRecurrent:evaluate()
return self:includingSharedClones(function()
return parent.evaluate(self)
end)
end
function AbstractRecurrent:reinforce(reward)
return self:includingSharedClones(function()
return parent.reinforce(self, reward)
end)
end
-- used by Recursor() after calling stepClone.
-- this solves a very annoying bug...
function AbstractRecurrent:setOutputStep(step)
self.output = self.outputs[step] --or self:getStepModule(step).output
assert(self.output, "no output for step "..step)
end
function AbstractRecurrent:maxBPTTstep(rho)
self.rho = rho
end
-- backwards compatibility
AbstractRecurrent.recursiveResizeAs = rnn.recursiveResizeAs
AbstractRecurrent.recursiveSet = rnn.recursiveSet
AbstractRecurrent.recursiveCopy = rnn.recursiveCopy
AbstractRecurrent.recursiveAdd = rnn.recursiveAdd
AbstractRecurrent.recursiveTensorEq = rnn.recursiveTensorEq
AbstractRecurrent.recursiveNormal = rnn.recursiveNormal
function AbstractRecurrent:backwardThroughTime(step, rho)
error"DEPRECATED Jan 8, 2016"
end
function AbstractRecurrent:updateGradInputThroughTime(step, rho)
error"DEPRECATED Jan 8, 2016"
end
function AbstractRecurrent:accGradParametersThroughTime(step, rho)
error"DEPRECATED Jan 8, 2016"
end
function AbstractRecurrent:accUpdateGradParametersThroughTime(lr, step, rho)
error"DEPRECATED Jan 8, 2016"
end
function AbstractRecurrent:backwardUpdateThroughTime(learningRate)
error"DEPRECATED Jan 8, 2016"
end