0
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 1. So, we can skip this step and just convert to float like X_train = X_train.astype('float32')? What do you say? $\endgroup$ – Dr Nisha Arora Sep 13 at 8:59
  • $\begingroup$ 2. If I'm not wrong, it means that if we don't convert integers to float there & then normalize it (or divide by 255), the resulting values would be coerced to be integer values so we may lose information. Please verify. $\endgroup$ – Dr Nisha Arora Sep 13 at 9:02
  • $\begingroup$ You can only omit 1 only when you intend to omit 2 as well. The only problem you would could face while omitting 1 and 2 would be slow convergence or maybe in some cases you would not converge. $\endgroup$ – Syed Nauyan Rashid Sep 13 at 10:25
  • $\begingroup$ Yes, that what I meant. As mentioned I just want to omit to reshape as it was redundant and instead using X_train = X_train.astype('float32') and X_test = X_test.astype('float32'). However, after re-running the same notebook this time I'm getting the shapes as mentioned by @from keras import michael. It's surpring for me too but those shape I was expecting earlier too, so finally I need reshaping as mentioned in the question. $\endgroup$ – Dr Nisha Arora Sep 13 at 10:33
  • $\begingroup$ Now, my concer is that datascience.stackexchange.com/questions/11704/… mentions the desired shape to be (nb_samples, nb_channels, width, height) and the course, I'm following is doing it like (nb_samples, width, height,nb_channels). Why is it different? $\endgroup$ – Dr Nisha Arora Sep 13 at 10:35
0
$\begingroup$

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)".

$\endgroup$
  • $\begingroup$ Does it depend on version? I'm using keras 2.2.5; which one is yours? $\endgroup$ – Dr Nisha Arora Sep 13 at 8:57
  • $\begingroup$ I was also surprised to see 6 tensor dimensions like "(60000, 10, 2, 2, 2, 2)". What is more surprising is that without making any change, I've just re-run the script & this time I got the same shapes as that of yours [which was expected result earlier too. Anyways, thanks. $\endgroup$ – Dr Nisha Arora Sep 13 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.