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

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

function KLDivCriterion:__init()
   parent.__init(self)
   -- user options
   self.inputIsProbability = false
   self.targetIsProbability = false
   -- internal
   self.targetSoftMax = nn.SoftMax()
   self.inputSoftMax = nn.SoftMax()
   self.gradProbInput = torch.Tensor()
end

function KLDivCriterion:normalize(input, target)
   -- normalize target
   if not self.targetIsProbability then
      self.probTarget = self.targetSoftMax:forward(target)
   else
      self.probTarget = target
   end

   -- normalize input
   if not self.inputIsProbability then
      self.probInput = self.inputSoftMax:forward(input)
   else
      self.probInput = input
   end
end

function KLDivCriterion:denormalize(input)
   -- denormalize gradients
   if not self.inputIsProbability then
      self.gradInput = self.inputSoftMax:backward(input, self.gradProbInput)
   else
      self.gradInput = self.gradProbInput
   end
end

function KLDivCriterion:forward(input, target)
   self:normalize(input, target)
   self.output = 0
   for i = 1,input:size(1) do
      local acc = 0
      if self.probTarget[i] > 0 then
         acc = self.probTarget[i] * math.log(self.probTarget[i] / math.max(self.probInput[i],1e-9))
      end
      self.output = self.output + acc
   end
   return self.output
end

function KLDivCriterion:backward(input, target)
   self:normalize(input, target)
   self.gradProbInput:resizeAs(input)
   for i = 1,input:size(1) do
      self.gradProbInput[i] = - self.probTarget[i] / math.max(self.probInput[i],1e-9)
   end
   self:denormalize(input)
   return self.gradInput
end