How does the Flatten layer work in Keras?

Viewed 58944

I am using the TensorFlow backend.

I am applying a convolution, max-pooling, flatten and a dense layer sequentially. The convolution requires a 3D input (height, width, color_channels_depth).

After the convolution, this becomes (height, width, Number_of_filters).

After applying max-pooling height and width changes. But, after applying the flatten layer, what happens exactly? For example, if the input before flatten is (24, 24, 32), then how it flattens it out?

Is it sequential like (24 * 24) for height, weight for each filter number sequentially, or in some other way? An example would be appreciated with actual values.

3 Answers

Flattening a tensor means to remove all of the dimensions except for one.

A Flatten layer in Keras reshapes the tensor to have a shape that is equal to the number of elements contained in the tensor.

This is the same thing as making a 1d-array of elements.

For example in the VGG16 model you may find it easy to understand:

>>> model.summary()
Layer (type)                     Output Shape          Param #
================================================================
vgg16 (Model)                    (None, 4, 4, 512)     14714688
________________________________________________________________
flatten_1 (Flatten)              (None, 8192)          0
________________________________________________________________
dense_1 (Dense)                  (None, 256)           2097408
________________________________________________________________
dense_2 (Dense)                  (None, 1)             257
===============================================================

Note how flatten_1 layer shape is (None, 8192), where 8192 is actually 4*4*512.


PS, None means any dimension (or dynamic dimension), but you can typically read it as 1. You can find more details in here.

Related