Implement a Dropout Layer that randomly zeroes elements of an input tensor during training with probability p, and scales the remaining elements by 1 / (1 - p) (inverted dropout). During evaluation, the input is returned unchanged.
import numpy as np
def dropout(x: np.ndarray, p: float = 0.5, training: bool = True) -> np.ndarray:
if not training or p == 0.0:
return x
if p == 1.0:
return np.zeros_like(x)
mask = (np.random.rand(*x.shape) > p).astype(float)
return x * mask / (1 - p)p == 0, return the input unchanged.p == 1, all elements are dropped, so return zeros.1 - p.1 / (1 - p) so that expected values remain the same at test time (inverted dropout).