class Dropout(Module): def __init__(self, prob_keep=0.5, inplace=True): super().__init__("Dropout") self.prob_keep = prob_keep self.inplace = inplace def forward(self, x): if not self.train_mode: return x self.mask = np.random.binomial(size=x.shape, p=1 - self.prob_keep, n=1) if not self.inplace: x = x.copy() x[self.mask] = 0 x *= 1 / self.prob_keep return x