-
Notifications
You must be signed in to change notification settings - Fork 966
/
MultiLabelMarginCriterion.lua
41 lines (38 loc) · 1.21 KB
/
MultiLabelMarginCriterion.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
local MultiLabelMarginCriterion, parent = torch.class('nn.MultiLabelMarginCriterion', 'nn.Criterion')
function MultiLabelMarginCriterion:__init()
parent.__init(self)
self.sizeAverage = true
self.isTarget = torch.Tensor()
end
function MultiLabelMarginCriterion:updateOutput(input, target)
if torch.typename(input):find('torch%.Cuda.*Tensor') then
target = torch.CudaLongTensor and target:cudaLong() or target
else
target = target:long()
end
self.output_tensor = self.output_tensor or input.new(1)
input.THNN.MultiLabelMarginCriterion_updateOutput(
input:cdata(),
target:cdata(),
self.output_tensor:cdata(),
self.isTarget:cdata(),
self.sizeAverage
)
self.output = self.output_tensor[1]
return self.output
end
function MultiLabelMarginCriterion:updateGradInput(input, target)
if torch.typename(input):find('torch%.Cuda.*Tensor') then
target = torch.CudaLongTensor and target:cudaLong() or target
else
target = target:long()
end
input.THNN.MultiLabelMarginCriterion_updateGradInput(
input:cdata(),
target:cdata(),
self.gradInput:cdata(),
self.isTarget:cdata(),
self.sizeAverage
)
return self.gradInput
end