-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdropout.py
35 lines (30 loc) · 966 Bytes
/
dropout.py
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
import torch
import torch.nn
generator = torch.Generator(device="cuda")
generator.manual_seed(42)
def createMask(x, dr):
mask = x.new().resize_as_(x).bernoulli_(1 - dr,generator=generator).div_(1 - dr).detach_()
# print('droprate='+str(dr))
return mask
class DropMask(torch.autograd.function.InplaceFunction):
@classmethod
def forward(cls, ctx, input, mask, train=False, inplace=False):
ctx.train = train
ctx.inplace = inplace
ctx.mask = mask
if not ctx.train:
return input
else:
if ctx.inplace:
ctx.mark_dirty(input)
output = input
else:
output = input.clone()
output.mul_(ctx.mask)
return output
@staticmethod
def backward(ctx, grad_output):
if ctx.train:
return grad_output * ctx.mask, None, None, None
else:
return grad_output, None, None, None