Retrieving The Next Element From Tf.data.dataset In Tensorflow 2.0 Beta
Before tensorflow 2.0-beta, to retrieve the first element from tf.data.Dataset, we may use a iterator as shown below: #!/usr/bin/python import tensorflow as tf train_dataset = tf
Solution 1:
What works with eager execution enabled (default in TF 2.0) is:
elem = next(iter(train_dataset))
Explanation: Datasets have an __iter__
member function to support the for elem in dataset:
approach. This returns an iterator. The Python function iter
does just that: Basically calls the __iter__
function. next
then returns the first element that iterator produces.
I haven't found a solution which works for non-eager execution though, as that currently raises RuntimeError: __iter__() is only supported inside of tf.function or when eager execution is enabled.
Solution 2:
You can .take(1)
from the dataset:
for elem in train_dataset.take(1):
print (elem.numpy())
Post a Comment for "Retrieving The Next Element From Tf.data.dataset In Tensorflow 2.0 Beta"