-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaskLayer.lua
42 lines (37 loc) · 1.11 KB
/
MaskLayer.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
--- Author: Ji Gao ---
local MaskLayer, Parent = torch.class('nn.MaskLayer', 'nn.Module')
function MaskLayer:__init(size, mask)
Parent.__init(self)
self.train = true
self.mask = torch.Tensor(size)
self.mask:copy(mask)
end
function MaskLayer:updateOutput(input)
self.output:resizeAs(input):copy(input)
-- print(self.output)
if not self.train then
if self.output:dim() > self.mask:dim() then
-- Batch
-- print(self.mask:repeatTensor(self.output:size(1), 1))
mask1 = torch.repeatTensor(self.mask,self.output:size(1), 1)
self.output:cmul(mask1)
else
self.output:cmul(self.mask)
end
end
return self.output
end
function MaskLayer:updateGradInput(input, gradOutput)
if self.train then
self.gradInput:resizeAs(gradOutput):copy(gradOutput)
else
self.gradInput:resizeAs(gradOutput):copy(gradOutput)
end
return self.gradInput
end
function MaskLayer:setmask(mask)
self.mask:resizeAs(mask):copy(mask)
end
function MaskLayer:__tostring__()
return string.format('%s', torch.type(self))
end