-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomponentAnalysis.py
489 lines (308 loc) · 10.1 KB
/
componentAnalysis.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# coding: utf-8
# In[40]:
# Philip Tenteromano
# Machine Learning
# CISC 5800
# 4/7/2018
# hw3.py
# using jupyter notebook
# question 6 comment at the bottom
# In[41]:
import numpy as np
# #### Helper Functions
# In[42]:
# sigmoid helper function
def _g(h):
return 1/(1+ np.e ** -h)
# ## Neural Network
# In[43]:
# Question 1
# Neural Net function -> 3 layers
# outputs response from layer 3 (the output layer) => a single number
# x ==> the input values, shape [1,3]
# layerNW ==> an array of weights for each layer, N
def feedForward(x, layer1W, layer2W, layer3W):
# init output for layers
lay1Out = np.zeros((2,))
lay2Out = np.zeros((2,))
lay3Out = 0
# -- LAYER 1 --
# layer 1 -- use input values to feed into layer1
for i,v in enumerate(x):
lay1Out[0] += layer1W[0,i] * v
lay1Out[1] += layer1W[1,i] * v
# layer 1 -- add b values
lay1Out[0] += layer1W[0,-1]
lay1Out[1] += layer1W[1,-1]
# layer 1 -- finally, pass to sigmoid
lay1Out[0] = _g(lay1Out[0])
lay1Out[1] = _g(lay1Out[1])
# -- LAYER 2 --
# layer 2 -- use layer1 output to feed into layer2
for i,v in enumerate(lay1Out):
lay2Out[0] += layer2W[0,i] * v
lay2Out[1] += layer2W[1,i] * v
# add b values to layer 2
lay2Out[0] += layer2W[0,-1]
lay2Out[1] += layer2W[1,-1]
# layer 2 -- finally, pass to sigmoid
lay2Out[0] = _g(lay2Out[0])
lay2Out[1] = _g(lay2Out[1])
# -- LAYER 3 --
# layer 3 -- use input values to feed into layer1
for i,v in enumerate(lay2Out):
lay3Out += layer3W[i] * v
# layer 3 -- add b values
lay3Out += layer3W[-1]
# layer 3 -- finally, pass to sigmoid
lay3Out = _g(lay3Out)
# return the final output
return lay3Out
# In[44]:
# feed forward
# from homework example - for question one
# x = [1, 2, 3]
# lay1w=np.array([[2, 1, 0, 1],[ 0, 2, 1, 0]])
# lay2w=np.array([[0, -2, 0],[ -1, 0, 0]])
# lay3w=np.array([1,-1,0])
# output should be 0.4624
# In[45]:
# testQ1 = feedForward(x, lay1w, lay2w, lay3w)
# print(round(testQ1,4))
# ## Recurrent Neural Network
# In[46]:
# Question 2
# Neural Net, recurrent function -> 3 layers
# outputs response from layer 3 (the output layer) => a single number
# x ==> the input values, shape [t,3]
# layerNW ==> an array of weights for each layer, N
def feedForwardRecurrent(x, layer1W, layer2W, layer3W):
# init the array of t return values
t = x.shape[0]
results = np.zeros((t,))
# number of times to iterate
passes = 0
# init outputs
lay1Out = np.zeros((2,))
lay2Out = np.zeros((2,))
lay3Out = 0
# init pass outputs (lay3 will be in results array)
lay1PastOut = np.zeros((2,))
lay2PastOut = np.zeros((2,))
while passes < t:
# get current input
currentInput = x[:,passes]
# -- LAYER 1 --
# layer 1 -- use input values to feed into layer1
for i,v in enumerate(currentInput):
lay1Out[0] += layer1W[0,i] * v
lay1Out[1] += layer1W[1,i] * v
# layer 1 -- add b values
lay1Out[0] += layer1W[0,-1]
lay1Out[1] += layer1W[1,-1]
# layer 1 -- add the RECURRENT term
lay1Out[0] += layer1W[0][-2] * lay1PastOut[0]
lay1Out[1] += layer1W[1][-2] * lay1PastOut[1]
# layer 1 -- finally, pass to sigmoid
lay1Out[0] = _g(lay1Out[0])
lay1Out[1] = _g(lay1Out[1])
# -- LAYER 2 --
# layer 2 -- use layer1 output to feed into layer2
for i,v in enumerate(lay1Out):
lay2Out[0] += layer2W[0,i] * v
lay2Out[1] += layer2W[1,i] * v
# add b values to layer 2
lay2Out[0] += layer2W[0,-1]
lay2Out[1] += layer2W[1,-1]
# layer 2 -- add the RECURRENT term
lay2Out[0] += layer2W[0][-2] * lay2PastOut[0]
lay2Out[1] += layer2W[1][-2] * lay2PastOut[1]
# layer 2 -- finally, pass to sigmoid
lay2Out[0] = _g(lay2Out[0])
lay2Out[1] = _g(lay2Out[1])
# -- LAYER 3 --
# layer 3 -- use input values to feed into layer1
for i,v in enumerate(lay2Out):
lay3Out += layer3W[i] * v
# layer 3 -- add b values
lay3Out += layer3W[-1]
# layer 3 -- add the RECURRENT term
if passes > 0:
lay3Out += layer3W[-2] * results[passes-1]
# layer 3 -- finally, pass to sigmoid
lay3Out = _g(lay3Out)
# store output into results
results[passes] = lay3Out
# store output results to look back on
lay1PastOut = np.copy(lay1Out)
lay2PastOut = np.copy(lay2Out)
# reset the current outputs arrays to 0 (operations rely on += )
lay1Out.fill(0)
lay2Out.fill(0)
lay3Out = 0
# increment and repeat
passes += 1
return results
# In[47]:
# recurent feed word
# homework example - for question 2
# xMat=np.array([[1, 2, 3],[2, 0, 1],[ 3, 1, 0]])
# lay1w=np.array([[2, 1, 0, -2, 1],[ 0, 2, 1, 0, 0]])
# lay2w=np.array([[0, -2, -1, 0],[ -1, 0, 1, 0]])
# lay3w=np.array([1,-1,3,0])
# output should be 0.4624, 0.7724, 0.891 (t # of numbers)
# In[48]:
# testQ2 = feedForwardRecurrent(xMat, lay1w, lay2w, lay3w)
# for i in testQ2:
# print(round(i,4))
# ## Start Part 2: questions 3-6
# ### Take this out on submission - ALL IMPORTS AND PLOTS
# In[49]:
# import matplotlib.pyplot as plt
# %matplotlib inline
# ### Uncomment this if you want to import data
# In[50]:
# import csv
# reader = csv.reader(open("data/mnist_train.csv", "rt", encoding="utf8"), delimiter=",")
# x = list(reader)
# digits = np.array(x).astype('float')
# import scipy.io as sio
# loadedData = sio.loadmat('data/hw3NNfactors.mat')
# nnFactors = loadedData['nnFactors']
# ### Show an example digit
# In[51]:
# n = 12
# digitX = digits[n,1:].reshape((28,28))
# plt.imshow(digitX)
# ### Question3
# In[52]:
# Question 3
# Finding Weights 3
# single data point x, and components u, find the weights z
# x ==> the input pixel values, shape [1,784]
# uMat ==> matrix of the components, shape [784, 40]
# returns shape [1,40] array of z weights
def findWeights3(x, uMat):
# init the weights array, shape [1,40]
zWeights = np.zeros((1,40))
# get x as vector
x = x.reshape(784,)
# loop over every component, u
for i,u in enumerate(uMat.T):
# dot product the component with the data point
zWeights[0][i] = np.dot(u,x)
return zWeights
# In[53]:
# test Q3
# n = 0
# xi = digits[n,1:]
# testQ3 = findWeights3(xi, nnFactors)
# print(testQ3.shape)
# print(testQ3)
# # Question 4
# In[54]:
# Question 4
# Finding Weights 4
# single data point x, and components u, find the weights z
# x ==> the input pixel values, shape [1,784]
# uMat ==> matrix of the components, shape [784, 40]
# returns shape [1,40] array of z weights
def findWeights4(x, uMat):
# init the weights array, shape [1,40]
zWeights = np.zeros((1,40))
# get x as vector
x = x.reshape(784,)
# list of removed components
removedComponents = []
# conditional compoenents
iters = 0;
highest_zq = 1
# component to remove
# highest_index = -1
while iters < 40 and highest_zq > 0:
# loop over every component, u
for i,u in enumerate(uMat.T):
# check if component is removed
if i not in removedComponents:
# dot product the component with the data point
zWeights[0][i] = np.dot(u,x)
# get the highest weight from the (u @ x) dot product
highest_zq = np.amax(zWeights[0])
# store q - the specifc u, z index for removal
q = np.argmax(zWeights[0])
# update x by removing uq (x <- x – zq uq)
x = x - (zWeights[0][q] * uMat.T[q])
# take these weights and components out
zWeights[0][q] = 0
removedComponents.append(q);
iters += 1
return zWeights
# In[55]:
# xi = digits[n,1:]
# testQ4 = findWeights4(xi, nnFactors)
# print(testQ4.shape)
# print(testQ4)
# ### Plotting digit
# In[56]:
# n = 4
# xi = digits[n,1:]
# z = findWeights4(xi, nnFactors)
# test = np.zeros((784,))
# for i, u in enumerate(nnFactors.T):
# test += z[0][i] * u
# plt.imshow(test.reshape(28,28))
# In[57]:
# Question 5
# build new x from components and their weights
def xEstimate(z, uMat):
xi = np.zeros((784,))
for i, u in enumerate(uMat.T):
xi += z[0][i] * u
xi = xi.reshape(784,1)
return xi
# ### Start question 6
# In[58]:
# findWeights3 - getting average error on entire dataset
# numPoints = digits.shape[0]
# total = 0.
# avgMSE3 = 0.
# for x in digits[:,1:]:
# zWeights = findWeights3(x, nnFactors)
# newX = xEstimate(zWeights, nnFactors)
# newX = newX.reshape(784,)
# mse = sum((x - newX) ** 2)
# total += mse
# avgMSE3 = total / numPoints
# print(avgMSE3)
# In[59]:
# findWeights4 - getting average error on entire dataset
# numPoints = digits.shape[0]
# total4 = 0.
# avgMSE4 = 0.
# for x in digits[:,1:]:
# zWeights = findWeights4(x, nnFactors)
# newX = xEstimate(zWeights, nnFactors)
# newX = newX.reshape(784,)
# mse = sum((x - newX) ** 2)
# total4 += mse
# avgMSE4 = total4 / numPoints
# print(avgMSE4)
# In[60]:
# findWeights3 has much larger error than findWeights4
# avgMSE3 > avgMSE4
# In[61]:
# the difference in error between the algorithms
# print(avgMSE3 - avgMSE4)
# In[62]:
# findWeights4 is more than 3x better than findWeights3
# avgMSE3/avgMSE4
# In[63]:
# - QUESTION 6 - DESCRIPTION OF ERRORS
# As we can see above, the first method, findWeights3()
# tries to use every weight/component to reconstruct the x point
# Unfortunately, this results in a much higher avereage MSE over the dataset
#
# For findWeights4(), we can see that, by subtracting larger weights
# from x, we begin to bring other weights closer to 0, eventually below 0.
# This ends up being more effective, lowering the average MSE over the dataset
# by a factor of over 3