# tf.sparse.expand_dims

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/sparse_ops.py#L135-L238) |

Returns a tensor with an length 1 axis inserted at index `axis`.

#### View aliases


**Compat aliases for migration**

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

[`tf.compat.v1.sparse.expand_dims`](https://www.tensorflow.org/api_docs/python/tf/sparse/expand_dims)

<br />

    tf.sparse.expand_dims(
        sp_input, axis=None, name=None
    )

Given a tensor `input`, this operation inserts a dimension of length 1 at the
dimension index `axis` of `input`'s shape. The dimension index follows python
indexing rules: It's zero-based, a negative index it is counted backward
from the end.

This operation is useful to:

- Add an outer "batch" dimension to a single element.
- Align axes for broadcasting.
- To add an inner vector length axis to a tensor of scalars.

#### For example:

If you have a sparse tensor with shape `[height, width, depth]`:

    sp = tf.sparse.SparseTensor(indices=[[3,4,1]], values=[7,],
                                dense_shape=[10,10,3])

You can add an outer `batch` axis by passing `axis=0`:

    tf.sparse.expand_dims(sp, axis=0).shape.as_list()
    [1, 10, 10, 3]

The new axis location matches Python `list.insert(axis, 1)`:

    tf.sparse.expand_dims(sp, axis=1).shape.as_list()
    [10, 1, 10, 3]

Following standard python indexing rules, a negative `axis` counts from the
end so `axis=-1` adds an inner most dimension:

    tf.sparse.expand_dims(sp, axis=-1).shape.as_list()
    [10, 10, 3, 1]

> [!NOTE]
> **Note:** Unlike [`tf.expand_dims`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/expand_dims) this function includes a default value for the `axis`: `-1`. So if \`axis is not specified, an inner dimension is added.

    sp.shape.as_list()
    [10, 10, 3]
    tf.sparse.expand_dims(sp).shape.as_list()
    [10, 10, 3, 1]

This operation requires that `axis` is a valid index for `input.shape`,
following python indexing rules:

    -1-tf.rank(input) <= axis <= tf.rank(input)

This operation is related to:

- [`tf.expand_dims`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/expand_dims), which provides this functionality for dense tensors.
- [`tf.squeeze`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/squeeze), which removes dimensions of size 1, from dense tensors.
- [`tf.sparse.reshape`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/sparse/reshape), which provides more flexible reshaping capability.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `sp_input` | A `SparseTensor`. |
| `axis` | 0-D (scalar). Specifies the dimension index at which to expand the shape of `input`. Must be in the range `[-rank(sp_input) - 1, rank(sp_input)]`. Defaults to `-1`. |
| `name` | The name of the output `SparseTensor`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `SparseTensor` with the same data as `sp_input`, but its shape has an additional dimension of size 1 added. ||

<br />