-
Notifications
You must be signed in to change notification settings - Fork 16
/
activations.py
159 lines (143 loc) · 4.59 KB
/
activations.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
from abc import ABC, abstractmethod
class Activation:
def __init__(self) -> None:
pass
@abstractmethod
def forward(self, Z: np.ndarray) -> np.ndarray:
"""
Forward pass for activation function.
args:
Z: input to the activation function
returns:
A: output of the activation function
"""
pass
@abstractmethod
def backward(self, dA: np.ndarray, Z: np.ndarray) -> np.ndarray:
"""
Backward pass for activation function.
args:
dA: derivative of the cost with respect to the activation
Z: input to the activation function
returns:
derivative of the cost with respect to Z
"""
pass
class Sigmoid(Activation):
def forward(self, Z: np.ndarray) -> np.ndarray:
"""
Sigmoid activation function.
args:
x: input to the activation function
returns:
sigmoid(x)
"""
# TODO: Implement sigmoid activation function
A = None
return A
def backward(self, dA: np.ndarray, Z: np.ndarray) -> np.ndarray:
"""
Backward pass for sigmoid activation function.
args:
dA: derivative of the cost with respect to the activation
Z: input to the activation function
returns:
derivative of the cost with respect to Z
"""
A = self.forward(Z)
# TODO: Implement backward pass for sigmoid activation function
dZ = None
return dZ
class ReLU(Activation):
def forward(self, Z: np.ndarray) -> np.ndarray:
"""
ReLU activation function.
args:
x: input to the activation function
returns:
relu(x)
"""
# TODO: Implement ReLU activation function
A = None
return A
def backward(self, dA: np.ndarray, Z: np.ndarray) -> np.ndarray:
"""
Backward pass for ReLU activation function.
args:
dA: derivative of the cost with respect to the activation
Z: input to the activation function
returns:
derivative of the cost with respect to Z
"""
# TODO: Implement backward pass for ReLU activation function
dZ = None
dZ[Z <= 0] = 0
return dZ
class Tanh(Activation):
def forward(self, Z: np.ndarray) -> np.ndarray:
"""
Tanh activation function.
args:
x: input to the activation function
returns:
tanh(x)
"""
# TODO: Implement tanh activation function
A = None
return A
def backward(self, dA: np.ndarray, Z: np.ndarray) -> np.ndarray:
"""
Backward pass for tanh activation function.
args:
dA: derivative of the cost with respect to the activation
Z: input to the activation function
returns:
derivative of the cost with respect to Z
"""
A = self.forward(Z)
# TODO: Implement backward pass for tanh activation function
dZ = None
return dZ
class LinearActivation(Activation):
def linear(Z: np.ndarray) -> np.ndarray:
"""
Linear activation function.
args:
x: input to the activation function
returns:
x
"""
# TODO: Implement linear activation function
A = None
return A
def backward(dA: np.ndarray, Z: np.ndarray) -> np.ndarray:
"""
Backward pass for linear activation function.
args:
dA: derivative of the cost with respect to the activation
Z: input to the activation function
returns:
derivative of the cost with respect to Z
"""
# TODO: Implement backward pass for linear activation function
dZ = None
return dZ
def get_activation(activation: str) -> tuple:
"""
Returns the activation function and its derivative.
args:
activation: activation function name
returns:
activation function and its derivative
"""
if activation == 'sigmoid':
return Sigmoid
elif activation == 'relu':
return ReLU
elif activation == 'tanh':
return Tanh
elif activation == 'linear':
return LinearActivation
else:
raise ValueError('Activation function not supported')