-
Notifications
You must be signed in to change notification settings - Fork 14
/
LookupTable.lua
236 lines (201 loc) · 7.04 KB
/
LookupTable.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
require 'nn'
nn.LookupTable.baseUpdateOutput = nn.LookupTable.updateOutput
--nn.LookupTable.baseUpdateGradInput = nn.LookupTable.updateGradInput
nn.LookupTable.baseAccGradParameters = nn.LookupTable.accGradParameters
nn.LookupTable.baseAccUpdateGradParameters = nn.LookupTable.accUpdateGradParameters
function nn.LookupTable:__init(nIndex, nOutput)
nn.Module.__init(self)
-- self.nIndex = nIndex
-- self.nOutput = nOutput
self.weight = torch.Tensor(nIndex, nOutput)
self.gradWeight = torch.Tensor(nIndex, nOutput):zero()
self:reset()
end
--function LookupTable:updateOutput(input)
-- if torch.type(input) ~= 'torch.ClTensor' then
-- return self:baseUpdateOutput(input)
-- end
-- self:backCompatibility()
-- input = self:makeInputContiguous(input)
-- if input:dim() == 1 then
-- self.output:index(self.weight, 1, input)
-- elseif input:dim() == 2 then
-- self.output:index(self.weight, 1, input:view(-1))
-- self.output = self.output:view(input:size(1), input:size(2), self.weight:size(2))
-- else
-- error("input must be a vector or matrix")
-- end
-- return self.output
--end
--function LookupTable:accGradParameters(input, gradOutput, scale)
-- if torch.type(input) ~= 'torch.ClTensor' then
-- return self:baseAccGradParameters(input)
-- end
-- self:backCompatibility()
-- input = self.copiedInput and self._input or input
-- if input:dim() == 2 then
-- input = input:view(-1)
-- elseif input:dim() ~= 1 then
-- error("input must be a vector or matrix")
-- end
-- self.gradWeight.nn.LookupTable_accGradParameters(self, input, gradOutput, scale)
--end
--local LookupTable, parent = torch.class('nn.LookupTable', 'nn.Module')
--LookupTable.__version = 3
--function LookupTable:__init(nIndex, ...)
-- parent.__init(self)
-- local arg = {...}
-- if select('#', ...) == 1 and type(arg[1]) ~= "number" then
-- local size = arg[1]
-- self.size = torch.LongStorage(#size + 1)
-- for i=1,#size do
-- self.size[i+1] = size[i]
-- end
-- else
-- self.size = torch.LongStorage(select('#', ...)+1)
-- for i=1,select('#',...) do
-- self.size[i+1] = arg[i]
-- end
-- end
-- self.size[1] = nIndex
--
-- batchSize = torch.LongTensor(#self.size + 1)
-- batchSize:narrow(1, 2,#self.size):copy(torch.LongTensor(self.size))
-- batchSize[1] = 1
-- self.batchSize = batchSize:storage()
--
-- self.weight = torch.Tensor(self.size)
-- self.gradWeight = torch.Tensor(self.size):zero()
-- self.inputs = {}
-- self.nBackward = 0
-- self:reset()
--end
--function LookupTable:reset(stdv)
-- stdv = stdv or 1
-- if nn.oldSeed then
-- self.weight:apply(function()
-- return torch.normal(0, stdv)
-- end)
-- else
-- self.weight:normal(0, stdv)
-- end
--end
function nn.LookupTable:updateOutput(input)
if torch.type(input) ~= 'torch.ClTensor' then
return self:baseUpdateOutput(input)
end
assert(not self.shouldScaleGradByFreq, 'self.shouldScaleGradByFreq not implemented')
if self.size == nil then
self.size = self.weight:size()
-- if select('#', ...) == 1 and type(arg[1]) ~= "number" then
-- local size = arg[1]
-- self.size = torch.LongStorage(#size + 1)
-- for i=1,#size do
-- self.size[i+1] = size[i]
-- end
-- else
-- self.size = torch.LongStorage(select('#', ...)+1)
-- for i=1,select('#',...) do
-- self.size[i+1] = arg[i]
-- end
-- end
-- self.size[1] = nIndex
batchSize = torch.LongTensor(#self.size + 1)
batchSize:narrow(1, 2,#self.size):copy(torch.LongTensor(self.size))
batchSize[1] = 1
self.batchSize = batchSize:storage()
self.nBackward = 0
self.inputs = {}
end
if input:dim() == 1 then
local nIndex = input:size(1)
self.size[1] = nIndex
self.output:resize(self.size)
for i=1,nIndex do
self.output:select(1, i):copy(self.weight:select(1, input[i]))
end
elseif input:dim() == 2 then
local nExample = input:size(1)
local nIndex = input:size(2)
self.batchSize[1] = nExample
self.batchSize[2] = nIndex
self.output:resize(self.batchSize)
for i=1,nExample do
local output = self.output:select(1, i)
local input = input:select(1, i)
for j=1,nIndex do
output:select(1, j):copy(self.weight:select(1, input[j]))
end
end
end
return self.output
end
--function LookupTable:zeroGradParameters()
-- for k,_ in pairs(self.inputs) do
-- self.gradWeight:select(1, k):zero()
-- end
-- self.inputs = {}
-- self.nBackward = 0
--end
function nn.LookupTable:accGradParameters(input, gradOutput, scale)
if torch.type(input) ~= 'torch.ClTensor' then
return self:baseAccGradParameters(input, gradOutput, scale)
end
assert(not self.shouldScaleGradByFreq, 'self.shouldScaleGradByFreq not implemented')
scale = scale or 1
if input:dim() == 1 then
self.nBackward = self.nBackward + 1
for i=1,input:size(1) do
local k = input[i]
self.inputs[k] = (self.inputs[k] or 0) + 1
self.gradWeight:select(1, k):add(scale, gradOutput:select(1, i))
end
elseif input:dim() == 2 then
self.nBackward = self.nBackward + input:size(1)
for i=1,input:size(1) do
local input = input:select(1, i)
local gradOutput = gradOutput:select(1, i)
for j=1,input:size(1) do
local k = input[j]
self.inputs[k] = (self.inputs[k] or 0) + 1
self.gradWeight:select(1, k):add(scale, gradOutput:select(1, j))
end
end
end
end
function nn.LookupTable:accUpdateGradParameters(input, gradOutput, lr)
if torch.type(input) ~= 'torch.ClTensor' then
return self:baseAccUpdateGradParameters(input, gradOutput, lr)
end
assert(not self.shouldScaleGradByFreq, 'self.shouldScaleGradByFreq not implemented')
if input:dim() == 1 then
for i=1,input:size(1) do
local k = input[j]
local kscale = self:scaleUpdateByKey(k)
self.weight:select(1, input[i]):add(-lr*kscale, gradOutput:select(1, i))
end
elseif input:dim() == 2 then
for i=1,input:size(1) do
local input = input:select(1, i)
local gradOutput = gradOutput:select(1, i)
for j=1,input:size(1) do
local k = input[j]
local kscale = self:scaleUpdateByKey(k)
self.weight:select(1, k):add(-lr*kscale, gradOutput:select(1, j))
end
end
end
end
--function LookupTable:updateParameters(learningRate)
-- for k,nBackward in pairs(self.inputs) do
-- local kscale = self:scaleUpdateByKey(k)
-- self.weight:select(1, k):add(-learningRate*kscale, self.gradWeight:select(1, k))
-- end
--end
---- scale the update for each key
--function LookupTable:scaleUpdateByKey(inputKey)
-- -- default is to perform no key-based scalling
-- return 1
--end
---- we do not need to accumulate parameters when sharing
--LookupTable.sharedAccUpdateGradParameters = LookupTable.accUpdateGradParameters