The previous answer is good, yet I would like to point out two matters:
- The code below works, no need to use the experimental package anymore.
import tensorflow as tf
dataset = tf.data.Dataset.range(42)
#Still prints 42
print(dataset.cardinality().numpy())
- If you use the
filter
predicate, the cardinality may return value -2, hence unknown; if you do use filter predicates on your dataset, ensure that you have calculated in another manner the length of your dataset( for example length of pandas dataframe before applying .from_tensor_slices()
on it.
Another important point is how to set the parameters steps_per_epoch
and validation_steps
: steps_per_epoch == length_of_training_dataset // batch_size, validation_steps == length_of_validation_dataset // batch_size
A full example is available here : How to use repeat() function when building data in Keras?