# tf.data.experimental.dense_to_sparse_batch

<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/data/experimental/ops/batching.py#L94-L142) |

A transformation that batches ragged elements into [`tf.sparse.SparseTensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/sparse/SparseTensor)s. (deprecated)

#### View aliases


**Compat aliases for migration**

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

[`tf.compat.v1.data.experimental.dense_to_sparse_batch`](https://www.tensorflow.org/api_docs/python/tf/data/experimental/dense_to_sparse_batch)

<br />

    tf.data.experimental.dense_to_sparse_batch(
        batch_size, row_shape
    )

> [!WARNING]
> **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use [`tf.data.Dataset.sparse_batch`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset#sparse_batch) instead.

Like [`Dataset.padded_batch()`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset#padded_batch), this transformation combines multiple
consecutive elements of the dataset, which might have different
shapes, into a single element. The resulting element has three
components (`indices`, `values`, and `dense_shape`), which
comprise a [`tf.sparse.SparseTensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/sparse/SparseTensor) that represents the same data. The
`row_shape` represents the dense shape of each row in the
resulting [`tf.sparse.SparseTensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/sparse/SparseTensor), to which the effective batch size is
prepended. For example:

    # NOTE: The following examples use `{ ... }` to represent the
    # contents of a dataset.
    a = { ['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c', 'd'] }

    a.apply(tf.data.experimental.dense_to_sparse_batch(
        batch_size=2, row_shape=[6])) ==
    {
        ([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1]],  # indices
         ['a', 'b', 'c', 'a', 'b'],                 # values
         [2, 6]),                                   # dense_shape
        ([[0, 0], [0, 1], [0, 2], [0, 3]],
         ['a', 'b', 'c', 'd'],
         [1, 6])
    }

<br />

<br />

<br />

| ## Args ||
|---|---|
| `batch_size` | A [`tf.int64`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf#int64) scalar [`tf.Tensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Tensor), representing the number of consecutive elements of this dataset to combine in a single batch. |
| `row_shape` | A [`tf.TensorShape`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/TensorShape) or [`tf.int64`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf#int64) vector tensor-like object representing the equivalent dense shape of a row in the resulting [`tf.sparse.SparseTensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/sparse/SparseTensor). Each element of this dataset must have the same rank as `row_shape`, and must have size less than or equal to `row_shape` in each dimension. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `Dataset` transformation function, which can be passed to [`tf.data.Dataset.apply`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset#apply). ||

<br />