# tf.sparse.to_dense

<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#L1671-L1724) |

Converts a `SparseTensor` into a dense tensor.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    tf.sparse.to_dense(
        sp_input, default_value=None, validate_indices=True, name=None
    )

For this sparse tensor with three non-empty values:

    sp_input = tf.sparse.SparseTensor(
      dense_shape=[3, 5],
      values=[7, 8, 9],
      indices =[[0, 1],
                [0, 3],
                [2, 0]])

The output will be a dense `[3, 5]` tensor with values:

    tf.sparse.to_dense(sp_input).numpy()
    array([[0, 7, 0, 8, 0],
           [0, 0, 0, 0, 0],
           [9, 0, 0, 0, 0]], dtype=int32)

> [!NOTE]
> **Note:** Indices must be without repeats. This is only tested if `validate_indices` is `True`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `sp_input` | The input `SparseTensor`. |
| `default_value` | Scalar value to set for indices not specified in `sp_input`. Defaults to zero. |
| `validate_indices` | A boolean value. If `True`, indices are checked to make sure they are sorted in lexicographic order and that there are no repeats. |
| `name` | A name prefix for the returned tensors (optional). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A dense tensor with shape `sp_input.dense_shape` and values specified by the non-empty values in `sp_input`. Indices not in `sp_input` are assigned `default_value`. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If `sp_input` is not a `SparseTensor`. |

<br />