-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation_function.py
More file actions
52 lines (39 loc) · 1.27 KB
/
Copy pathactivation_function.py
File metadata and controls
52 lines (39 loc) · 1.27 KB
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
from typing import Iterable
import numpy as np
class ActivationFunction:
@staticmethod
def forward(x: int | float | Iterable) -> float | np.ndarray: pass
@staticmethod
def derivative(x: int | float | Iterable) -> float | np.ndarray: pass
class Sigmoid(ActivationFunction):
@staticmethod
def forward(x):
return 1/(1+np.exp(-x))
@staticmethod
def derivative(x):
return Sigmoid.forward(x)*(1-Sigmoid.forward(x))
class ReLU(ActivationFunction):
@staticmethod
def forward(x):
if type(x) == float or type(x) == int or type(x) == np.float64:
return x if x > 0 else 0
return np.array(list(map(lambda element: ReLU.forward(element), x)))
@staticmethod
def derivative(x):
if type(x) == float or type(x) == int or type(x) == np.float64:
return 1 if x > 0 else 0
return np.array(list(map(lambda element: ReLU.derivative(element), x)))
class Linear(ActivationFunction):
@staticmethod
def forward(x):
return x
@staticmethod
def derivative(x):
return np.ones(np.shape(x))
class Tanh(ActivationFunction):
@staticmethod
def forward(x):
return np.tanh(x)
@staticmethod
def derivative(x):
return 1 - Tanh.forward(x)**2