Skip to content
Home » Keras Flatten with a DNN example in Python

Keras Flatten with a DNN example in Python

Keras Flatten

Keras flatten class is very important when you have to deal with multi-dimensional inputs such as image datasets. keras.layers.flatten function flattens the multi-dimensional input tensors into a single dimension, so you can structure your input layer and build your neural network model, then pass those data into every single neuron of the model effectively. Note that the flattened array elements do not include the batch dimension.

To understand the concept thoroughly let us use a practical example using the Fashion MNIST dataset.

keras flatten being used to create a deep neural network in python
Keras flatten is being used to create a deep neural network in python

Why do we have to flatten the input data?

The first layer of a neural network model should have the same shape as the input data. This is a general rule of thumb for neural networks.

Let’s focus on the fashion mnist dataset. It’s a dataset of 70,000 fashion images labeled under 10 different categories. (60,000 for training and 10,000 for testing) Each image in this dataset is 28 pixels in height and 28 pixels in width, hence each image contains a total of 784 pixels.

One way to pass this dataset into a neural network is to have 28 layers containing 28 neurons in each layer. But that is infeasible and not practical. Instead, we can use the Keras flatten class to flatten each image data into a 784 (28*28) * 1 array. Hence, we can create our input layer with 784 neurons to input the data into our neural network model.

Keras Flatten practical application with Fashion Mnist dataset

To understand and use keras.layers.flatten() I’m going to build a deep neural network with one input layer, one hidden layer, and one output layer. We need TensorFlow and Keras installed as minimum requirements. I’m using PyCharm, But you can use Spyder, Sublime Text, Jupoyter notebooks as you are pleased.

Importing Tensorflow and Keras.

import tensorflow as tf
from tensorflow import keras

Now it’s time to import the fashion mnist dataset.

inputdata = keras.datasets.fashion_mnist

Then we can define our training images, training labels and testing images, testing labels.

(train images, train_labels), (test_images, test_labels) = inputdata.load_data()

Shall we have a look at some unflattened data? Let me just print the data array of the 1st image in fashion mnist dataset.

print(train_images[0])
unflattened Fashion MNIST 1st image

Dear, neural network, don’t get confused! We are going to flatten these data for you!

Creating our deep neural network starting from flatenning input data using keras flatten

Finally, we can start building our model. We can do this by creating the individual layers required.

nnlmodel = keras.Sequential([
    keras.layers.flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10,activation='softmax')
    ])

If you investigated carefully, we have created 3 sequential layers using keras.layers.

1st Layer (Input Layer):

This is where we apply keras flatten in our neural network. As mentioned before, our input layer or the first layer of the model should have the same shape as our input data. Hence, it should have 784 neurons. We could do this by passing our flattened input data shape when as we create the first layer.

keras.layers.flatten(input_shape=(28,28)),

2nd Layer:

The second layer is a dense fully connected layer with 128 neurons. Here you can play with the neuron numbers. 128 seems to be working really well. We have used Rectified Linear Unit (Relu) as our activation function.

keras.layers.Dense(128, activation='relu'),

3rd Layer:

The 3rd layer is our output layer. Which is again a dense, fully connected layer. It has 10 neurons to output/predict our 10 different labels of fashion mnist data. Here in this layer, I’m using the softmax activation function to fire up the neurons.

keras.layers.Dense(10,activation='softmax')

Compiling our keras dnn model

nnlmodel.compile(optimizer="adam",loss="sparse_categorical_crossentropy", metrics=["accuracy"])

Training our keras model

Now it’s time for us to train the model. We have to train the model using the training potion of fashion mnist data. If this is the first time you are running the code, it’s going to download the dataset before training starts. This shouldn’t take long.

Batch size is not defined. So the model is going to train on the default batch size which is 32.

I have used 10 epochs, which seems to work very well. You could try increasing the number of epochs. But, if you look carefully you can see that we already see some diminishing returns as it reaches the 5th epoch. Hence for this tutorial, I’m going to stop from there.

The model should train fast as we don’t have a crazy huge number of model parameters.

nnlmodel.fit(train_images, train_labels, batch_size=1, epochs=5)
Keras model training under 5 epochs

Evaluating the model

To test the model accuracy we can evaluate the model using our test images and test labels.

test_loss, test_accuracy = nnlmodel.evaluate(test_images, test_labels)

Printing the model accuracy.

print("our model acc:", test_accuracy)

Simply because we aren’t saving the model, we will have to run the whole code again and train the model to test its accuracy.

caras dnn model accuracy

Conclusion

As we can see now, the model has a high accuracy which is around 0.802. It’s not the focus of this article to predict the data labels or increase the accuracy of the model even more if possible. Our main focus was to understand and investigate how keras flatten works. And then to use keras.layers.flatten() practically. To complete that mission we have created a simple deep neural architecture and trained it using Fashion MNIST data. And the model was successfully evaluated for its accuracy.

Want to become an ML and Data Science Expert? And get hired by reputed companies?

Enroll with Eduraka Today!

1 thought on “Keras Flatten with a DNN example in Python”

  1. Pingback: Edge AI (Deep Neural Networks for Edge AI) - Neural Net Lab

Leave a Reply