-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivation.py
More file actions
26 lines (20 loc) · 833 Bytes
/
Activation.py
File metadata and controls
26 lines (20 loc) · 833 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
from numbers import Number
import numpy as np
def sigmoid(x: Number | np.ndarray) -> np.float64 | np.ndarray:
return 1 / (1 + np.exp(-x))
def relu(x: Number | np.ndarray) -> np.float64 | np.ndarray:
return np.maximum(0, x)
def identity_function(x: Number | np.ndarray) -> Number | np.ndarray:
return x
def softmax(x: np.ndarray) -> np.ndarray:
c: np.float64 = np.max(a=x) # 避免出现上溢出
exp_a: np.ndarray = np.exp(x - c) # exp_a 中的各个元素一定在 (0, 1] 之间
sum_exp_a: np.float64 = np.sum(a=exp_a)
y: np.ndarray = exp_a / sum_exp_a
return y
if __name__ == "__main__":
print(relu(x=np.array(object=[-1, 0 ,1])))
print(softmax(x=np.array([0.3, 2.9, 4.0])))
a: np.ndarray = np.array(object=[1010, 1000, 990])
print(softmax(x=a))
print(type(sigmoid(x=0.0)))