forked from clementfarabet/lua---nnx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialSparseCriterion.lua
68 lines (62 loc) · 2.69 KB
/
SpatialSparseCriterion.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
local SpatialSparseCriterion, parent = torch.class('nn.SpatialSparseCriterion', 'nn.SparseCriterion')
function SpatialSparseCriterion:__init(...)
parent.__init(self)
xlua.unpack_class(self, {...},
'nn.SpatialSparseCriterion',
'A spatial extension of the SparseCriterion class.\n'
..' Provides a set of parameters to deal with spatial mini-batch training.',
{arg='nbGradients', type='number', help='number of gradients to backpropagate (-1:all, >=1:nb)', default=-1},
{arg='sizeAverage', type='number', help='if true, forward() returns an average instead of a sum of errors', default=true}
)
end
function SpatialSparseCriterion:forward(input)
self.fullOutput = self.fullOutput or torch.Tensor()
self.fullOutput:resize(input:size(2), input:size(3))
input.nn.SpatialSparseCriterion_forward(self, input)
if self.sizeAverage then
self.output = self.fullOutput:mean()
else
self.output = self.fullOutput:sum()
end
return self.output
end
function SpatialSparseCriterion:backward(input,target)
-- (1) retrieve adjusted target
target = self.target
-- (2) resize input gradient map
self.gradInput:resizeAs(input):zero()
-- (3) compute input gradients, based on the nbGradients param
if self.nbGradients == -1 then
-- dense gradients
input.nn.SpatialSparseCriterion_backward(self, input, self.gradInput)
elseif self.nbGradients == 1 then
-- only 1 gradient is computed, sampled in the center
self.fullGradInput = torch.Tensor() or self.fullGradInput
self.fullGradInput:resizeAs(input):zero()
input.nn.SpatialSparseCriterion_backward(self, input, self.fullGradInput)
local y = math.ceil(self.gradInput:size(2)/2)
local x = math.ceil(self.gradInput:size(3)/2)
self.gradInput:select(3,x):select(2,y):copy(self.fullGradInput:select(3,x):select(2,y))
else
-- only N gradients are computed, sampled in random locations
self.fullGradInput = torch.Tensor() or self.fullGradInput
self.fullGradInput:resizeAs(input):zero()
input.nn.SpatialSparseCriterion_backward(self, input, self.fullGradInput)
for i = 1,self.nbGradients do
local x = math.random(1,self.gradInput:size(1))
local y = math.random(1,self.gradInput:size(2))
self.gradInput:select(3,x):select(2,y):copy(self.fullGradInput:select(3,x):select(2,y))
end
end
return self.gradInput
end
function SpatialSparseCriterion:write(file)
parent.write(self, file)
file:writeDouble(self.resampleTarget)
file:writeInt(self.nbGradients)
end
function SpatialSparseCriterion:read(file)
parent.read(self, file)
self.resampleTarget= file:readDouble()
self.nbGradients = file:readInt()
end