-1

I am using MNIST dataset from keras -

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

Before loading the data, how can I shuffle this dataset?

2 Answers2

1

I think it is not possible to do this with keras.datasets.mnist.load_data().

You have different options:

  1. Download the dataset yourself and load it directly from your files. Then shuffle and split it

  2. Use tensorflow_datasets. Example:
    import tensorflow_dataset as tfds

    (df_train,df_test)=tfds.load('minst',split=['train','test'],shuffle_files=True, as_supervised=True)

  3. Build a dataset from your x_train,x_test,y_train,y_test. Then shuffel and split it again.

Fabian
  • 756
  • 5
  • 12
0

You can use as tensorflow suggests,

import tensorflow as tf
import tensorflow_datasets as tfds 
(ds_train, ds_test), ds_info = tfds.load(
    'mnist',
    split=['train', 'test'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
) 

I am not sure but you can use this and convert into numpy as follows:

    import numpy as np
    ds_test_np = np.array(list(ds_test.as_numpy_iterator()))
    ds_train_np = np.array(list(ds_train.as_numpy_iterator()))