How does the Flatten layer work in Keras?
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)
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.
It is sequential like 24*24*32 and reshape it as shown in following code.
def batch_flatten(x):
"""Turn a nD tensor into a 2D tensor with same 0th dimension.
In other words, it flattens each data samples of a batch.
# Arguments
x: A tensor or variable.
# Returns
A tensor.
"""
x = tf.reshape(x, tf.stack([-1, prod(shape(x)[1:])]))
return x