Dropouts icon indicating copy to clipboard operation
Dropouts copied to clipboard

Use gpu for random number generation

Open the-moliver opened this issue 6 years ago • 1 comments

Thanks for posting this. However I found you can make things much faster by using the gpu for generating your random numbers as below:

class GaussianDropout(nn.Module):
    def __init__(self, alpha=1.0):
        super(GaussianDropout, self).__init__()
        self.alpha = torch.cuda.FloatTensor([alpha])
        
    def forward(self, x):
        """
        Sample noise   e ~ N(1, alpha)
        Multiply noise h = h_ * e
        """
        if self.train():
            epsilon = torch.cuda.FloatTensor(*x.size()).normal_() * self.alpha + 1

            epsilon = Variable(epsilon)

            return x * epsilon
        else:
            return x

the-moliver avatar Mar 29 '18 19:03 the-moliver

Thanks! I will merge PR if you make one :)

j-min avatar Apr 05 '18 07:04 j-min