forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitTable.lua
41 lines (35 loc) · 1.15 KB
/
SplitTable.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
local SplitTable, parent = torch.class('nn.SplitTable', 'nn.Module')
function SplitTable:__init(dimension, nInputDims)
parent.__init(self)
self.dimension = dimension
self.nInputDims = nInputDims
end
function SplitTable:_getPositiveDimension(input)
local dimension = self.dimension
if dimension < 0 then
dimension = input:dim() + dimension + 1
elseif self.nInputDims and input:dim()==(self.nInputDims+1) then
dimension = dimension + 1
end
return dimension
end
function SplitTable:updateOutput(input)
local dimension = self:_getPositiveDimension(input)
local slices = input:size(dimension)
local currentOutput= {}
for i=1,slices do
currentOutput[#currentOutput+1] = input:select(dimension,i)
end
self.output = currentOutput
return self.output
end
function SplitTable:updateGradInput(input, gradOutput)
local dimension = self:_getPositiveDimension(input)
local slices = input:size(dimension)
self.gradInput:resizeAs(input)
for i=1,slices do
local currentGradInput = gradOutput[i];
self.gradInput:select(dimension,i):copy(currentGradInput)
end
return self.gradInput
end