SharedDropout.lua (1228B)
1 local SharedDropout, Parent = torch.class('nn.SharedDropout', 'nn.Dropout') 2 3 SharedDropout_noise = torch.Tensor() 4 5 function SharedDropout:__init(p,v1,inplace) 6 Parent.__init(self, p, v1, inplace) 7 self.noise = SharedDropout_noise 8 end 9 10 function SharedDropout:updateOutput(input) 11 if self.inplace then 12 self.output = input 13 else 14 self.output:resizeAs(input):copy(input) 15 end 16 if self.train then 17 -- SharedDropout_noise:resizeAs(input) 18 -- self.noise:resizeAs(input) 19 -- self.noise:bernoulli(1-self.p) 20 -- if self.v2 then 21 -- self.noise:div(1-self.p) 22 -- end 23 self.output:cmul(SharedDropout_noise) 24 elseif not self.v2 then 25 print("ERROR: v1 in SharedDropout NOT IMPLEMENTED") 26 self.output:mul(1-self.p) 27 end 28 return self.output 29 end 30 31 function SharedDropout:updateGradInput(input, gradOutput) 32 if self.train then 33 if self.inplace then 34 self.gradInput = gradOutput 35 else 36 self.gradInput:resizeAs(gradOutput):copy(gradOutput) 37 end 38 self.gradInput:cmul(SharedDropout_noise) -- simply mask the gradients with the noise vector 39 else 40 error('backprop only defined while training') 41 end 42 return self.gradInput 43 end