# tf.sparse.retain

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/sparse/retain) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/sparse_ops.py#L1704-L1748) |

Retains specified non-empty values within a `SparseTensor`.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    tf.sparse.retain(
        sp_input, to_retain
    )

For example, if `sp_input` has shape `[4, 5]` and 4 non-empty string values:

    [0, 1]: a
    [0, 3]: b
    [2, 0]: c
    [3, 1]: d

and `to_retain = [True, False, False, True]`, then the output will
be a `SparseTensor` of shape `[4, 5]` with 2 non-empty values:

    [0, 1]: a
    [3, 1]: d

<br />

<br />

<br />

| ## Args ||
|---|---|
| `sp_input` | The input `SparseTensor` with `N` non-empty elements. |
| `to_retain` | A bool vector of length `N` with `M` true values. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `SparseTensor` with the same shape as the input and `M` non-empty elements corresponding to the true positions in `to_retain`. ||

<br />

<br />

<br />

<br />

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

<br />