-
Notifications
You must be signed in to change notification settings - Fork 0
/
activationfunctions.py
46 lines (31 loc) · 1.05 KB
/
activationfunctions.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
import numpy as np
from module import Module
class Activation(Module):
def __init__(self) -> None:
self.activation = None
def __call__(self, x):
return self.forward(x)
def forward(self, x):
raise NotImplementedError
def backward(self, previous_gradient):
raise NotImplementedError
class Sigmoid(Activation):
def __init__(self) -> None:
self.activation = None
def __call__(self, x):
return self.forward(x)
def forward(self, x):
self.activation = 1 / (1 + np.exp(-x))
return self.activation
def backward(self, previous_gradient):
return previous_gradient * self.activation * (1 - self.activation)
class ReLU(Activation):
def __init__(self) -> None:
self.activation = None
def __call__(self, x):
return self.forward(x)
def forward(self, x):
self.activation = np.maximum(0, x)
return self.activation
def backward(self, previous_gradient):
return previous_gradient * np.where(self.activation > 0, 1, 0)