Skip to content

Commit 3a22bfd

Browse files
committed
Neural networks: Building a neural network that classifies MNIST images & time travel testing with that.
1 parent e9f8287 commit 3a22bfd

File tree

9 files changed

+82
-15
lines changed

9 files changed

+82
-15
lines changed
File renamed without changes.
File renamed without changes.

fundamentals/scientific_libraries/pandas_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
student_df = pd.DataFrame(student_dict)
1919
student_df.to_csv('it_student_k19.csv')
20-
my_data = pd.read_csv('it_student_k19.csv', index_col=0)
20+
my_data = pd.read_csv('../datasets/it_student_k19.csv', index_col=0)
2121

22-
income_data = pd.read_csv('us-income-annual.csv', delimiter=';')
22+
income_data = pd.read_csv('../datasets/us-income-annual.csv', delimiter=';')
2323
print('DataFrame shape is: ', income_data.shape)
2424
print(income_data.keys())
2525
print('\nRevenue:\n', income_data['Revenue']) # view a single column series

ml/supervised_learning/classifications/base_classifier.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
def classify(x, w):
12+
"""Predict the value of an unlabeled image"""
1213
y_hat = forward(x, w)
1314
labels = np.argmax(y_hat, axis=1)
1415
return labels.reshape(-1, 1)

ml/supervised_learning/classifications/our_own_mnist_lib.py

Lines changed: 7 additions & 11 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 one_hot_encoding
13+
from ml.util import prepend_bias, one_hot_encoding
1414

1515
TRAIN_IMAGE = "../../../fundamentals/datasets/mnist/train-images-idx3-ubyte.gz"
1616
TRAIN_LABEL = "../../../fundamentals/datasets/mnist/train-labels-idx1-ubyte.gz"
@@ -26,22 +26,17 @@ def load_images(filename):
2626
return all_pixels.reshape(n_images, columns * rows)
2727

2828

29-
def prepend_bias(x):
30-
"""
31-
Insert a column of 1s in the position 0 of X.
32-
:param x: X examples - a matrix
33-
:return: A new matrix
34-
"""
35-
return np.insert(x, 0, 1, axis=1)
36-
37-
3829
# 60000 images, each 785 (1 bias + 28 * 28) elements
3930
X_train_data = prepend_bias(load_images(TRAIN_IMAGE))
4031
print(X_train_data.shape)
4132
# 10000 images, each 785 (1 bias + 28 * 28) elements
4233
X_test_data = prepend_bias(load_images(TEST_IMAGE))
4334
print(X_test_data.shape)
4435

36+
# Neural networks #
37+
X_train = load_images(TRAIN_IMAGE) # no bias - 784 elements
38+
X_test = load_images(TEST_IMAGE) # no bias - 784 elements
39+
4540

4641
def load_labels(filename):
4742
"""Loads MNIST labels into a Numpy array, then molds that array into a
@@ -85,10 +80,11 @@ def show_a_digit(num):
8580
print(Y_train_data.shape)
8681

8782
Y_train_unencoded = load_labels(TRAIN_LABEL)
83+
# 60000 labels, each consisting of 10 one-hot encoded elements
8884
Y_train = one_hot_encoding(Y_train_unencoded, 10)
8985

9086
# 10000 labels
9187
Y_test_data = encode_fives(load_labels(TEST_LABEL))
9288
print(Y_test_data.shape)
93-
89+
# 10000 labels, each a single digit from 0 to 9
9490
Y_test = load_labels(TEST_LABEL)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Topic: Supervised Learning: Classifications in Action.
5+
# Neural Networks: Are leaps & bounds more powerful than perceptrons.
6+
# The Network's classification: A Classifier's Answers
7+
8+
import json
9+
10+
import numpy as np
11+
12+
from ml.supervised_learning.classifications import our_own_mnist_lib as mnist
13+
from ml.util import sigmoid, prepend_bias
14+
15+
16+
def loss(y, y_hat):
17+
"""The log loss - cross entropy loss for our binary classifier"""
18+
return -np.sum(y * np.log(y_hat)) / y.shape[0]
19+
20+
21+
def softmax(logits):
22+
"""The Softmax function"""
23+
exponentials = np.exp(logits)
24+
return exponentials / np.sum(exponentials, axis=1).reshape(-1, 1)
25+
26+
27+
def forward(x, w1, w2):
28+
"""
29+
Forward propagation.
30+
Calculates the system's outputs from the system's inputs.
31+
"""
32+
# Calculate the hidden layer
33+
h = sigmoid(np.matmul(prepend_bias(x), w1))
34+
# Calculate the output layer
35+
y_hat = softmax(np.matmul(prepend_bias(h), w2))
36+
return y_hat
37+
38+
39+
def classify(x, w1, w2):
40+
"""Predict the value of an unlabeled image"""
41+
y_hat = forward(x, w1, w2)
42+
labels = np.argmax(y_hat, axis=1)
43+
return labels.reshape(-1, 1)
44+
45+
46+
def report(iteration, x_train, y_train, x_test, y_test, w1, w2):
47+
"""To check how well the system is learning"""
48+
y_hat = forward(x_train, w1, w2)
49+
training_loss = loss(y_train, y_hat)
50+
classifications = classify(x_test, w1, w2)
51+
accuracy = np.average(classifications == y_test) * 100.0
52+
print("Iteration: %5d, Loss: %.6f, Accuracy: %.2f%%" % (iteration, training_loss, accuracy))
53+
54+
55+
# Time travel testing section #
56+
with open('weights.json') as f:
57+
weights = json.load(f)
58+
59+
weight1, weight2 = (np.array(weights[0]), np.array(weights[1]))
60+
report(0, mnist.X_train, mnist.Y_train, mnist.X_test, mnist.Y_test, weight1, weight2)
61+
# Iteration: 0, Loss: 2.221439, Accuracy: 43.19%

ml/supervised_learning/neural_networks/weights.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

ml/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def one_hot_encoding(y, a_number):
2727
return encoded_y
2828

2929

30+
def prepend_bias(x):
31+
"""
32+
Insert a column of 1s in the position 0 of X.
33+
:param x: X examples - a matrix
34+
:return: A new matrix
35+
"""
36+
return np.insert(x, 0, 1, axis=1)
37+
38+
3039
def test(x, _y, _w):
3140
total_examples = x.shape[0]
3241
correct_results = np.sum(classify(x, _w) == _y)

usecases/stock_market_financials/rudimentary_stock_screener.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
# Get csv data
1010
income_data = pd.read_csv(
11-
'D:/PythonInPractice/Coding-Deep-Learning/fundamentals/scientific_libraries/'
12-
'us-income-annual.csv', delimiter=';')
11+
'/fundamentals/datasets/us-income-annual.csv', delimiter=';')
1312
stock_prices = pd.read_csv('us-shareprices-daily.csv', delimiter=';')
1413
# Retrieve info
1514
print('Income data size is: ', income_data.shape)

0 commit comments

Comments
 (0)