-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_util.py
More file actions
142 lines (86 loc) · 3.45 KB
/
math_util.py
File metadata and controls
142 lines (86 loc) · 3.45 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
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
import numpy as np
# Various math functions, including a collection of activation functions used in NN.
class MyMath:
def tanh(x):
''' tanh function.
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is tanh of the corresponding element in array x
'''
return np.tanh(x)
def tanh_de(x):
''' Derivative of the tanh function.
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is tanh derivative of the corresponding element in array x
'''
tanhde = 1 - (np.tanh(x)**2)
return tanhde
def logis(x):
''' Logistic function.
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is logistic of
the corresponding element in array x
'''
#x = np.array(x)
def logis_f(x):
return (1 / (1 + np.exp(-x)))
logis_v = np.vectorize(logis_f)
return logis_v(x)
def logis_de(x):
''' Derivative of the logistic function.
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is logistic derivative of
the corresponding element in array x
'''
x = np.array(x)
#log_de = (1/(1+np.exp(-x)))* (np.exp(-x)/(1+np.exp(-x)))
log_de = MyMath.logis(x) * (1 - MyMath.logis(x))
return log_de
def iden(x):
''' Identity function
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is the same as
the corresponding element in array x
'''
x = np.array(x)
return x
def iden_de(x):
''' The derivative of the identity function
Support vectorized operation
x: an array type of real numbers
return: the numpy array of all ones of the same shape of x.
'''
x = np.array(x)
return np.ones(x.shape)
def relu(x):
''' The ReLU function
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is the max of: zero vs. the corresponding element in x.
'''
def relu_f(x):
return np.maximum(0,x)
relu_v = np.vectorize(relu_f)
return relu_v(x)
def _relu_de_scaler(x):
''' The derivative of the ReLU function. Scaler version.
x: a real number
return: 1, if x > 0; 0, otherwise.
'''
x = np.array(x)
x[x<0] = 0
x[x>0] = 1
return x
#return np.where(x > 0, 1, 0)
def relu_de(x):
''' The derivative of the ReLU function
Support vectorized operation
x: an array type of real numbers
return: the numpy array where every element is the _relu_de_scaler of the corresponding element in x.
'''
relu_d = np.vectorize(MyMath._relu_de_scaler)
return relu_d(x)