Algoteka
Log in to submit your own samples!

MNIST Digit Classification

Problem by oml1111
# Tech tags Title Creator Created date
1 0
TensorFlow
Keras
2022-10-11 17:56
View all samples for this language (1 verified and 0 unverified)

Simple 3-layer Neural Network 96% acc. | Python | TensorFlow Keras |

By oml1111 |
0 likes

Creates a simple 3-layer feedforward Neural Network with 500 and 150 hidden units in the hidden layers. Uses sigmoid activation functions.

It achieves 96.1% accuracy on the MNIST test set.

It requires about 30s to train on the CPU of the personal computer it was developed on.

Code

from tensorflow import keras

# Seed the Pseudorandom number generators to make the code deterministic
keras.utils.set_random_seed(0)

# Load the dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data(path="mnist.npz")
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

# Construct the neural network
inputs = keras.Input(shape=x_train.shape[1:])
hidden = keras.layers.Flatten()(inputs)
hidden = keras.layers.Dense(500, activation='sigmoid')(hidden)
hidden = keras.layers.Dense(150, activation='sigmoid')(hidden)
outputs = keras.layers.Dense(10, activation='softmax')(hidden)
model = keras.Model(inputs, outputs, name="simple_feedforward_sample")
model.compile(
    optimizer=keras.optimizers.RMSprop(learning_rate=1e-3),
    loss=keras.losses.CategoricalCrossentropy(),
    metrics=["acc"]
)

# Train the model on the training dataset
model.fit(x_train, y_train, batch_size=64, epochs=7, validation_split=0.2)

# Evaluate the accuracy of the model
model.evaluate(x_test, y_test)

Further reading

Neural Networks course Timetable (with slides and video lectures) - courses.cs.ut.ee

References

classes
tensorflow.keras.Input tensorflow.org
tensorflow.keras.Model tensorflow.org
tensorflow.keras.layers.Dense tensorflow.org
tensorflow.keras.layers.Flatten tensorflow.org
tensorflow.keras.losses.CategoricalCrossentropy tensorflow.org
tensorflow.keras.optimizers.RMSprop tensorflow.org
functions
tensorflow.keras.Model.compile tensorflow.org
tensorflow.keras.Model.evaluate tensorflow.org
tensorflow.keras.Model.fit tensorflow.org
tensorflow.keras.datasets.mnist.load_data tensorflow.org
tensorflow.keras.utils.set_random_seed tensorflow.org
tensorflow.keras.utils.to_categorical tensorflow.org

Problem Description

Create an algorithm that classifies the images in the MNIST dataset test set into the digits they correspond to.
You are allowed to use a training dataset to train your algorithm, but the training dataset should be separate from the test dataset.

Your algorithm should strive to maximize accuracy, and you should specify what accuracy you achieve. Your algorithm should also be generalizable, that is work well on the type of digit images provided in MNIST, even if they don't happen to be in the test set.

View sample discussion (0 comments)
View problem discussion (0 comments)