Skip to content

Commit 33f670a

Browse files
committed
Neural networks: The zen of Testing: Three sets strategy
1 parent 3502be9 commit 33f670a

File tree

5 files changed

+120
-22
lines changed

5 files changed

+120
-22
lines changed

ml/supervised_learning/classifications/our_own_mnist_lib.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import matplotlib.pyplot as plt
1111
import numpy as np
1212

13-
from ml.util import prepend_bias, one_hot_encoding
13+
from ml.util import prepend_bias, one_hot_encoding, load_images, load_labels
1414

1515
# TRAIN_IMAGE = "../../../fundamentals/datasets/mnist/train-images-idx3-ubyte.gz"
1616
# TRAIN_LABEL = "../../../fundamentals/datasets/mnist/train-labels-idx1-ubyte.gz"
@@ -23,14 +23,6 @@
2323
TEST_LABEL = "../../../../fundamentals/datasets/mnist/t10k-labels-idx1-ubyte.gz"
2424

2525

26-
def load_images(filename):
27-
"""Decodes images from MNIST's library files"""
28-
with gzip.open(filename, "rb") as f:
29-
_ignored, n_images, columns, rows = struct.unpack('>IIII', f.read(16))
30-
all_pixels = np.frombuffer(f.read(), dtype=np.uint8)
31-
return all_pixels.reshape(n_images, columns * rows)
32-
33-
3426
# 60000 images, each 785 (1 bias + 28 * 28) elements
3527
X_train_data = prepend_bias(load_images(TRAIN_IMAGE))
3628
print(X_train_data.shape)
@@ -43,16 +35,6 @@ def load_images(filename):
4335
X_test = load_images(TEST_IMAGE) # no bias - 784 elements
4436

4537

46-
def load_labels(filename):
47-
"""Loads MNIST labels into a Numpy array, then molds that array into a
48-
one-column matrix.
49-
"""
50-
with gzip.open(filename, "rb") as f:
51-
f.read(8)
52-
all_labels = f.read()
53-
return np.frombuffer(all_labels, dtype=np.uint8).reshape(-1, 1)
54-
55-
5638
def encode_fives(y):
5739
"""Convert all 5s to 1, and everything else to 0"""
5840
return (y == 5).astype(int)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Topic: Supervised Learning: The zen of Testing
5+
# A neural network implementation
6+
7+
# An MNIST data (pre-shuffled) loader that splits data into training, validation & test sets.
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
from ml.supervised_learning.neural_networks import training_the_network as tn
11+
from ml.util import load_images, load_labels, one_hot_encoding
12+
13+
TRAIN_IMAGE = "../../../fundamentals/datasets/mnist/train-images-idx3-ubyte.gz"
14+
TRAIN_LABEL = "../../../fundamentals/datasets/mnist/train-labels-idx1-ubyte.gz"
15+
TEST_IMAGE = "../../../fundamentals/datasets/mnist/t10k-images-idx3-ubyte.gz"
16+
TEST_LABEL = "../../../fundamentals/datasets/mnist/t10k-labels-idx1-ubyte.gz"
17+
18+
# X_train/ X_validation/ X_test: 60k/ 5k/ 5k images
19+
# Each image has 784 elements (28 * 28 pixels)
20+
X_train = load_images(TRAIN_IMAGE)
21+
X_test_all = load_images(TEST_IMAGE) # To ensure best practice: np.random.shuffle(X_test_all)
22+
X_validation, X_test = np.split(X_test_all, 2)
23+
24+
# 60K labels, each a single digit from 0 to 9
25+
Y_train_unencoded = load_labels(TRAIN_LABEL)
26+
# Y_train: 60k labels, each consisting of 10 one-hot-encoded elements
27+
Y_train = one_hot_encoding(Y_train_unencoded, 10)
28+
# Y_validation/ Y_test: 5k/ 5k labels, each a single digit from 0 to 9
29+
Y_test_all = load_labels(TEST_LABEL) # To ensure best practice: np.random.shuffle(Y_test_all)
30+
Y_validation, Y_test = np.split(Y_test_all, 2)
31+
32+
33+
# This loss() takes different parameters than the ones in other source files
34+
def loss(_x, _y, _w1, _w2):
35+
_y_hat, _ = tn.forward(_x, _w1, _w2)
36+
return -np.sum(_y * np.log(_y_hat)) / _y.shape[0]
37+
38+
39+
def train(x_train, y_train, x_test, y_test, _n_hidden_nodes, iterations, lr):
40+
n_input_variables = x_train.shape[1]
41+
n_classes = y_train.shape[1]
42+
# Initialize all the weights at zero
43+
# _w1 = np.zeros((n_input_variables + 1, _n_hidden_nodes))
44+
# _w2 = np.zeros((_n_hidden_nodes + 1, n_classes))
45+
# Initialize all the weights with good initialization
46+
_w1, _w2 = tn.initialize_weights(n_input_variables, _n_hidden_nodes, n_classes)
47+
_training_losses = []
48+
_test_losses = []
49+
50+
for i in range(iterations):
51+
y_hat_train, h = tn.forward(x_train, _w1, _w2)
52+
y_hat_test, _ = tn.forward(x_test, _w1, _w2)
53+
w1_gradient, w2_gradient = tn.back(x_train, y_train, y_hat_train, _w2, h)
54+
_w1 = _w1 - (w1_gradient * lr)
55+
_w2 = _w2 - (w2_gradient * lr)
56+
57+
training_loss = -np.sum(y_train * np.log(y_hat_train)) / y_train.shape[0]
58+
_training_losses.append(training_loss)
59+
test_loss = -np.sum(y_test * np.log(y_hat_test)) / y_test.shape[0]
60+
_test_losses.append(test_loss)
61+
62+
print("%5d > Training loss: %.5f - Test loss: %.5f" % (i, training_loss, test_loss))
63+
64+
return _training_losses, _test_losses, _w1, _w2
65+
66+
67+
training_losses, test_losses, w1, w2 = train(X_train, Y_train,
68+
X_test,
69+
one_hot_encoding(Y_test, 10),
70+
_n_hidden_nodes=200,
71+
iterations=10000,
72+
lr=0.01)
73+
training_accuracy = tn.accuracy(X_train, Y_train, w1, w2)
74+
test_accuracy = tn.accuracy(X_test, Y_test, w1, w2)
75+
print("Training accuracy: %.2f%%, Test accuracy: %.2f%%" % (training_accuracy, test_accuracy))
76+
77+
plt.plot(training_losses, label="Training set", color='blue', linestyle='-')
78+
plt.plot(test_losses, label="Test set", color='green', linestyle='--')
79+
plt.xlabel("Iterations", fontsize=30)
80+
plt.ylabel("Loss", fontsize=30)
81+
plt.xticks(fontsize=15)
82+
plt.yticks(fontsize=15)
83+
plt.legend(fontsize=30)
84+
plt.show()

