Vectorize structured features
Preparing image, signal, or other multidimensional features for a fully connected layer.
TensorFlow · Class
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.
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.
tf.keras.layers.Flatten(data_format=None, **kwargs)| Name | Type | Description |
|---|---|---|
data_format | string or None | Controls 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. |
data_format to choose the convention for channel placement.data_format unset makes the layer consult the Keras configuration, if available.(batch, 1) rather than remaining rank one.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.
Preparing image, signal, or other multidimensional features for a fully connected layer.
Connecting convolutional feature maps to dense layers.
Keeping per-example processing independent while changing representation rank.
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).
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.
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.Layertf.keras.layers.Densetf.keras.Input