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

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

function CMinTable:__init()
   parent.__init(self)
   self.gradInput = {}
   self.minIdx = torch.Tensor()
   self.mask = torch.Tensor()
   self.minVals = torch.Tensor()
   self.gradMaxVals = torch.Tensor()
end

function CMinTable:updateOutput(input)
   self.output:resizeAs(input[1]):copy(input[1])
   self.minIdx:resizeAs(input[1]):fill(1)
   for i=2,#input do
      self.maskByteTensor = self.maskByteTensor or
         (torch.type(self.output) == 'torch.CudaTensor' and
         torch.CudaByteTensor() or torch.ByteTensor())
      self.mask:lt(input[i], self.output)
      self.maskByteTensor:resize(self.mask:size()):copy(self.mask)
      self.minIdx:maskedFill(self.maskByteTensor, i)
      self.minVals:maskedSelect(input[i], self.maskByteTensor)
      self.output:maskedCopy(self.maskByteTensor, self.minVals)
   end
   return self.output
end

function CMinTable:updateGradInput(input, gradOutput)
   for i=1,#input do
      self.gradInput[i] = self.gradInput[i] or input[i].new()
      self.gradInput[i]:resizeAs(input[i]):fill(0.0)
      self.maskByteTensor = self.maskByteTensor or
         (torch.type(self.output) == 'torch.CudaTensor' and
         torch.CudaByteTensor() or torch.ByteTensor())
      self.mask:eq(self.minIdx, i)
      self.maskByteTensor:resize(self.mask:size()):copy(self.mask)
      self.gradMaxVals:maskedSelect(gradOutput, self.maskByteTensor)
      self.gradInput[i]:maskedCopy(self.maskByteTensor, self.gradMaxVals)
   end

   for i=#input+1, #self.gradInput do
       self.gradInput[i] = nil
   end

   return self.gradInput
end