-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151_dropout_layer.py
More file actions
30 lines (21 loc) · 917 Bytes
/
151_dropout_layer.py
File metadata and controls
30 lines (21 loc) · 917 Bytes
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
import numpy as np
class DropoutLayer:
def __init__(self, p: float) -> None:
assert 0 <= p < 1, "Dropout probability must be in [0, 1)"
self.p = p
self.mask: np.ndarray | None = None
def forward(self, x: np.ndarray, training: bool = True) -> np.ndarray:
if training:
self.mask = np.random.binomial(1, 1 - self.p, size=x.shape).astype(x.dtype)
return x * self.mask / (1 - self.p)
return x
def backward(self, grad: np.ndarray) -> np.ndarray:
return grad * self.mask / (1 - self.p)
np.random.seed(42)
x = np.array([[1.0, 2.0], [3.0, 4.0]])
grad = np.array([[0.5, 0.2], [1.0, 2.0]])
dropout = DropoutLayer(0.2)
print(dropout.forward(x, training=True), dropout.forward(x, training=False), dropout.backward(grad))
"""
(array([[1.25, 0. ], [3.75, 5. ]]), array([[1., 2.], [3., 4.]]), array([[0.625, 0. ], [1.25 , 2.5 ]]))
"""