tf.data.experimental.dense_to_sparse_batch
Stay organized with collections
Save and categorize content based on your preferences.
A transformation that batches ragged elements into tf.SparseTensor
s.
tf.data.experimental.dense_to_sparse_batch(
batch_size, row_shape
)
Like 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.SparseTensor
that represents the same data. The
row_shape
represents the dense shape of each row in the
resulting tf.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])
}
Args |
batch_size
|
A tf.int64 scalar tf.Tensor , representing the number of
consecutive elements of this dataset to combine in a single batch.
|
row_shape
|
A tf.TensorShape or tf.int64 vector tensor-like object
representing the equivalent dense shape of a row in the resulting
tf.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.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.data.experimental.dense_to_sparse_batch\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 2 version](/api_docs/python/tf/data/experimental/dense_to_sparse_batch) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/data/experimental/ops/batching.py#L33-L80) |\n\nA transformation that batches ragged elements into [`tf.SparseTensor`](../../../tf/sparse/SparseTensor)s.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.data.experimental.dense_to_sparse_batch`](/api_docs/python/tf/data/experimental/dense_to_sparse_batch), \\`tf.compat.v2.data.experimental.dense_to_sparse_batch\\`\n\n\u003cbr /\u003e\n\n tf.data.experimental.dense_to_sparse_batch(\n batch_size, row_shape\n )\n\nLike [`Dataset.padded_batch()`](../../../tf/data/Dataset#padded_batch), this transformation combines multiple\nconsecutive elements of the dataset, which might have different\nshapes, into a single element. The resulting element has three\ncomponents (`indices`, `values`, and `dense_shape`), which\ncomprise a [`tf.SparseTensor`](../../../tf/sparse/SparseTensor) that represents the same data. The\n`row_shape` represents the dense shape of each row in the\nresulting [`tf.SparseTensor`](../../../tf/sparse/SparseTensor), to which the effective batch size is\nprepended. For example: \n\n # NOTE: The following examples use `{ ... }` to represent the\n # contents of a dataset.\n a = { ['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c', 'd'] }\n\n a.apply(tf.data.experimental.dense_to_sparse_batch(\n batch_size=2, row_shape=[6])) ==\n {\n ([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1]], # indices\n ['a', 'b', 'c', 'a', 'b'], # values\n [2, 6]), # dense_shape\n ([[0, 0], [0, 1], [0, 2], [0, 3]],\n ['a', 'b', 'c', 'd'],\n [1, 6])\n }\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `batch_size` | A [`tf.int64`](../../../tf#int64) scalar [`tf.Tensor`](../../../tf/Tensor), representing the number of consecutive elements of this dataset to combine in a single batch. |\n| `row_shape` | A [`tf.TensorShape`](../../../tf/TensorShape) or [`tf.int64`](../../../tf#int64) vector tensor-like object representing the equivalent dense shape of a row in the resulting [`tf.SparseTensor`](../../../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. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Dataset` transformation function, which can be passed to [`tf.data.Dataset.apply`](../../../tf/data/Dataset#apply). ||\n\n\u003cbr /\u003e"]]