-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_driver.py
188 lines (165 loc) · 6.67 KB
/
mlp_driver.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
# =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#\|
# =#| Author: Danny Ly MugenKlaus|RedKlouds
# =#| File: mlp_driver.py
# =#| Date: 12/7/2017
# =#|
# =#| Program Desc:
# =#|
# =#| Usage:
# =#|
# =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#\|
import random
import numpy as np
from matplotlib import pyplot as plt
from multilayerperceptron import MultiLayerPerceptron
def generateNoise(original, pixelToChange):
"""
Transform the original image vectors with noise, given the number
of vectors to change.
:param original: Original Vector
:param pixelToChange: Number of pixels or indexs to change
:return: Changed vector
"""
copyMax = original['p'].copy() # make a hard copy
randomNums = np.random.permutation(pixelToChange)
for index in randomNums:
copyMax[0, index] = (copyMax[0, index] * -1)
return {'p': copyMax}
def testDataPerformance(network, data, learningRate, numSamples, epochs):
"""
Takes the data parameter and introduces noise, via changes the pixels randomly within the image
:param network:
:param data:
:return:
"""
# create the performance over the interval of 4 test,
# x axis will be [0,1,2,3,4]
# where 0:0, 1:2, 2:4, 3:6, 4:8 pixels removed
# so y_list will be a size of 5 each index has the respective items
y_plot = [0] * 5
x_axis_bar = [0, 2, 4, 6, 8]
numTest = 400
for iii in range(5): # for each pixel to remove category
# for each x axis
for j in range(numTest): # for each test
# run the test 50 times
# grab the random input vector to change
ranindex = random.randint(0, len(data) - 1)
# grab a random input to apply noise to
change = generateNoise(data[ranindex], x_axis_bar[iii]) # get random vector and change pix times
performance = network.predict(change, data[ranindex]['t'])
if performance:
y_plot[iii] += 1
y_plot[iii] = 1 - (y_plot[iii] / numTest)
figz = plt.figure(2)
plt.title(r"Test set Performance $\alpha$=%.3f $\eta$=%s epochs=%s" % (learningRate, numSamples, epochs))
plt.bar(x_axis_bar, y_plot)
plt.xlabel("Number of Pixels changed")
plt.ylabel("Mean performance per %s iterations per pixel" % numTest)
plt.grid()
plt.show()
def getData():
zero = {'p': np.matrix([-1, 1, 1, 1, 1, -1,
1, -1, -1, -1, -1, 1,
1, -1, -1, -1, -1, 1,
1, -1, -1, -1, -1, 1,
-1, 1, 1, 1, 1, -1]),
't': np.matrix([[0],
[0],
[0],
[0]])}
one = {'p': np.matrix([-1, -1, -1, -1, -1, -1,
1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1]),
't': np.matrix([[0],
[0],
[0],
[1]])}
two = {'p': np.matrix([1, -1, -1, -1, -1, -1,
1, -1, -1, 1, 1, 1,
1, -1, -1, 1, -1, 1,
-1, 1, 1, -1, -1, 1,
-1, -1, -1, -1, -1, 1]),
't': np.matrix([[0],
[0],
[1],
[0]])}
three = {'p': np.matrix([1, -1, -1, -1, -1, 1,
1, -1, 1, 1, -1, 1,
1, -1, 1, 1, -1, 1,
1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1]),
't': np.matrix([[0],
[0],
[1],
[1]])}
four = {'p': np.matrix([-1, -1, -1, 1, -1, -1,
-1, -1, 1, 1, -1, -1,
-1, 1, -1, 1, -1, -1,
1, 1, 1, 1, 1, 1,
-1, -1, -1, 1, -1, -1]),
't': np.matrix([[0],
[1],
[0],
[0]])}
five = {'p': np.matrix([1, 1, 1, -1, -1, 1,
1, -1, 1, -1, -1, 1,
1, -1, 1, -1, -1, 1,
1, -1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1]),
't': np.matrix([[0],
[1],
[0],
[1]])}
six = {'p': np.matrix([1, 1, 1, 1, 1, 1,
1, -1, -1, 1, -1, 1,
1, -1, -1, 1, -1, 1,
1, -1, -1, 1, 1, 1,
-1, -1, -1, -1, -1, -1]),
't': np.matrix([[0],
[1],
[1],
[0]])}
seven = {'p': np.matrix([1, -1, -1, -1, -1, 1,
1, -1, -1, -1, 1, -1,
1, -1, -1, 1, -1, -1,
1, -1, 1, -1, -1, -1,
1, 1, -1, -1, -1, -1]),
't': np.matrix([[0],
[1],
[1],
[1]])}
eight = {'p': np.matrix([1, 1, 1, 1, 1, 1,
1, -1, 1, -1, -1, 1,
1, -1, 1, -1, -1, 1,
1, -1, 1, -1, -1, 1,
1, 1, 1, 1, 1, 1]),
't': np.matrix([[1],
[0],
[0],
[0]])}
nine = {'p': np.matrix([1, 1, 1, -1, -1, -1,
1, -1, 1, -1, -1, -1,
1, -1, 1, -1, -1, -1,
1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1]),
't': np.matrix([[1],
[0],
[0],
[1]])}
FinalTestData = [zero, one, two, three, four, five, six, seven, eight, nine]
return FinalTestData
def test():
FinalTestData = getData()
test_epoch = 100
test_learning_rate = .5
test_number_sample = len(FinalTestData)
network = MultiLayerPerceptron(epoch=test_epoch, learning_rate=test_learning_rate)
network.train(FinalTestData)
network.plotPerformance()
testDataPerformance(network, FinalTestData, test_learning_rate, test_number_sample, test_epoch)
plt.close()
if __name__ == "__main__":
test()