Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

layers.Flatten

tf.keras.layers.Flatten turns each sample’s non-batch dimensions into one feature axis. It preserves the batch size and supports channels-last or channels-first dimension ordering.

How it works

Flatten converts every dimension after the batch dimension into one feature axis. It can interpret those dimensions using either channels-last or channels-first ordering, and when no format is supplied it follows the Keras image-data-format configuration.

Syntax

tf.keras.layers.Flatten(data_format=None, **kwargs)

Parameters

NameTypeDescription
data_formatstring or NoneControls whether channels are interpreted at the end (channels_last) or directly after the batch dimension (channels_first). If omitted, the Keras image-data-format setting is used.

Key points

  • Combines the non-batch dimensions of each input sample into a single feature dimension.
  • The first dimension represents separate examples and is preserved by the layer.
  • Use data_format to choose the convention for channel placement.
  • With channels-last ordering, channels occupy the final input axis.
  • With channels-first ordering, channels follow the batch axis.
  • Leaving data_format unset makes the layer consult the Keras configuration, if available.

Common mistakes

  • A batch-only input receives an added channel dimension, so the output becomes (batch, 1) rather than remaining rank one.

Example: Flatten feature dimensions

import tensorflow as tf

inputs = tf.keras.Input(shape=(10, 64))
outputs = tf.keras.layers.Flatten()(inputs)
model = tf.keras.Model(inputs, outputs)
print(model.output_shape)  # (None, 640)

Creates a model that converts each 10-by-64 sample into one 640-element feature vector while preserving the batch dimension.

When to use it

Vectorize structured features

Preparing image, signal, or other multidimensional features for a fully connected layer.

Bridge convolutional and dense blocks

Connecting convolutional feature maps to dense layers.

Preserve batch semantics

Keeping per-example processing independent while changing representation rank.

Interview questions

QWhat does tf.keras.layers.Flatten do to tensor shape?

Flatten preserves the leading batch dimension and combines the remaining dimensions into one axis. For an input shaped (None, 10, 64), the output is (None, 640).

Follow-up: What happens to an input shaped (batch,)? The layer adds a channel dimension, producing (batch, 1).

QWhy would you specify data_format?

The data_format argument accepts channels_last or channels_first. It describes whether channels are at the end or immediately after the batch dimension.

Follow-up: What happens when data_format is omitted? Keras uses the configured image_data_format, defaulting to channels_last.

QWhat is the constructor signature and main argument?

The layer signature is tf.keras.layers.Flatten(data_format=None, **kwargs). data_format controls dimension ordering, while additional keyword arguments are passed through the layer interface.

Follow-up: How can you verify the resulting size? Build or call the layer with a symbolic input and inspect its shape, such as (None, 640).

  • tf.keras.layers.Layer
  • tf.keras.layers.Dense
  • tf.keras.Input

Source: tf.keras.layers.Flatten | TensorFlow v2.16.1. Checked 2026-10-15.