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: fadcd21e989b2d630681d03d826b8a7cf090e379 (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
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.bias = torch.Tensor(size)
   self.gradBias = torch.Tensor(size)
     
   -- state
   self.gradInput:resize(inputSize)
   self.output:resize(inputSize) 

   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

   for i=1,self.bias:size(1) do
      self.bias[i] = torch.uniform(-stdv, stdv)
   end
end

function Add:updateOutput(input)
   self.output:copy(input);
   if self.gradBias:size(1)==1 then
     self.output:add(self.bias[1]);
   else
     self.output:add(self.bias);
   end
   return self.output
end 

function Add:updateGradInput(input, gradOutput)
   if self.gradInput then
      self.gradInput: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
      self.gradBias:add(scale, gradOutput)
   end
end