2

I'm a beginner in Keras. I've loaded MNIST dataset in Keras and checked it's dimension. The code is

from keras.datasets import mnist

# load data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
print("Shape: ", X_train[0].shape)

And the output is

(60000, 28, 28, 1)
(60000, 10, 2, 2, 2, 2)
(10000, 28, 28, 1)
(10000, 10, 2, 2)
Shape:  (28, 28, 1)

As X_train and X_test are already in the shape (#sample, width, height, #channel). Do we still need reshaping? Why? The tutorial I'm following use the following reshaping code:

X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')

My second question is that why is .astype('float32') is used in code?

Lastly, I could not understand the output of print(y_train.shape) and print(y_test.shape).

Please suggest. I've already read Reshaping of data for deep learning using Keras however still my doubts are unclear.

Dr Nisha Arora
  • 133
  • 1
  • 8

2 Answers2

2

Answer 1 The reason for reshaping is to ensure that the input data to the model is in the correct shape. But you can say it using reshape is a replication of effort.

Answer 2 The reason for converting to float so that later we could normalize image between the range of 0-1 without loss of information.

0

I do not get those shapes. Using your code, I get:

(60000, 28, 28) (60000,) (10000, 28, 28) (10000,) Shape: (28, 28)

which makes more sense. For example, your output should not have 6 tensor dimensions: "(60000, 10, 2, 2, 2, 2)".