TransWikia.com

Keras mnist.load_data() unshuffled?

Data Science Asked by user4779 on December 14, 2020

I want to do some data augmentation on MNIST and therefore must manually label the Y set. This can be automated if MNIST is unshuffled, however mnist.load_data() appears to be returned in a shuffled state. How do I load this with Keras or TensorFlow in an unshuffled state?

UPDATE: It’s trivial to unshuffle manually with:

perms=np.where(Y==0)
x[perms] #all X that are 0

Nevertheless would be nice if Keras had a way to import MNIST directly without shuffle.

One Answer

I do not believe that Keras returns the MNIST data shuffled. You can see that it is not the case below.

First I will define a function which we will use to plot the MNIST data nicely.

import matplotlib.pyplot as plt
%matplotlib inline

# utility function for showing images
def show_imgs(x_test, decoded_imgs=None, n=10):
    plt.figure(figsize=(20, 4))
    for i in range(n):
        ax = plt.subplot(2, n, i+1)
        plt.imshow(x_test[i].reshape(28,28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

        if decoded_imgs is not None:
            ax = plt.subplot(2, n, i+ 1 +n)
            plt.imshow(decoded_imgs[i].reshape(28,28))
            plt.gray()
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)
    plt.show()

Then let's reload and plot the MNIST dataset 4 separate times. We will see that the order of these is unchanged

for i in range(4):
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    show_imgs(x_train, x_test)
    print('Training labels: ', y_train[0:10])
    print('Testing labels : ', y_test[0:10])

enter image description here

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

enter image description here

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

enter image description here

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

enter image description here

Training labels: [5 0 4 1 9 2 1 3 1 4]
Testing labels : [7 2 1 0 4 1 4 9 5 9]

Answered by JahKnows on December 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP