Skip to content
Branch: master
Find file History
tranvohuy and dynamicwebpaige change tf.to_int32 to tf.cast (#6359)
tf.to_int32 raise deprecated warning.
change tf.to_int32(labels) to tf.cast(labels, tf.int32)
Latest commit 6765b16 Mar 26, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information. Rename programmers_guide/ to guide/ in tf-models. Jun 26, 2018 Add the TensorFlow official models directory (#2384) Sep 21, 2017 change tf.to_int32 to tf.cast (#6359) Mar 26, 2019
example5.png official/mnist: support savedmodel (#2967) Dec 18, 2017
examples.npy official/mnist: support savedmodel (#2967) Dec 18, 2017 Use float32 metrics in mnist_eager Jul 21, 2018 official/mnist: Use tf.keras.Sequential to simplify network definition. Apr 10, 2018 Glint everything (#3654) Mar 20, 2018 Update with recommended APIs (#5853) Dec 20, 2018

MNIST in TensorFlow

This directory builds a convolutional neural net to classify the MNIST dataset using the, tf.estimator.Estimator, and tf.layers APIs.


To begin, you'll simply need the latest version of TensorFlow installed. First make sure you've added the models folder to your Python path; otherwise you may encounter an error like ImportError: No module named official.mnist.

Then to train the model, run the following:


The model will begin training and will automatically evaluate itself on the validation data.

Illustrative unit tests and benchmarks can be run with:

python --benchmarks=.

Exporting the model

You can export the model into Tensorflow SavedModel format by using the argument --export_dir:

python --export_dir /tmp/mnist_saved_model

The SavedModel will be saved in a timestamped directory under /tmp/mnist_saved_model/ (e.g. /tmp/mnist_saved_model/1513630966/).

Getting predictions with SavedModel Use saved_model_cli to inspect and execute the SavedModel.

saved_model_cli run --dir /tmp/mnist_saved_model/TIMESTAMP --tag_set serve --signature_def classify --inputs image=examples.npy

examples.npy contains the data from example5.png and example3.png in a numpy array, in that order. The array values are normalized to values between 0 and 1.

The output should look similar to below:

Result for output key classes:
[5 3]
Result for output key probabilities:
[[  1.53558474e-07   1.95694142e-13   1.31193523e-09   5.47467265e-03
    5.85711526e-22   9.94520664e-01   3.48423509e-06   2.65365645e-17
    9.78631419e-07   3.15522470e-08]
 [  1.22413359e-04   5.87615965e-08   1.72251271e-06   9.39960718e-01
    3.30306928e-11   2.87386645e-02   2.82353517e-02   8.21146413e-18
    2.52568233e-03   4.15460236e-04]]

Experimental: Eager Execution

Eager execution (an preview feature in TensorFlow 1.5) is an imperative interface to TensorFlow. The exact same model defined in can be trained without creating a TensorFlow graph using:


Experimental: TPU Acceleration (and demonstrate training a neural network to classify digits on CPUs and GPUs. can be used to train the same model using TPUs for hardware acceleration. More information in the tensorflow/tpu repository.

You can’t perform that action at this time.