ml/supervised_learning/neural_networks/training_the_network.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def report(iteration, x_train, y_train, x_test, y_test, _w1, _w2):
3737
y_hat, _ = forward(x_train, _w1, _w2)
3838
training_loss = loss(y_train, y_hat)
3939
classifications = classify(x_test, _w1, _w2)
40-
accuracy = np.average(classifications == y_test) * 100.0
41-
print("Iteration: %5d, Loss: %.8f, Accuracy: %.2f%%" % (iteration, training_loss, accuracy))
40+
_accuracy = np.average(classifications == y_test) * 100.0
41+
print("Iteration: %5d, Loss: %.8f, Accuracy: %.2f%%" % (iteration, training_loss, _accuracy))
4242

4343

4444
def initialize_weights(_n_input_variables, _n_hidden_nodes, _n_classes):
@@ -58,6 +58,10 @@ def initialize_weights(_n_input_variables, _n_hidden_nodes, _n_classes):
5858
return _w1, _w2
5959

6060

61+
def accuracy(_x, _y_unencoded, _w1, _w2):
62+
return np.average(classify(_x, _w1, _w2) == _y_unencoded) * 100.0
63+
64+
6165
def classify(_x, _w1, _w2):
6266
"""Predict the value of an unlabeled image"""
6367
y_hat, _ = forward(_x, _w1, _w2)
@@ -108,5 +112,4 @@ def softmax(logits):
108112
exponentials = np.exp(logits)
109113
return exponentials / np.sum(exponentials, axis=1).reshape(-1, 1)
110114

111-
112115
# w1, w2 = train(mnist.X_train, mnist.Y_train, mnist.X_test, mnist.Y_test, _n_hidden_nodes=200, iterations=10000, lr=0.01)
29.3 KB
Loading

ml/util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Contact me: [email protected] || +84393280504
33
#
44
# The helper functions
5+
import gzip
6+
import struct
7+
58
import matplotlib.pyplot as plt
69
import numpy as np
710
import seaborn as sns
@@ -124,6 +127,32 @@ def loss(x, y, w, b):
124127
return np.average((predict(x, w, b) - y) ** 2)
125128

126129

130+
def load_labels(fileName):
131+
""" Loads MNIST labels into a Numpy array, then molds
132+
that array into a one-column matrix.
133+
"""
134+
# Open and unzip the file of images:
135+
with gzip.open(fileName, 'rb') as f:
136+
# Skip the header bytes:
137+
f.read(8)
138+
# Read all the labels into a list:
139+
all_labels = f.read()
140+
# Reshape the list of labels into a one-column matrix
141+
return np.frombuffer(all_labels, dtype=np.uint8).reshape(-1, 1)
142+
143+
144+
def load_images(fileName):
145+
""" Decodes images from MNIST's library files """
146+
# Open and unzip the file of images
147+
with gzip.open(fileName, 'rb') as f:
148+
# Read the header information into a bunch of variables
149+
_ignored, n_images, columns, rows = struct.unpack('>IIII', f.read(16))
150+
# Read all the pixels into a NumPy array of bytes
151+
all_pixels = np.frombuffer(f.read(), dtype=np.uint8)
152+
# Reshape the pixels into a matrix where each line is an image
153+
return all_pixels.reshape(n_images, columns * rows)
154+
155+
127156
def load_text_dataset(text_dataset):
128157
return np.loadtxt(text_dataset, skiprows=1, unpack=True)
129158

0 commit comments

Comments
 (0)