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

LinearTHNN.lua « test - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd1529f454c5d0e046df45c218c1328512fff3a7 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local LinearTHNN, parent = torch.class('nn.LinearTHNN', 'nn.Module')

function LinearTHNN:__init(inputSize, outputSize, bias)
   parent.__init(self)
   local bias = ((bias == nil) and true) or bias
   self.weight = torch.Tensor(outputSize, inputSize)
   self.gradWeight = torch.Tensor(outputSize, inputSize)
   if bias then
      self.bias = torch.Tensor(outputSize)
      self.gradBias = torch.Tensor(outputSize)
   end
   self.addBuffer = torch.Tensor(outputSize)
   self:reset()
end

function LinearTHNN:noBias()
   self.bias = nil
   self.gradBias = nil
   return self
end

function LinearTHNN:reset(stdv)
   if stdv then
      stdv = stdv * math.sqrt(3)
   else
      stdv = 1./math.sqrt(self.weight:size(2))
   end
   if nn.oldSeed then
      for i=1,self.weight:size(1) do
         self.weight:select(1, i):apply(function()
            return torch.uniform(-stdv, stdv)
         end)
      end
      if self.bias then
         for i=1,self.bias:nElement() do
            self.bias[i] = torch.uniform(-stdv, stdv)
         end
      end
   else
      self.weight:uniform(-stdv, stdv)
      if self.bias then self.bias:uniform(-stdv, stdv) end
   end
   return self
end

function LinearTHNN:updateOutput(input)
   input.THNN.Linear_updateOutput(
      input:cdata(),
      self.output:cdata(),
      self.weight:cdata(),
      self.bias and self.bias:cdata(),
      self.addBuffer:cdata()
   )
   return self.output
end

function LinearTHNN:updateGradInput(input, gradOutput)
   input.THNN.Linear_updateGradInput(
      input:cdata(),
      gradOutput:cdata(),
      self.gradInput:cdata(),
      self.weight:cdata()
   )
   return self.gradInput
end

function LinearTHNN:accGradParameters(input, gradOutput, scale)
   input.THNN.Linear_accGradParameters(
      input:cdata(),
      gradOutput:cdata(),
      self.gradInput:cdata(),
      self.weight:cdata(),
      self.bias and self.bias:cdata(),
      self.gradWeight:cdata(),
      self.bias and self.gradBias:cdata(),
      self.addBuffer:cdata(),
      scale or 1
   )
   return self.gradWeight
end

function LinearTHNN:sharedAccUpdateGradParameters(input, gradOutput, lr)
   -- we do not need to accumulate parameters when sharing:
   self:defaultAccUpdateGradParameters(input, gradOutput, lr)
end

function LinearTHNN:clearState()
   if self.addBuffer then self.addBuffer:set() end
   return parent.clearState(self)
end

function LinearTHNN:__tostring__()
  return torch.type(self) ..
      string.format('(%d -> %d)', self.weight:size(2), self.weight:size(1)) ..
      (self.bias == nil and ' without bias' or '')
end