How to do it...

We will proceed with the recipe as follows:

  1. We will start by loading the necessary libraries. Note that we will also import the Python Image Library (PIL), to be able to plot a sample of the predicted outputs. And TensorFlow has a built-in method to load the MNIST dataset that we will use, as follows:
import random 
import numpy as np 
import tensorflow as tf 
import matplotlib.pyplot as plt 
from PIL import Image 
from tensorflow.examples.tutorials.mnist import input_data 
  1. Now, we will start a graph session and load the MNIST data in a one-hot encoded form:
sess = tf.Session() 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
One-hot encoding is a numerical representation of categorical values that are better ...

Get TensorFlow Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.