"Fossies" - the Fresh Open Source Software Archive

Member "rspamd-1.7.3/contrib/torch/nn/Add.lua" (10 Apr 2018, 1692 Bytes) of package /linux/misc/rspamd-1.7.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 local Add, parent = torch.class('nn.Add', 'nn.Module')
    2 
    3 function Add:__init(inputSize,scalar)
    4    parent.__init(self)
    5 
    6    local size = inputSize
    7    if scalar then size=1 end
    8    self.scalar = scalar
    9    self.bias = torch.Tensor(size)
   10    self.gradBias = torch.Tensor(size)
   11 
   12    self._ones = torch.Tensor{1}
   13 
   14    self:reset()
   15 end
   16 
   17 function Add:reset(stdv)
   18    if stdv then
   19       stdv = stdv * math.sqrt(3)
   20    else
   21       stdv = 1./math.sqrt(self.bias:size(1))
   22    end
   23 
   24    self.bias:uniform(-stdv, stdv)
   25 end
   26 
   27 function Add:updateOutput(input)
   28    self.output:resizeAs(input):copy(input)
   29    if self.scalar then
   30       self.output:add(self.bias[1]);
   31    else
   32       if input:isSameSizeAs(self.bias) then
   33          self.output:add(self.bias)
   34       else
   35          local batchSize = input:size(1)
   36          if self._ones:size(1) ~= batchSize then
   37             self._ones:resize(batchSize):fill(1)
   38          end
   39          local bias = self.bias:view(-1)
   40          local output = self.output:view(batchSize, -1)
   41          output:addr(1, self._ones, bias)
   42       end
   43    end
   44    return self.output
   45 end
   46 
   47 function Add:updateGradInput(input, gradOutput)
   48    if self.gradInput then
   49       self.gradInput:resizeAs(gradOutput):copy(gradOutput)
   50       return self.gradInput
   51    end
   52 end
   53 
   54 function Add:accGradParameters(input, gradOutput, scale)
   55    scale = scale or 1
   56    if self.gradBias:size(1) == 1 then
   57       self.gradBias[1] = self.gradBias[1] + scale*gradOutput:sum();
   58    else
   59       if input:isSameSizeAs(self.bias) then
   60          self.gradBias:add(scale, gradOutput)
   61       else
   62          local gradOutput = gradOutput:view(input:size(1), -1)
   63          self.gradBias:view(-1):addmv(scale, gradOutput:t(), self._ones)
   64       end
   65    end
   66 end