Skip to content

Commit 1c0acd6

Browse files
author
yichen
committed
init nn
1 parent ffc83f8 commit 1c0acd6

File tree

4 files changed

+336
-0
lines changed

4 files changed

+336
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
*.out
22
*.class
3+
4+
*.gz
5+
MNIST_data
6+
venv

src/ml/basic/nn.py

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import numpy as np
5+
import random
6+
import matplotlib
7+
import os, struct
8+
from array import array as pyarray
9+
from numpy import append, array, int8, uint8, zeros
10+
matplotlib.use('TkAgg')
11+
import matplotlib.pyplot as pyplot
12+
import gzip
13+
import shutil
14+
15+
class NeuralNet(object):
16+
def __init__(self, sizes):
17+
self.sizes = sizes
18+
self.num_layers = len(sizes)
19+
self.w = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
20+
self.b = [np.random.randn(y, 1) for y in sizes[1:]]
21+
22+
def sigmoid(self, z):
23+
return 1.0 / (1.0 + np.exp(-z))
24+
25+
def sigmoid_prime(self, z):
26+
sigmoid = self.sigmoid(z)
27+
return sigmoid * (1 - sigmoid)
28+
29+
def farward(self, x):
30+
for b, w in zip(self.b, self.w):
31+
x = self.sigmoid(np.dot(w, x) + b)
32+
return x
33+
34+
# epoches: training times, eta: learning rate
35+
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
36+
if test_data:
37+
n_test = len(test_data)
38+
39+
n = len(training_data)
40+
print(epochs)
41+
for i in range(epochs):
42+
random.shuffle(training_data)
43+
mini_batchs = [training_data[k: k + mini_batch_size] for k in range(0, n, mini_batch_size)]
44+
for ind, mini_batch in enumerate(mini_batchs):
45+
print("batch {}".format(ind))
46+
self.update_mini_batch(mini_batch, eta)
47+
if test_data:
48+
print("Epoch {0}: {1} / {2}".format(i, self.evaluate(test_data), n_test))
49+
else:
50+
print("Epoch {0} complete".format(i))
51+
52+
def backprop(self, x, y):
53+
nabla_b = [np.zeros(b.shape) for b in self.b]
54+
nabla_w = [np.zeros(w.shape) for w in self.w]
55+
activation = x
56+
activations = [x]
57+
zs = []
58+
59+
for b, w in zip(self.b, self.w):
60+
z = np.dot(w, activation) + b
61+
zs.append(z)
62+
activation = self.sigmoid(z)
63+
activations.append(activation)
64+
65+
delta = self.cost_derivative(activations[-1], y) * self.sigmoid_prime(zs[-1])
66+
nabla_b[-1] = delta
67+
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
68+
69+
for l in range(2, self.num_layers):
70+
z = zs[-l]
71+
sp = self.sigmoid_prime(z)
72+
delta = np.dot(self.w[-l+1].transpose(), delta) * sp
73+
nabla_b[-l] = delta
74+
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
75+
return (nabla_b, nabla_w)
76+
77+
def update_mini_batch(self, mini_batch, eta):
78+
nabla_b = [np.zeros(b.shape) for b in self.b]
79+
nabla_w = [np.zeros(w.shape) for w in self.w]
80+
for x, y in mini_batch:
81+
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
82+
nabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
83+
nabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
84+
self_w = [w - (eta / len(mini_batch)) * nw for w, nw in zip(self.w, nabla_w)]
85+
self_b = [b - (eta / len(mini_batch)) * nb for b, nb in zip(self.b, nabla_b)]
86+
87+
def evaluate(self, test_data):
88+
test_results = [(np.argmax(self.farward(x)), y) for (x, y) in test_data]
89+
return sum(int(x == y) for (x, y) in test_results)
90+
91+
def cost_derivative(self, output_activations, y):
92+
return (output_activations-y)
93+
94+
def predict(self, data):
95+
value = self.farward(data)
96+
return value.tolist().index(max(value))
97+
98+
def save(self):
99+
pass
100+
101+
def load(self):
102+
pass
103+
104+
105+
def load_mnist(dataset="training_data", digits=np.arange(10), path="../mnist"):
106+
107+
if dataset == "training_data":
108+
fname_image = os.path.join(path, 'train-images-idx3-ubyte.gz')
109+
fname_label = os.path.join(path, 'train-labels-idx1-ubyte.gz')
110+
elif dataset == "testing_data":
111+
fname_image = os.path.join(path, 't10k-images-idx3-ubyte.gz')
112+
fname_label = os.path.join(path, 't10k-labels-idx1-ubyte.gz')
113+
else:
114+
raise ValueError("dataset must be 'training_data' or 'testing_data'")
115+
116+
# flbl = open(fname_label, 'rb')
117+
flbl = gzip.open(fname_label, 'rb')
118+
# magic_nr, size = struct.unpack(">II", flbl.read(8))
119+
magic_nr, size = struct.unpack(">2I", flbl.read(8))
120+
lbl = pyarray("b", flbl.read())
121+
# print(magic_nr, size)
122+
flbl.close()
123+
124+
# fimg = open(fname_image, 'rb')
125+
fimg = gzip.open(fname_image, 'rb')
126+
# print(fimg.tell()) # refer to https://docs.python.org/2/tutorial/inputosizeutput.html
127+
magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
128+
# magic_nr, size, rows, cols = struct.unpack(">4I", fimg.read(16))
129+
# print(magic_nr, size, rows, cols)
130+
img = pyarray("B", fimg.read())
131+
fimg.close()
132+
133+
ind = [ k for k in range(size) if lbl[k] in digits ]
134+
N = len(ind)
135+
# print(N)
136+
137+
images = zeros((N, rows, cols), dtype=uint8)
138+
labels = zeros((N, 1), dtype=int8)
139+
for i in range(N):
140+
images[i] = array(img[ ind[i] * rows * cols : (ind[i] + 1) * rows * cols ]).reshape((rows, cols))
141+
labels[i] = lbl[ind[i]]
142+
143+
return images, labels
144+
145+
def load_samples(dataset="training_data"):
146+
147+
image,label = load_mnist(dataset)
148+
149+
X = [np.reshape(x, (28 * 28, 1)) for x in image]
150+
X = [x / 255.0 for x in X]
151+
# print(np.array(X).shape)
152+
153+
# 5 -> [0,0,0,0,0,1.0,0,0,0]; 1 -> [0,1.0,0,0,0,0,0,0,0]
154+
def vectorized_Y(y):
155+
e = np.zeros((10, 1))
156+
e[y] = 1.0
157+
return e
158+
159+
if dataset == "training_data":
160+
Y = [vectorized_Y(y) for y in label]
161+
pair = list(zip(X, Y))
162+
return pair
163+
elif dataset == 'testing_data':
164+
pair = list(zip(X, label))
165+
# print(pair[:1], len(pair[1]))
166+
return pair
167+
else:
168+
print('Something wrong')
169+
170+
171+
if __name__ == "__main__":
172+
net=NeuralNet([3,4,2])
173+
# print('weight: ',net.w)
174+
# print('biases: ',net.b)
175+
176+
# x = np.linspace(-8.0,8.0, 2000)
177+
# y = net.sigmoid(x)
178+
# pyplot.plot(x,y)
179+
# # pyplot.show()
180+
181+
# array = np.arange(12)
182+
# random.shuffle(array)
183+
# array = array.reshape(3, 4)
184+
# print(array, np.argmax(array)) # max index
185+
# print("\nIndices of Max element : ", np.argmax(array, axis=0))
186+
# print("\nIndices of Max element : ", np.argmax(array, axis=1))
187+
188+
# data = ""
189+
# with gzip.open('../mnist/t10k-labels-idx1-ubyte.gz', 'rb') as f_in:
190+
# data = f_in.read()
191+
# # print(data)
192+
# print(np.zeros(3))
193+
# for item in array:
194+
# print(item)
195+
196+
# print(np.arange(10))
197+
# print(np.zeros((10, 1))) # 10 rows 1 col
198+
199+
INPUT = 28*28
200+
OUTPUT = 10
201+
net = NeuralNet([INPUT, 40, OUTPUT])
202+
203+
train_set = load_samples(dataset='training_data')
204+
test_set = load_samples(dataset='testing_data')
205+
206+
net.SGD(train_set, 13, 10000, 3.0, test_data=test_set)
207+
208+
correct = 0;
209+
for test_feature in test_set:
210+
if net.predict(test_feature[0]) == test_feature[1][0]:
211+
correct += 1
212+
print("accuracy: ", correct/len(test_set))

