forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelect.lua
More file actions
22 lines (19 loc) · 741 Bytes
/
Select.lua
File metadata and controls
22 lines (19 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local Select, parent = torch.class('nn.Select', 'nn.Module')
function Select:__init(dimension,index)
parent.__init(self)
self.dimension = dimension
self.index = index
end
function Select:updateOutput(input)
local index = self.index < 0 and input:size(self.dimension) + self.index + 1 or self.index
local output = input:select(self.dimension, index);
self.output:resizeAs(output)
return self.output:copy(output)
end
function Select:updateGradInput(input, gradOutput)
local index = self.index < 0 and input:size(self.dimension) + self.index + 1 or self.index
self.gradInput:resizeAs(input)
self.gradInput:zero()
self.gradInput:select(self.dimension,index):copy(gradOutput)
return self.gradInput
end