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

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

function Add:__init(inputSize,scalar)
   parent.__init(self)
  
   local size = inputSize
   if scalar then size=1 end
   self.scalar = scalar
   self.bias = torch.Tensor(size)
   self.gradBias = torch.Tensor(size)
   
   self._ones = torch.Tensor{1}

   self:reset()
end

function Add:reset(stdv)
   if stdv then 
      stdv = stdv * math.sqrt(3)
   else
      stdv = 1./math.sqrt(self.bias:size(1))
   end

   self.bias:uniform(-stdv, stdv)
end

function Add:updateOutput(input)
   self.output:resizeAs(input):copy(input)
   if self.scalar then
      self.output:add(self.bias[1]);
   else
      if input:isSameSizeAs(self.bias) then
         self.output:add(self.bias)
      else
         local batchSize = input:size(1)
         if self._ones:size(1) ~= batchSize then
            self._ones:resize(batchSize):fill(1)
         end
         local bias = self.bias:view(-1)
         local output = self.output:view(batchSize, -1)
         output:addr(1, self._ones, bias)
      end
   end
   return self.output
end

function Add:updateGradInput(input, gradOutput)
   if self.gradInput then
      self.gradInput:resizeAs(gradOutput):copy(gradOutput) 
      return self.gradInput
   end
end

function Add:accGradParameters(input, gradOutput, scale)
   scale = scale or 1
   if self.gradBias:size(1) == 1 then
      self.gradBias[1] = self.gradBias[1] + scale*gradOutput:sum();
   else
      if input:isSameSizeAs(self.bias) then
         self.gradBias:add(scale, gradOutput)
      else
         local gradOutput = gradOutput:view(input:size(1), -1)
         self.gradBias:view(-1):addmv(scale, gradOutput:t(), self._ones)
      end
   end
end