<br />

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

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
    )

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [Working with sparse tensors](https://www.tensorflow.org/guide/sparse_tensor) - [Migrate \`tf.feature_column\`s to Keras preprocessing layers](https://www.tensorflow.org/guide/migrate/migrating_feature_columns) - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) - [Introduction to Tensors](https://www.tensorflow.org/guide/tensor) - [Introduction to tensor slicing](https://www.tensorflow.org/guide/tensor_slicing) | - [Graph-based Neural Structured Learning in TFX](https://www.tensorflow.org/tfx/tutorials/tfx/neural_structured_learning) - [Text generation with an RNN](https://www.tensorflow.org/text/tutorials/text_generation) - [TFX Estimator Component Tutorial](https://www.tensorflow.org/tfx/tutorials/tfx/components) - [TFX Keras Component Tutorial](https://www.tensorflow.org/tfx/tutorials/tfx/components_keras) - [Licensed under the Apache License, Version 2.0 (the "License");](https://www.tensorflow.org/tfx/tutorials/tfx/gpt2_finetuning_and_conversion) |

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 />