1

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
  • 123
  • 1
  • 8
  • Can you link the tutorial? The `X_train` nad `X_test` reshapes you are doing are different that the reshapes done in the [Reshaping of data for deep learning using Keras](https://datascience.stackexchange.com/questions/11704/reshaping-of-data-for-deep-learning-using-keras) – Mason Caiby Sep 12 '19 at 18:37
  • This is a part of IBM'S Coursera class week 4 [ungraded assignment]. https://labs.cognitiveclass.ai/tools/jupyterlab/lab/tree/labs/DL0101EN/DL0101EN-4-1-Convolutional-Neural-Networks-with-Keras-py-v1.0.ipynb The course is at https://www.coursera.org/learn/introduction-to-deep-learning-with-keras/home/welcome – Dr Nisha Arora Sep 13 '19 at 02:25
  • Now, my concern 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? – Dr Nisha Arora Sep 13 '19 at 17:46
  • I THINK this is because of an old Tensorflow convention. GPU input was one way, CPU input was another. I'll try to find a reference. – Mason Caiby Sep 13 '19 at 17:50
  • Sure, please share if you find some reference. Thanks again – Dr Nisha Arora Sep 13 '19 at 17:55
  • https://stackoverflow.com/questions/44774234/why-tensorflow-uses-channel-last-ordering-instead-of-row-major discusses the reason for the different order. Are all of your questions answered? – Mason Caiby Sep 13 '19 at 17:59
  • Thanks for the link. Yeah, all my questions are answered. Your comments helped me a lot. – Dr Nisha Arora Sep 14 '19 at 04:29

2 Answers2

1

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.

  • 1. So, we can skip this step and just convert to float like X_train = X_train.astype('float32')? What do you say? – Dr Nisha Arora Sep 13 '19 at 08:59
  • 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. – Dr Nisha Arora Sep 13 '19 at 09:02
  • 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. – Syed Nauyan Rashid Sep 13 '19 at 10:25
  • 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. – Dr Nisha Arora Sep 13 '19 at 10:33
  • Now, my concer is that https://datascience.stackexchange.com/questions/11704/reshaping-of-data-for-deep-learning-using-keras 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? – Dr Nisha Arora Sep 13 '19 at 10:35
  • I am not sure maybe it has something to with your hosted environment etc. – Syed Nauyan Rashid Sep 13 '19 at 10:38
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)".

  • Does it depend on version? I'm using keras 2.2.5; which one is yours? – Dr Nisha Arora Sep 13 '19 at 08:57
  • 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. – Dr Nisha Arora Sep 13 '19 at 09:29