# tf.keras.backend.batch_flatten

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 2 version](https://www.tensorflow.org/api_docs/python/tf/keras/backend/batch_flatten) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/keras/backend.py#L2928-L2952) |

Turn a nD tensor into a 2D tensor with same 0th dimension.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.keras.backend.batch_flatten`](https://www.tensorflow.org/api_docs/python/tf/keras/backend/batch_flatten), \`tf.compat.v2.keras.backend.batch_flatten\`

<br />

    tf.keras.backend.batch_flatten(
        x
    )

In other words, it flattens each data samples of a batch.

<br />

<br />

<br />

| ## Arguments ||
|---|---|
| `x` | A tensor or variable. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tensor. ||

<br />

#### Examples:

Flattening a 3D tensor to 2D by collapsing the last dimension.

    from tensorflow.keras import backend as K
    x_batch = K.ones(shape=(2, 3, 4, 5))
    x_batch_flatten = K.batch_flatten(x_batch)
    K.int_shape(x_batch_flatten)
    (2, 60)