# tf.sparse.from_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#L103-L132) |

Converts a dense tensor into a sparse tensor.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    tf.sparse.from_dense(
        tensor, name=None
    )

Only elements not equal to zero will be present in the result. The resulting
`SparseTensor` has the same dtype and shape as the input.

    sp = tf.sparse.from_dense([0, 0, 3, 0, 1])
    sp.shape.as_list()
    [5]
    sp.values.numpy()
    array([3, 1], dtype=int32)
    sp.indices.numpy()
    array([[2],
           [4]])

<br />

<br />

<br />

| ## Args ||
|---|---|
| `tensor` | A dense `Tensor` to be converted to a `SparseTensor`. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| The `SparseTensor`. ||

<br />