MNIST image classfier using TensorFlow Estimator

This is a simple example of the classic MNIST classifier but this time using the TensorFlow Estimator API.

The example demonstrates the following:

  • Simple download of MNIST dataset and one-hot encoding of the labels.
  • How to define a CNN using the TensorFlow tf.layers API.
  • How to train and evaluate a model using the TensorFlow Estimator API.
  • How to make predictions with the trained model.


The network is made up of the following layers:
  • 2D convolution, 32 filters, each kernel is 5x5, ReLU activation.
  • Max pooling, 2x2 pooling window, stride length = 2.
  • 2D convolution, 64 filters, each kernel is 5x5, ReLU activation.
  • Max pooling, 2x2 pooling window, stride length = 2.
  • Flatten layer.
  • Fully connected layer with 128 nodes, ReLU activation.
  • Dropout layer with a dropout rate of 0.5 during training.
  • Fully connected layer with 10 nodes, no activation.


The accuracy achieved on the validation set of 1000 images, after 10 epochs of training, is typically over 99%.

The code is available on my Github page.

If you have any questions or comments about this design, please email me at designs@markharvey.info