-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndex.lua
More file actions
27 lines (22 loc) · 734 Bytes
/
Index.lua
File metadata and controls
27 lines (22 loc) · 734 Bytes
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
local Index, parent = torch.class('nn.Index', 'nn.Module')
function Index:__init(dimension)
parent.__init(self)
self.dimension = dimension
self.gradInput = {self.gradInput}
end
function Index:updateOutput(input)
local t = input[1]
local index = input[2]
self.output:index(t, self.dimension, index)
return self.output
end
function Index:updateGradInput(input, gradOutput)
local t = input[1]
local index = input[2]
local gradInput = self.gradInput[1] -- no gradient for the index variable
gradInput:resizeAs(t):zero()
for n = 1, index:size(1) do
gradInput:narrow(self.dimension, index[n], 1):add(gradOutput:narrow(self.dimension, n, 1))
end
return self.gradInput
end