-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassNLLCriterion.lua
103 lines (94 loc) · 2.9 KB
/
ClassNLLCriterion.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
local ClassNLLCriterion, parent = torch.class('nn.ClassNLLCriterion', 'nn.Criterion')
function ClassNLLCriterion:__init(weights)
parent.__init(self)
self.sizeAverage = true
self.outputTensor = torch.Tensor(1)
if weights then
assert(weights:dim() == 1, "weights input should be 1-D Tensor")
self.weights = weights
end
end
function ClassNLLCriterion:__len()
if (self.weights) then
return #self.weights
else
return 0
end
end
function ClassNLLCriterion:updateOutput(input, target)
if input:type() == 'torch.CudaTensor' and not self.weights then
if input:dim() == 1 then
self._target = self._target or input.new(1)
if type(target) == 'number' then
self._target[1] = target
else
self._target:copy(target)
end
input.nn.ClassNLLCriterion_updateOutput(self, input, self._target)
else
input.nn.ClassNLLCriterion_updateOutput(self, input, target)
end
self.output = self.outputTensor[1]
return self.output
end
if input:dim() == 1 then
if torch.isTensor(target) then target = target[1] end
self.output = -input[target]
if self.weights then
self.output = self.output*self.weights[target]
end
elseif input:dim() == 2 then
local output = 0
for i=1,target:size(1) do
if self.weights then
output = output - input[i][target[i]]*self.weights[target[i]]
else
output = output - input[i][target[i]]
end
end
if self.sizeAverage then
output = output / target:size(1)
end
self.output = output
else
error('matrix or vector expected')
end
return self.output
end
function ClassNLLCriterion:updateGradInput(input, target)
self.gradInput:resizeAs(input)
self.gradInput:zero()
if input:type() == 'torch.CudaTensor' and not self.weights then
if input:dim() == 1 then
self._target = self._target or input.new(1)
if type(target) == 'number' then
self._target[1] = target
else
self._target:copy(target)
end
input.nn.ClassNLLCriterion_updateGradInput(self, input, self._target)
else
input.nn.ClassNLLCriterion_updateGradInput(self, input, target)
end
return self.gradInput
end
if input:dim() == 1 then
if torch.isTensor(target) then target = target[1] end
self.gradInput[target] = -1
if self.weights then
self.gradInput[target] = self.gradInput[target]*self.weights[target]
end
else
local z = -1
if self.sizeAverage then
z = z / target:size(1)
end
for i=1,target:size(1) do
self.gradInput[i][target[i]] = z
if self.weights then
self.gradInput[i][target[i]] = self.gradInput[i][target[i]]*self.weights[target[i]]
end
end
end
return self.gradInput
end