src/ml/basic/nn_tensor.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import tensorflow as tf
5+
import numpy as np
6+
import datetime
7+
8+
from tensorflow.examples.tutorials.mnist import input_data
9+
mnist = input_data.read_data_sets('/tmp/', one_hot=True)
10+
11+
12+
n_input_layer = 28*28 # input
13+
14+
n_layer_1 = 500 # hide layer
15+
n_layer_2 = 1000 # hide layer
16+
n_layer_3 = 300 # hide layer
17+
18+
n_output_layer = 10 # output
19+
20+
def neural_network(data):
21+
layer_1_w_b = {'w_':tf.Variable(tf.random_normal([n_input_layer, n_layer_1])), 'b_':tf.Variable(tf.random_normal([n_layer_1]))}
22+
layer_2_w_b = {'w_':tf.Variable(tf.random_normal([n_layer_1, n_layer_2])), 'b_':tf.Variable(tf.random_normal([n_layer_2]))}
23+
layer_3_w_b = {'w_':tf.Variable(tf.random_normal([n_layer_2, n_layer_3])), 'b_':tf.Variable(tf.random_normal([n_layer_3]))}
24+
layer_output_w_b = {'w_':tf.Variable(tf.random_normal([n_layer_3, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))}
25+
26+
# w·x+b
27+
layer_1 = tf.add(tf.matmul(data, layer_1_w_b['w_']), layer_1_w_b['b_'])
28+
layer_1 = tf.nn.relu(layer_1)
29+
layer_2 = tf.add(tf.matmul(layer_1, layer_2_w_b['w_']), layer_2_w_b['b_'])
30+
layer_2 = tf.nn.relu(layer_2 )
31+
layer_3 = tf.add(tf.matmul(layer_2, layer_3_w_b['w_']), layer_3_w_b['b_'])
32+
layer_3 = tf.nn.relu(layer_3 )
33+
layer_output = tf.add(tf.matmul(layer_3, layer_output_w_b['w_']), layer_output_w_b['b_'])
34+
35+
return layer_output
36+
37+
batch_size = 100
38+
39+
X = tf.placeholder('float', [None, 28*28])
40+
Y = tf.placeholder('float')
41+
42+
def train_neural_network(X, Y):
43+
predict = neural_network(X)
44+
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=Y))
45+
optimizer = tf.train.AdamOptimizer().minimize(cost_func) # learning rate default 0.001
46+
47+
epochs = 13
48+
with tf.Session() as session:
49+
session.run(tf.global_variables_initializer())
50+
epoch_loss = 0
51+
for epoch in range(epochs):
52+
for i in range( int(mnist.train.num_examples / batch_size) ):
53+
x, y = mnist.train.next_batch(batch_size)
54+
_, c = session.run([optimizer, cost_func], feed_dict={X:x, Y:y})
55+
epoch_loss += c
56+
print(epoch, ' : ', epoch_loss)
57+
58+
# print(predict.eval(feed_dict={X:[features]}))
59+
correct = tf.equal(tf.argmax(predict,1), tf.argmax(Y,1))
60+
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
61+
print('accuracy: ', accuracy.eval({X:mnist.test.images, Y:mnist.test.labels}))
62+
63+
t1 = datetime.datetime.now()
64+
train_neural_network(X,Y)
65+
t2 = datetime.datetime.now()
66+
print(datetime.timedelta(t1, t2))

