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

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

function SparseLinear:__init(inputSize, outputSize)
   parent.__init(self)

   self.weightDecay = 0
   self.weight = torch.Tensor(outputSize, inputSize)
   self.bias = torch.Tensor(outputSize)
   self.gradWeight = torch.Tensor(outputSize, inputSize)
   self.gradBias = torch.Tensor(outputSize)
   self.lastInput = torch.Tensor()
   -- state
   self.gradInput:resize(inputSize)
   self.output:resize(outputSize)

   self:reset()
end

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

   -- we do this so the initialization is exactly
   -- the same than in previous torch versions
   for i=1,self.weight:size(1) do
      self.weight:select(1, i):apply(function()
                                        return torch.uniform(-stdv, stdv)
                                     end)
      self.bias[i] = torch.uniform(-stdv, stdv) * 0.000001
   end
end

function SparseLinear:updateOutput(input)
   return input.nn.SparseLinear_updateOutput(self, input)
end

function SparseLinear:accGradParameters(input, gradOutput, scale)
   return input.nn.SparseLinear_accGradParameters(self, input, gradOutput, scale)
end