0
$\begingroup$

In going through the different tutorials on CNN, auto encoders and so on I trained my self on the MNIST problem. The different images are stored in a 3D array which shape is (60000,28,28). In some tutorials for the first layer of CNN they use the Flatten function

keras.layers.Flatten(input_shape=())

but in other tutorial they transform the 3D Array in A 4D Array (60.000, 28,28,1 ) that I suppose is identical that use the Flatten function? Am I right ? Why there are two different approaches on this? Do Keras understand both of them? Many thanks

$\endgroup$
  • $\begingroup$ When they flatten the image, what kind of network architecture do they use, CNN or MLP (dense layers)? What happens if you write a command after flatten like print(MNISTDATA.shape)? $\endgroup$ – Dave Jun 19 at 19:09
0
$\begingroup$

In the first example there are 60000 images of 2828 which is a 2d grayscale image. But in order to use CNN your images must be 3 dimensinal with height, width and channel as a new dimension. So you have to resize your every 28 * 28 image into 2828*1 image before you can send it into your CNN layers.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I am trying to see the 4D array as a "matrix of matrices" so I have a matrix composed of (60000*28) matrices. Each one of these inner matrices is composed of a column vector (28,1). Using information about the channel is a different approach. I think that is clear but I am wondering if a the same time my reasoning is correct as well $\endgroup$ – AntonYellow Jun 20 at 19:19
0
$\begingroup$

It's because of the approach applied -

CNN - We use 2-D Convolution on the image, Hence we need the image in 2-D. In this case, we use the fully connected neural network at the end. hence flattening is done at the end.


CNN is used to reduce the dimension of the Image without losing the key information. A Simple neural network will become too big to train on image data. Although MNIST data are image but are a bit simple and you can use a simple neural network too. That's why you are seeing both kinds of approach on MNIST. You will not see a simple neural network on big coloured images.

See this excerpt from Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, by Aurélien Géron -
Why not simply use a deep neural network with fully connected layers for image recognition tasks? Unfortunately, although this works fine for small images (e.g., MNIST), it breaks down for larger images because of the huge number of parameters it requires. For example, a 100 × 100–pixel image has 10,000 pixels, and if the first layer has just 1,000 neurons (which already severely restricts the amount of information transmitted to the next layer), this means a total of 10 million connections. And that’s just the first layer. CNN's solve this problem using partially connected layers and weight sharing.


In simple neural network one row should be one record, so we flatten. Now each pixel is a column.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I would have said that the images 28x28 are transformed in 28 columns each composed of 28 elements in case of the 4D array. Each column in my view is composed of 28 elements. $\endgroup$ – AntonYellow Jun 20 at 18:38
  • $\begingroup$ In 4D - 1 is for Channel, 60k is for Data count, rest is image size. Why I will not say 28 column because these 28 columns for each image are independent i.e. Not like features. If we want row x column, it will be 784x1..Then it will be meaningful across 60k images $\endgroup$ – 10xAI Jun 20 at 23:40

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.