-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSparseMaxCriterion.lua
32 lines (26 loc) · 989 Bytes
/
SparseMaxCriterion.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
require 'nn'
-- Combination of SparseMaxLoss and ClassNLLCriterion
local SparseMaxCriterion, Criterion = torch.class('nn.SparseMaxCriterion', 'nn.Criterion')
function SparseMaxCriterion:__init(weights)
Criterion.__init(self)
self.sml = nn.SparseMaxLoss()
self.nll = nn.ClassNLLCriterion(weights)
end
function SparseMaxCriterion:updateOutput(input, target)
input = input:squeeze()
target = type(target) == 'number' and target or target:squeeze()
self.sml:updateOutput(input)
self.nll:updateOutput(self.sml.output, target)
self.output = self.nll.output
return self.output
end
function SparseMaxCriterion:updateGradInput(input, target)
local size = input:size()
input = input:squeeze()
target = type(target) == 'number' and target or target:squeeze()
self.nll:updateGradInput(self.sml.output, target)
self.sml:updateGradInput(input, self.nll.gradInput)
self.gradInput:view(self.sml.gradInput, size)
return self.gradInput
end
return nn.SparseMaxCriterion