Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Sum.lua - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b068e25b1745bfb05b791744b8f220b2f854426f (plain)
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
local Sum, parent = torch.class('nn.Sum', 'nn.Module')

function Sum:__init(dimension)
   parent.__init(self)
   dimension = dimension or 1
   self.dimension = dimension
end

function Sum:updateOutput(input)
   input.torch.sum(self.output, input, self.dimension)
   self.output = self.output:select(self.dimension, 1)
   return self.output
end

function Sum:updateGradInput(input, gradOutput)
   local size = gradOutput:size():totable()
   local stride = gradOutput:stride():totable()
   table.insert(size, self.dimension, input:size(self.dimension))
   table.insert(stride, self.dimension, 0)

   self.gradInput:set(gradOutput:storage(),
                      1,
                      torch.LongStorage(size),
                      torch.LongStorage(stride))
                      
   return self.gradInput
end