-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLP
153 lines (123 loc) · 6.37 KB
/
MLP
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
import numpy as np
import gzip
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
global train_images
train_images = np.zeros((60000,784))
global train_labels
train_labels = np.zeros((60000,1))
global train_labels_final
train_labels_final = np.zeros((60000,10))
global test_images
test_images = np.zeros((10000,784))
global test_labels
test_labels = np.zeros((10000,1))
global hidden_layer_weights , output_layer_weights, output_layer_weights_with_bias, modified_weights_hidden_to_output, \
modified_weights_input_to_hidden
global accuracy_count ,counter
global finding_accuracy
finding_accuracy = np.zeros((60000,1))
accuracy_count = counter = 0
Hidden_Layer_1 = 20
Hidden_Layer_2 = 50
Hidden_Layer_3 = 100
global Learning_rate
Learning_rate = 0.1
global Momentum
Momentum = 0.9
hidden_layer_weights = np.zeros((785,Hidden_Layer_1))
modified_weights_input_to_hidden = np.zeros((Hidden_Layer_1,785))
output_layer_weights_with_bias = np.zeros((Hidden_Layer_1+1,10))
output_layer_weights = np.zeros((Hidden_Layer_1,10))
modified_weights_hidden_to_output = np.zeros((10,Hidden_Layer_1+1))
############################### Pre-Processing #################################
class preProcessInput:
def __init__(self):
global train_labels
f = gzip.open("train-images.gz", 'rb')
self.train_images = np.frombuffer(f.read(), np.uint8, offset=16)
self.train_images = self.train_images.reshape(-1, 1, 28, 28)
self.train_images = self.train_images.reshape(self.train_images.shape[0],784)
self.train_images = self.train_images / np.float32(255)
self.train_images = np.append(self.train_images,np.ones((60000,1)),axis = 1)
f = gzip.open("train-labels.gz", 'rb')
train_labels = np.frombuffer(f.read(), np.uint8, offset=8)
train_labels = train_labels.reshape(60000,1)
self.train_labels_final = np.full((60000, 10), 0.1, dtype=float)
for row in range(len(train_labels)):
index = train_labels[row].astype(int)
self.train_labels_final[row][index[0]] += 0.8
f = gzip.open("test-images.gz", 'rb')
self.test_images = np.frombuffer(f.read(), np.uint8, offset=16)
self.test_images = self.test_images.reshape(-1, 1, 28, 28)
self.test_images = self.test_images.reshape(self.test_images.shape[0],784)
self.test_images = self.test_images / np.float32(255)
self.test_images = np.append(self.test_images,np.ones((10000,1)),axis = 1)
f = gzip.open("test-labels.gz", 'rb')
self.test_labels = np.frombuffer(f.read(), np.uint8, offset=8)
self.test_labels = self.test_labels.reshape(10000,1)
self.hidden_layer_weights = np.random.uniform(-0.05, 0.05, size=(785,Hidden_Layer_1))
self.output_layer_weights_with_bias = np.random.uniform(-0.05, 0.05, size=(Hidden_Layer_1+1,10))
self.output_layer_weights = self.output_layer_weights_with_bias[:20,:]
self.modified_weights_hidden_to_output = np.zeros((10,Hidden_Layer_1+1))
self.modified_weights_input_to_hidden = np.zeros((Hidden_Layer_1,785))
def sigmoid (self,calc_sigma):
return 1/(1 + np.exp(calc_sigma))
def matrix_multiplication_hidden_layer (self,row_test):
return np.matmul(self.train_images[row_test:row_test+1],self.hidden_layer_weights)
def matrix_multiplication_output_layer (self,step_1):
return np.matmul(step_1,self.output_layer_weights_with_bias)
def calc_error_term_output_to_hidden(self,row,y_final):
step_1 = np.ones((1,10))
step_1 = step_1 - y_final
global counter,accuracy_count
global finding_accuracy
#global train_labels,train_labels_final
step_2 = np.matmul(y_final,np.transpose(step_1))
step_3 = train_labels_final[row:row+1] - np.around(y_final,decimals = 1)
step_4 = np.matmul(step_2,step_3)
max_index = np.argmax(np.around(y_final,decimals = 1), axis = 1)
#print finding_accuracy.shape , train_labels.shape
finding_accuracy[row] = max_index
#print finding_accuracy[row]
#print self.train_labels[row]
if finding_accuracy[0] == 0:
accuracy_count += 1
else:
counter += 1
return step_4
def calc_error_term_hidden_to_input(self,hidden_layer_calc,delta_k):
step_1 = np.ones((1,20))
step_2 = step_1 - hidden_layer_calc
step_3 = np.matmul(hidden_layer_calc,np.transpose(step_2))
step_4 = np.matmul(self.output_layer_weights,np.transpose(delta_k))
step_5 = np.matmul(step_3,np.transpose(step_4))
return step_5
def update_weights_hidden_to_output(self,delta_k,hidden_layer_calc_after_bias):
step_1 = Learning_rate * (np.matmul(np.transpose(delta_k),hidden_layer_calc_after_bias))
step_2 = Momentum * self.modified_weights_hidden_to_output
self.modified_weights_hidden_to_output = step_1 + step_2
self.output_layer_weights_with_bias += np.transpose(self.modified_weights_hidden_to_output)
return self.output_layer_weights_with_bias
def update_weights_input_to_hidden(self,row,delta_h,hidden_layer_calc):
step_1 = Learning_rate * (np.matmul(np.transpose(delta_h),self.train_images[row_test:row_test+1]))
step_2 = Momentum * self.modified_weights_input_to_hidden
self.modified_weights_input_to_hidden = step_1 + step_2
self.hidden_layer_weights += np.transpose(self.modified_weights_input_to_hidden)
return self.hidden_layer_weights
def main():
object = preProcessInput()
for row in range(len(object.train_images)):
step_1 = object.matrix_multiplication_hidden_layer(row) #1*20
step_1_without_bias = object.sigmoid(step_1) #1*20
step_1 = np.append(step_1_without_bias,np.ones((1,1)),axis = 1) #1*21
step_2 = object.matrix_multiplication_output_layer(step_1)#1*10
step_2 = object.sigmoid(step_2)#1*10
step_3_delta_k = object.calc_error_term_output_to_hidden(row,step_2)
step_4 = object.calc_error_term_hidden_to_input(step_1_without_bias,step_3_delta_k);
step_5_update_weights_hidden_to_output = object.update_weights_hidden_to_output(step_3_delta_k,step_1)
step_6_update_weights_input_to_ohidden = object.update_weights_input_to_hidden(row,step_4,step_1_without_bias)
finding_accuracy.reshape(60000,1)
cfm = confusion_matrix(train_labels,finding_accuracy)
print cfm.shape
main()