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

TemporalDynamicKMaxPooling.lua - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 511275b1b0b278914149c159ede925356f0f3fa2 (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
--[[
   This file implements Dynamic K Max Pooling as described in the paper:
   "A Convolutional Neural Network for Modelling Sentences"
                   by Nal Kalchbrenner, Edward Grefenstette, Phil Blunsom
   
   The operation is simply selecting the k highest values out of a sequence.
   k can be a calculated value or pre-defined

   The value of k can be calulated as in the paper by using:
      k_top as minK
      (L-l)/L as factor
   
   Where:
      k_top is the desired sequence length at the end of the convolution part,
      L is the total number of layers,
      l is this layers number
]]

local TemporalDynamicKMaxPooling, parent = torch.class('nn.TemporalDynamicKMaxPooling', 'nn.Module')

function TemporalDynamicKMaxPooling:__init(minK, factor)
   parent.__init(self)

   self.minK = minK
   self.factor = factor or 0
end

function TemporalDynamicKMaxPooling:updateOutput(input)
   assert(input:dim() == 2 or input:dim() == 3, 'Only 2D or 3D(batch mode) accepted')

   local seqDim = input:dim()-1
   local k = math.max(self.minK, math.ceil(self.factor*input:size(seqDim)))
   assert(input:size(seqDim) >= self.minK, 'Input sequence length (' .. input:size(seqDim) .. ') too small for desired k value (' .. k .. ')')

   -- Sort input in descending order
   local sorted, allIndices = input:sort(seqDim,true)
   -- Reduce the indices to only include the top-k and return to original order by sorting
   self.indices = allIndices:narrow(seqDim, 1, k):sort(seqDim)

   self.output = input:gather(seqDim, self.indices)

   return self.output
end

function TemporalDynamicKMaxPooling:updateGradInput(input, gradOutput)
   if self.gradInput then
      local seqDim = input:dim()-1

      self.gradInput:resizeAs(input)
      self.gradInput:zero()

      -- Using the previously stored indices, add the gradOutputs to their respective
      -- input indices in the self.gradInput buffer
      local updateValues = self.gradInput:gather(seqDim, self.indices)
      updateValues:add(gradOutput)
      self.gradInput:scatter(seqDim, self.indices, updateValues)

      return self.gradInput
   end
end

function TemporalDynamicKMaxPooling:clearState()
   nn.utils.clear(self, 'indices')
   return parent.clearState(self)
end