-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymathlib.py
298 lines (264 loc) · 8.13 KB
/
mymathlib.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# This file houses all of the required mathematical functions and
# matrix/vector operations used in the NeuralNetworkApp.
import math, random, copy, numbers
import numpy as np
# with support for up to 2 dimensional lists
def almostEqual(a, b):
if type(a) == type(b) == list and len(a) == len(b):
listIs2d = False
for item in a:
if isinstance(item, list):
listIs2d = True
for i in range(len(a)):
if listIs2d:
for j in range(len(a[0])):
if abs(a[i][j] - b[i][j]) > 10**-9:
return False
if not listIs2d:
if abs(a[i] - b[i]) > 10**-9:
return False
return True
else:
return abs(a - b) <= 10**-9
# Returns true if the specified (x,y) pair exists within the circle of radius r
def pointInCircle(r, circleCenter, point):
circleCenterX, circleCenterY = circleCenter
x1, y1 = point
return (circleCenterX - r < x1 < circleCenterX + r and
circleCenterY - r < y1 < circleCenterY + r)
# Returns true if the point is in the rectangular bounds specified.
# Does not assume sorted bounds
def pointInBounds(point, bounds):
ax1, ay1, ax2, ay2 = bounds
ax1, ax2 = sorted([ax1, ax2])
ay1, ay2 = sorted([ay1, ay2])
x1, y1 = point
return ax1 < x1 < ax2 and ay1 < y1 < ay2
# Returns the column of the 2d list as a 1d list
def getColumn(M, col):
return [row[col] for row in M]
# Bounds a value between an upper and a lower limit
def getInBounds(x, lower, upper):
if x < lower:
return lower
elif x > upper:
return upper
else:
return x
# Karlik, Bekir, and A. Vehbi Olgac. "Performance analysis of various activation
# functions in generalized MLP architectures of neural networks." International
# Journal of Artificial Intelligence and Expert Systems 1.4 (2011): 111-122.
# The hyperbolic tangent function, may take a 2d, 1d, or scalar input
# where order is the order of the derivative, and order 0 is the 0th order
# derivative
def tanH(x, order = 0):
if order == 0:
return np.tanh(x)
elif order == 1:
return 1 - np.tanh(x)**2
'''
if isinstance(x, numbers.Number):
if order == 0:
e = math.e
return (e**(2*x) - 1)/(e**(2*x) + 1)
elif order == 1:
return 1 - tanH(x)**2
elif type(x) == list:
return applyFunctionToMatrix(tanH, x, order)
'''
# The logistic function, may take a 2d, 1d, or scalar input
# where order is the order of the derivative, and order 0 is the 0th order
# derivative
def logistic(x, order = 0):
z = 1 / (1 + np.exp(-x))
if order == 0:
return z
elif order == 1:
return z * (1 - z)
# Applies the provided function to a 2d or 1d list
# where order is the order of the derivative, and order 0 is the 0th order
# derivative
def applyFunctionToMatrix(f, M, order):
rows, cols = len(M), len(M[0])
res = make2dList(rows, cols)
for i in range(rows):
for j in range(cols):
res[i][j] = f(M[i][j], order = order)
return res
# Returns the dot product of two vectors
def dotProduct(a, b):
if isinstance(a, np.ndarray):
return np.dot(a, b, out = None)
else:
if len(a) != len(b):
return None
res = 0
for i in range(len(a)):
res += (a[i][0]*b[i][0])
return res
# Returns the matrix product of two matrices (M x N)
def matProd(M, N):
if isinstance(M, np.ndarray):
return np.matmul(M, N)
else:
try:
res = make2dList(len(M), len(N[0]))
except:
return
for mRow in range(len(M)):
for nCol in range(len(N[0])):
for nRow in range(len(N)):
res[mRow][nCol] += M[mRow][nRow] * N[nRow][nCol]
return res
# Adds two vectors component-wise
def addVectors(a, b, sign = 1):
return a + sign*b
'''
if len(a) != len(b):
return None
res = []
for i in range(len(a)):
aElem = a[i] if isinstance(a[i], numbers.Number) else a[i][0]
bElem = b[i] if isinstance(b[i], numbers.Number) else b[i][0]
res.append(aElem + sign*bElem)
return transpose(res)
'''
# Transposes the given matrix
def transpose(M):
return np.transpose(M)
'''
# If there are no inner lists, then rows of that "2d list" = 1
hasRows = False
for elem in M:
if type(elem) == list:
hasRows = True
cols = len(M) if hasRows else 1
rows = len(M[0]) if hasRows else len(M)
res = make2dList(rows, cols)
for i in range(cols):
for j in range(rows):
if hasRows:
res[j][i] = M[i][j]
else:
res[j][i] = M[j]
return res
'''
# Performs MSE for a single training example
# where order is the order of the derivative, and order 0 is the 0th order
# derivative
def MSE(observed, actual, order = 0):
observed = observed.flatten()
actual = actual.flatten()
if order == 0:
# perform MSE over entire vector
diff = (addVectors(observed, actual, -1))
return 1/2 * dotProduct(diff, diff)
else:
# perform elementwise MSE prime
return addVectors(observed, actual, -1)
# Elementwise matrix product
def hadamardProd(A, B):
return np.array(A) * np.array(B)
# Performs elementwise multiplication on a matrix by a scalar
def multiplyMatrixByScalar(n, M):
if isinstance(M, np.ndarray):
return n * M
else:
res = M[:]
for i in range(len(M)):
for j in range(len(M[0])):
res[i][j] = M[i][j] * n
return res
# Returns the elementwise sum A + B
def matrixSum(A, B, sign = 1):
if isinstance(A, np.ndarray):
return A + sign*B
else:
res = []
for i in range(len(A)):
res.append([])
for j in range(len(A[0])):
res[i].append(A[i][j] + sign*B[i][j])
return res
# Returns an empty 2d list with the number of rows and columns specified
def make2dList(rows, cols):
return np.zeros((rows, cols))
'''
return [[0]*cols for row in range(rows)]
'''
# Returns a 2d list with each element sampled from the normal distribution
def makeGaussian2dList(rows, cols, mu, sigma):
return np.random.randn(rows, cols)
'''
L = make2dList(rows, cols)
for i in range(rows):
for j in range(cols):
L[i][j] = random.gauss(mu, sigma)
return L
'''
# Flattens a 2d list into a 1d list
def flatten2dList(lst):
'''
res = []
for row in range(len(lst)):
for col in range(len(lst[row])):
res.append(lst[row][col])
return res
'''
return [y for x in lst for y in x]
# Tests the functions in this library
def testMathHelpers():
# Test transpose
M = [[1, 1, 1, 6], [0, 2, -1, 3], [4, 0, 10, 42]]
actual = [[1,0,4], [1,2,0], [1,-1,10], [6,3,42]]
observed = transpose(M)
assert(actual == observed)
v = [1,2,3,4]
actual = [[1],[2],[3],[4]]
observed = transpose(v)
assert(actual == observed)
# Test matProd
M = [[1, 2],
[3, 4],
[5, 6]]
x = [[0],
[1]]
actual = [[0+2*1], [0+1*4], [0+6*1]]
observed = matProd(M, x)
assert(observed == actual)
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# Test hadamardProd
actual = [[1, 4, 9 ],
[16, 25, 36],
[49, 64, 81]]
observed = hadamardProd(A, B)
assert(observed == actual)
# Test matrixSum
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
actual = [[2, 4, 6],
[8, 10, 12],
[14, 16, 18]]
observed = matrixSum(A, B)
assert(observed == actual)
# Test MSE
a = [[20]]
y = [[1]]
actual = [[19]]
observed = MSE(a, y, order = 1)
assert(observed == actual)
# Test addVectors
a = [[1],[2],[3],[4]]
b = [[2],[3],[4],[5]]
actual = [[3],[5],[7],[9]]
observed = addVectors(a, b)
assert(observed == actual)