-
Notifications
You must be signed in to change notification settings - Fork 0
/
Criterion.lua
56 lines (45 loc) · 1.22 KB
/
Criterion.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
local Criterion = torch.class('nn.Criterion')
function Criterion:__init()
self.gradInput = torch.Tensor()
self.output = 0
end
function Criterion:updateOutput(input, target)
end
function Criterion:forward(input, target)
return self:updateOutput(input, target)
end
function Criterion:backward(input, target)
return self:updateGradInput(input, target)
end
function Criterion:updateGradInput(input, target)
end
function Criterion:clone()
local f = torch.MemoryFile("rw"):binary()
f:writeObject(self)
f:seek(1)
local clone = f:readObject()
f:close()
return clone
end
function Criterion:type(type)
assert(type, 'Criterion: must provide a type to convert to')
-- find all tensors and convert them
for key,param in pairs(self) do
self[key] = nn.utils.recursiveType(param, type)
end
return self
end
function Criterion:float()
return self:type('torch.FloatTensor')
end
function Criterion:double()
return self:type('torch.DoubleTensor')
end
function Criterion:cuda()
return self:type('torch.CudaTensor')
end
function Criterion:__call__(input, target)
self.output = self:forward(input, target)
self.gradInput = self:backward(input, target)
return self.output, self.gradInput
end