-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural2.py
165 lines (131 loc) · 5.75 KB
/
neural2.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
import numpy as np
import scipy.special as ss
import matplotlib.pyplot as plt
import neupy as ne
import pickle
class neuralNetwork:
#TODO : add one more layer + dropout
def __init__(self, inputnodes, hiddennodes, hiddennodes2, outputnodes, learningrate, epochs):
self.inodes = inputnodes
self.hnodes = hiddennodes
self.hnodes2 = hiddennodes2
self.onodes = outputnodes
self.lr = learningrate
self.epochs = epochs
self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
self.whh2 = np.random.normal(0.0, pow(self.hnodes2, -0.5), (self.hnodes2, self.hnodes))
self.wh2o = np.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes2))
self.activation_function = lambda x: ss.expit(x)
pass
def train(self, inputs_list, targets_list):
inputs = np.array(inputs_list, ndmin=2).T
targets = np.array(targets_list, ndmin=2).T
hidden_inputs = np.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden2_inputs = np.dot(self.whh2, hidden_outputs)
hidden2_outputs = self.activation_function(hidden2_inputs)
final_inputs = np.dot(self.wh2o, hidden2_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
# output_errors = self.cross_entropy(final_outputs, targets)
hidden2_errors = np.dot(self.wh2o.T, output_errors)
hidden_errors = np.dot(self.whh2.T,hidden2_errors)
# input_errors = np.dot(self.wih,hidden_errors)
self.wh2o += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden2_outputs))
self.whh2 += self.lr * np.dot((hidden2_errors * hidden2_outputs * (1.0 - hidden2_outputs)), np.transpose(hidden_outputs))
self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))
pass
def query(self, input_list):
inputs = np.array(input_list, ndmin = 2).T
hidden_inputs = np.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden2_inputs = np.dot(self.whh2,hidden_outputs)
hidden2_outputs = self.activation_function(hidden2_inputs)
final_inputs = np.dot(self.wh2o, hidden2_outputs)
final_outputs = self.activation_function(final_inputs)
# final_outputs = self.softmax(final_inputs)
return final_outputs
def deserialize(self,path):
f = open(path, 'rb')
weights = pickle.load(f)
self.wih = weights[0]
self.whh2 = weights[1]
self.wh2o = weights[2]
f.close()
def train_mnist(self,path):
file = load(path)
for e in range(self.epochs):
for record in file:
all_values = record.split(',')
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
targets = np.zeros(self.onodes) + 0.01
targets[int(all_values[0])] = 0.99
self.train(inputs,targets)
def test_mnist(self, path):
file2 = load(path)
for record in file2:
all_values = record.split(',')
correct_label = int(all_values[0])
# print(correct_label, "correct answer")
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
outputs = n.query(inputs)
label = np.argmax(outputs)
# print(label, "network's answer")
if( label == correct_label):
scorecard.append(1)
else:
scorecard.append(0)
scorecard_array = np.asarray(scorecard)
print ("performance = ", scorecard_array.sum() / scorecard_array.size)
def softmax(self,x):
return np.exp(x) / np.sum(np.exp(x), axis=0)
def cross_entropy(self,predictions, targets, epsilon=1e-12):
predictions = np.clip(predictions, epsilon, 1. - epsilon)
N = predictions.shape[0]
ce = -np.sum(targets*np.log(predictions+1e-9))/N
return ce
def load(path):
data_file = open(path, "r")
data_list = data_file.readlines()
data_file.close()
return data_list
if __name__ == "__main__":
# file = load("bigger/mnist_train.csv")
# file2 = load("bigger/mnist_test.csv")
input_nodes = 784
hidden_nodes = 150
hidden_nodes2 = 150
output_nodes = 10
learning_rate = 0.1
scorecard = []
epochs = 1
n = neuralNetwork(input_nodes,hidden_nodes,hidden_nodes2,output_nodes,learning_rate,epochs)
# train
# for e in range(epochs):
# for record in file:
# all_values = record.split(',')
# inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# targets = np.zeros(output_nodes) + 0.01
# targets[int(all_values[0])] = 0.99
# n.train(inputs,targets)
n.train_mnist("bigger/mnist_train.csv")
n.serialize()
# n.deserialize()
n.test_mnist("bigger/mnist_test.csv")
# print(file[0].split(',')[0])
# print(n.query(np.asfarray(file[0].split(',')[1:])))
#query
# for record in file2:
# all_values = record.split(',')
# correct_label = int(all_values[0])
# print(correct_label, "correct answer")
# inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# outputs = n.query(inputs)
# label = np.argmax(outputs)
# # print(label, "network's answer")
# if( label == correct_label):
# scorecard.append(1)
# else:
# scorecard.append(0)
# scorecard_array = np.asarray(scorecard)
# print ("performance = ", scorecard_array.sum() / scorecard_array.size)