-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoward_feed_neural_network_test.py
206 lines (148 loc) · 5.9 KB
/
foward_feed_neural_network_test.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
import unittest
import numpy as np
from foward_feed_neural_network import NeuralNetwork
from training_model import TrainingModel
from scipy.misc import imread
import matplotlib.pyplot as plt
import os
import pickle
PLOT_IMAGES = 0
# TODO: fill these out and learn how to do TDD!!!
class TestHappy(unittest.TestCase):
def test_foward_feed():
pass
def test_cost():
pass
def test_back_prop():
pass
def mnist_test():
images = []
y_vals = []
test_image_paths = os.listdir("mnist/Images/test")
# if mnist data has already been processed, then load the pickle, otherwise
# process the data
if os.path.isfile("mnist/mnist_test.pickle"):
with open("mnist/mnist_test.pickle", 'rb') as mnist_pickle:
pickled_data = pickle.load(mnist_pickle)
images = pickled_data
else:
#with open("mnist/train.csv") as train_results:
# for line in train_results:
# image_path, y_val = line.split(",")
#
# # read image from training data
# if image_path == "filename" or not os.path.exists(os.path.join("mnist/Images/test", image_path)):
# continue
#
# image = imread(os.path.join("mnist/Images/test", image_path), flatten=True)
#
# image = np.ndarray.flatten(image)
#
# images.append(image)
#
# y_val = int(y_val)
# y_vals.append(TrainingModel.digit_to_one_hot(10, y_val))
for path in test_image_paths:
image = imread(os.path.join("mnist/Images/test", path), flatten=True)
image = np.ndarray.flatten(image)
images.append(image)
images = np.array(images)
with open("mnist/mnist_test.pickle", 'wb') as mnist_pickle:
pickled_data = images
pickle.dump(pickled_data, mnist_pickle)
# normalize so we don't saturate the model
X = images/255
y = []
activation_fn = TrainingModel.sigmoid
with open("mnist/weights.pickle", 'rb') as mnist_pickle:
pickle_data = pickle.load(mnist_pickle)
weights, biases = pickle_data[0], pickle_data[1]
_,_,y_approx = NeuralNetwork.foward_feed(X, weights, biases, activation_fn)
total = 0
total_right = 0
for x_val, y_app in zip(X, y_approx):
y_digit_approx = TrainingModel.one_hot_to_digit(y_app)
print("approx: %d" % y_digit_approx)
print("\n")
# plot the image
plt.imshow(x_val.reshape(28, 28))
plt.show()
def mnist_train():
images = []
y_vals = []
# if mnist data has already been processed, then load the pickle, otherwise
# process the data
if os.path.isfile("mnist/mnist_train.pickle"):
with open("mnist/mnist_train.pickle", 'rb') as mnist_pickle:
pickled_data = pickle.load(mnist_pickle)
y_vals, images = pickled_data[0], pickled_data[1]
else:
with open("mnist/train.csv") as train_results:
for line in train_results:
image_path, y_val = line.split(",")
if image_path == "filename":
continue
# read image from training data
image = imread(os.path.join("mnist/Images/train/", image_path), flatten=True)
image = np.ndarray.flatten(image)
images.append(image)
y_val = int(y_val)
y_vals.append(TrainingModel.digit_to_one_hot(10, y_val))
y_vals = np.array(y_vals)
images = np.array(images)
pickled_data = [y_vals, images]
with open("mnist/mnist_train.pickle", 'wb') as mnist_pickle:
pickle.dump(pickled_data, mnist_pickle)
# TODO: regularize so we don't saturate the model
X = images/255
y = y_vals
learning_rate = 3
layers = np.array([784, 50, 50, 10])
weight_decay = 0
number_of_epochs = 10
# TODO: get relu to work
activation_fn = TrainingModel.sigmoid
batch_size = 100
plot_cost_graph = False
nn = NeuralNetwork(layers=layers, X=X, y=y, learning_rate=learning_rate, weight_decay=weight_decay, activation_fn=activation_fn, number_of_epochs=number_of_epochs, plot_cost_graph=plot_cost_graph, batch_size=batch_size)
nn.train_model()
with open("mnist/weights.pickle", 'wb') as mnist_weights_pickle:
pickle_data = [nn.weights, nn.biases]
pickle.dump(pickle_data, mnist_weights_pickle)
test = np.zeros(784)
total = 0
correct = 0
for i in range(X.shape[0]):
test = X[i]
actual = y[i]
_,_,approx = nn.foward_feed(test, nn.weights, nn.biases, activation_fn)
#print("approx: %d" % TrainingModel.one_hot_to_digit(approx))
#print("actual: %d" % TrainingModel.one_hot_to_digit(actual))
#print("\n")
if TrainingModel.one_hot_to_digit(approx) == TrainingModel.one_hot_to_digit(actual):
correct += 1
total += 1
if PLOT_IMAGES:
# plot the image
plt.imshow(images[i].reshape(28, 28))
plt.show()
accuracy = np.divide(correct, total) * 100
print("\ntraining accuracy: %f%%" % accuracy)
def dumb_example():
X = np.array([[2,1], [1,3], [4,4]]).reshape(3,2)
y = np.array([0, 1, 1]).reshape(3,1)
learning_rate = .3
layers = np.array([2, 3, 1])
weight_decay = 0
number_of_epochs = 10
activation_fn = TrainingModel.relu
number_of_batches = 3
nn = NeuralNetwork(layers=layers, X=X, y=y, learning_rate=learning_rate, weight_decay=weight_decay, activation_fn=activation_fn, number_of_epochs=number_of_epochs, plot_cost_graph=False, number_of_batches=number_of_batches)
test = np.array([[4, 4]]).reshape(1,2)
#_,_,approx = nn.foward_feed(test, nn.weights, nn.biases, activation_fn)
#
nn.train_model()
_,_,approx = nn.foward_feed(test, nn.weights, nn.biases, activation_fn)
#print(approx)
if __name__ == "__main__":
mnist_train()