Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Flatten layer work in Keras?

Tags:

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.

like image 954
ssg Avatar asked May 25 '17 09:05

ssg


People also ask

Why do we use flatten Keras?

flatten function flattens the multi-dimensional input tensors into a single dimension, so you can model your input layer and build your neural network model, then pass those data into every single neuron of the model effectively.

What does a flatten layer do?

Flatten layer is used to make the multidimensional input one-dimensional, commonly used in the transition from the convolution layer to the full connected layer.

How does Tensorflow flatten work?

Tensorflow flatten is the function available in the tensorflow library and reduces the input data into a single dimension instead of 2 dimensions. While doing so, it does not affect the batch size.

What is the difference between flatten layer and dense layer?

The Flatten layer converts the 28x28x32 output of the convolutional layer into a single one-dimensional vector, that can be used as input for a dense layer. The last dense layer has the most parameters. This layer connects every single output 'pixel' from the convolutional layer to the 10 output classes.


2 Answers

The Flatten() operator unrolls the values beginning at the last dimension (at least for Theano, which is "channels first", not "channels last" like TF. I can't run TensorFlow in my environment). This is equivalent to numpy.reshape with 'C' ordering:

‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest.

Here is a standalone example illustrating Flatten operator with the Keras Functional API. You should be able to easily adapt for your environment.

import numpy as np from keras.layers import Input, Flatten from keras.models import Model inputs = Input(shape=(3,2,4))  # Define a model consisting only of the Flatten operation prediction = Flatten()(inputs) model = Model(inputs=inputs, outputs=prediction)  X = np.arange(0,24).reshape(1,3,2,4) print(X) #[[[[ 0  1  2  3] #   [ 4  5  6  7]] # #  [[ 8  9 10 11] #   [12 13 14 15]] # #  [[16 17 18 19] #   [20 21 22 23]]]] model.predict(X) #array([[  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10., #         11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21., #         22.,  23.]], dtype=float32) 
like image 64
dhinckley Avatar answered Nov 02 '22 08:11

dhinckley


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.

like image 23
prosti Avatar answered Nov 02 '22 10:11

prosti