forked from AcademyNEXT2020/MNIST_digit_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_simple_with_comments.py
100 lines (81 loc) · 3.14 KB
/
mnist_simple_with_comments.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
# -------------------------
# LAST UPDATED:
# 21 June 2020 04:46 PM CDT
# Seth Cattanach
# -------------------------
# NOTES:
# Format changes and fork
# testing for AcademyNEXT
# -------------------------
# -------------------------
# PACKAGE IMPORTS
# -------------------------
# Importing the required Keras modules containing model and layers
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# -------------------------
# DATA COLLECTION
# -------------------------
# Load data and split into train/test sets
# using the MNIST Digit dataset (black & white)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# -------------------------
# DATA PRE-PROCESSING
# -------------------------
# Reshaping the array to
# 4-dims (match Keras API)
num_pixel_height = 28
num_pixel_width = 28
num_color_channels = 1
input_shape = (num_pixel_height, num_pixel_width, num_color_channels)
x_train = x_train.reshape(x_train.shape[0], num_pixel_height, num_pixel_width, num_color_channels)
x_test = x_test.reshape(x_test.shape[0], num_pixel_height, num_pixel_width, num_color_channels)
# -------------------------
# DATA PRE-PROCESSING
# -------------------------
# Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# -------------------------
# DATA PRE-PROCESSING
# -------------------------
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train = x_train / 255
x_test = x_test / 255
# -------------------------
# MODEL CREATION
# -------------------------
# Create a Sequential model and add neural network layers
# NOTE: final Dense layer MUST have 10 neurons b/c 10 classes (digits 0-9)
model = keras.Sequential()
model.add(layers.Conv2D(28, kernel_size=(5,5), input_shape=input_shape))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(layers.Dense(128, activation=tf.nn.relu))
model.add(layers.Dense(10,activation=tf.nn.softmax))
# -------------------------
# MODEL COMPILATION
# -------------------------
# Compile the model
# NOTE: "adam" optimizer (gradient descent - 1st & 2nd order)
# "loss" specifies which loss metric(s) we'd like to use
# ...[sparse_]cat_crossentropy = rough measure of std dev
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# -------------------------
# MODEL FITTING/TRAINING
# -------------------------
# Fit the training data to our compiled model
model.fit(x=x_train,y=y_train, epochs=10)
# -------------------------
# MODEL EVALUATION
# -------------------------
# Evaluate our model using default metric (accuracy)
# available metrics:
# ..."TopK" TopKCategoricalAccuracy(k=N)
# ...other options to discuss: [Root]MeanSquaredError
# ...kullback_leibler_divergence ("Relative Entropy")
results = model.evaluate(x_test, y_test)
print(dict(zip(model.metrics_names, results)))