-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropout.py
82 lines (61 loc) · 1.89 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import numpy as np
class Dropout:
"""
Dropout layer
"""
def __init__(
self,
rate: float = 0.5,
) -> None:
"""
Initialize the Dropout layer.
Parameters:
- dropout_rate (float): The dropout rate.
"""
if not 0 <= rate < 1:
raise ValueError("Dropout rate must be in the range [0, 1).")
self.__rate = rate
def forward(
self,
inputs: np.ndarray,
is_training: bool,
) -> np.ndarray:
"""
Forward propagation.
Parameters:
- inputs (np.ndarray): The inputs of the layer.
Returns:
- outputs (np.ndarray): The outputs of the layer.
"""
# If the layer is in training mode, compute the outputs using dropout mask
if is_training:
# Generate the dropout mask
self.__mask = (np.random.rand(*inputs.shape) > self.__rate) / (1.0 - self.__rate)
return inputs * self.__mask # Multiply the inputs by the dropout mask
# Otherwise, return the inputs
else:
return inputs
def backward(
self,
delta: np.ndarray,
) -> np.ndarray:
"""
Backward propagation.
Parameters:
- delta (np.ndarray): The delta of the layer.
Returns:
- delta (np.ndarray): The delta of the previous layer.
"""
return delta * self.__mask
def set_output_dim(self, input_dim: int) -> None:
"""
Set the output_dim attribute of the layer.
Parameters:
- input_dim (int): The input dimension of the layer.
"""
self.__output_dim = input_dim
@property
def output_dim(self) -> int:
return self.__output_dim
def __str__(self):
return f"Dropout(rate={self.__rate})"