Loading MNIST

Luckily for us, an MNIST loading function that retrieves the MNIST data and loads it for us is built right into Keras. All we need to do is import keras.datasets.mnist and use the load_data() method, as shown in the following code:

(train_X, train_y), (test_X, test_y) = mnist.load_data()

The shape of train_X is 50,000 x 28 x 28. As we explained in the Model inputs and outputs section, we will need to flatten the 28x28 matrix into a 784 element vector. NumPy makes that pretty easy. The following code illustrates this technique:

train_X = train_X.reshape(-1, 784)

With that out of the way, we should think about scaling the input. Previously, we used scikit-learn's StandardScaler. There's no need to do so with MNIST. Since we know ...

Get Deep Learning Quick Reference 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.