src/ml/basic/svm.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from sklearn import svm
5+
import numpy as np
6+
import os, struct
7+
from array import array as pyarray
8+
from numpy import append, array, int8, uint8, zeros
9+
import pickle
10+
11+
def load_samples(dataset="training_data"):
12+
image,label = load_mnist(dataset)
13+
14+
X = [np.reshape(x,(28*28)) for x in image]
15+
X = [x/255.0 for x in X]
16+
#print(X.shape)
17+
18+
pair = list(zip(X, label))
19+
return pair
20+
21+
if __name__ == '__main__':
22+
23+
train_set = load_samples(dataset='training_data')
24+
test_set = load_samples(dataset='testing_data')
25+
26+
train_X = []
27+
train_Y = []
28+
for feature in train_set:
29+
train_X.append(feature[0])
30+
train_Y.append(feature[1][0])
31+
32+
clf = svm.SVR()
33+
clf.fit(train_X, train_Y)
34+
35+
#with open('minst.module', 'wb') as f:
36+
#pickle.dump(clf, f)
37+
38+
#with open('minst.module', 'rb') as f:
39+
# clf = pickle.load(f)
40+
41+
test_X = []
42+
test_Y = []
43+
for feature in test_set:
44+
test_X.append(feature[0])
45+
test_Y.append(feature[1][0])
46+
47+
correct = 0
48+
i = 0
49+
for feature in test_X:
50+
predict = clf.predict(np.array(feature).reshape(1, -1))
51+
if round(float(predict)) == test_Y[i]:
52+
correct += 1
53+
i = i + 1
54+
print("accuracy: ", correct/len(test_X))

0 commit comments

Comments
 (0)