-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistKLDivCriterion.lua
34 lines (31 loc) · 1.08 KB
/
DistKLDivCriterion.lua
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
local DistKLDivCriterion, parent = torch.class('nn.DistKLDivCriterion', 'nn.Criterion')
function DistKLDivCriterion:__init()
parent.__init(self)
self.sizeAverage = true
end
function DistKLDivCriterion:updateOutput(input, target)
assert(input:dim() == target:dim() and
torch.LongTensor(input:size()):eq(torch.LongTensor(target:size())):all(),
'input and target should have the same size')
self.output_tensor = self.output_tensor or input.new(1)
input.THNN.DistKLDivCriterion_updateOutput(
input:cdata(),
target:cdata(),
self.output_tensor:cdata(),
self.sizeAverage
)
self.output = self.output_tensor[1]
return self.output
end
function DistKLDivCriterion:updateGradInput(input, target)
assert(input:dim() == target:dim() and
torch.LongTensor(input:size()):eq(torch.LongTensor(target:size())):all(),
'input and target should have the same size')
input.THNN.DistKLDivCriterion_updateGradInput(
input:cdata(),
target:cdata(),
self.gradInput:cdata(),
self.sizeAverage
)
return self.gradInput
end