tf.data.experimental.pad_to_cardinality
Stay organized with collections
Save and categorize content based on your preferences.
Pads a dataset with fake elements to reach the desired cardinality.
tf.data.experimental.pad_to_cardinality(
cardinality, mask_key='valid'
)
The dataset to pad must have a known and finite cardinality and contain
dictionary elements. The mask_key
will be added to differentiate between
real and padding elements -- real elements will have a <mask_key>=True
entry
while padding elements will have a <mask_key>=False
entry.
Example usage:
ds = tf.data.Dataset.from_tensor_slices({'a': [1, 2]})
ds = ds.apply(tf.data.experimental.pad_to_cardinality(3))
list(ds.as_numpy_iterator())
[{'a': 1, 'valid': True}, {'a': 2, 'valid': True}, {'a': 0, 'valid': False}]
This can be useful, e.g. during eval, when partial batches are undesirable but
it is also important not to drop any data.
ds = ...
# Round up to the next full batch.
target_cardinality = -(-ds.cardinality() // batch_size) * batch_size
ds = ds.apply(tf.data.experimental.pad_to_cardinality(target_cardinality))
# Set `drop_remainder` so that batch shape will be known statically. No data
# will actually be dropped since the batch size divides the cardinality.
ds = ds.batch(batch_size, drop_remainder=True)
Args |
cardinality
|
The cardinality to pad the dataset to.
|
mask_key
|
The key to use for identifying real vs padding elements.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.data.experimental.pad_to_cardinality\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/experimental/ops/pad_to_cardinality.py#L26-L105) |\n\nPads a dataset with fake elements to reach the desired cardinality.\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.pad_to_cardinality`](https://www.tensorflow.org/api_docs/python/tf/data/experimental/pad_to_cardinality)\n\n\u003cbr /\u003e\n\n tf.data.experimental.pad_to_cardinality(\n cardinality, mask_key='valid'\n )\n\nThe dataset to pad must have a known and finite cardinality and contain\ndictionary elements. The `mask_key` will be added to differentiate between\nreal and padding elements -- real elements will have a `\u003cmask_key\u003e=True` entry\nwhile padding elements will have a `\u003cmask_key\u003e=False` entry.\n\n#### Example usage:\n\n ds = tf.data.Dataset.from_tensor_slices({'a': [1, 2]})\n ds = ds.apply(tf.data.experimental.pad_to_cardinality(3))\n list(ds.as_numpy_iterator())\n [{'a': 1, 'valid': True}, {'a': 2, 'valid': True}, {'a': 0, 'valid': False}]\n\nThis can be useful, e.g. during eval, when partial batches are undesirable but\nit is also important not to drop any data. \n\n ds = ...\n # Round up to the next full batch.\n target_cardinality = -(-ds.cardinality() // batch_size) * batch_size\n ds = ds.apply(tf.data.experimental.pad_to_cardinality(target_cardinality))\n # Set `drop_remainder` so that batch shape will be known statically. No data\n # will actually be dropped since the batch size divides the cardinality.\n ds = ds.batch(batch_size, drop_remainder=True)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|----------------------------------------------------------|\n| `cardinality` | The cardinality to pad the dataset to. |\n| `mask_key` | The key to use for identifying real vs padding elements. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A dataset transformation that can be applied via [`Dataset.apply()`](../../../tf/data/Dataset#apply). ||\n\n\u003cbr /\u003e"]]