Skip to content

Instantly share code, notes, and snippets.

@kevashcraft
Created February 7, 2020 18:42
Show Gist options
  • Save kevashcraft/02b17a8d69238ac2fcfcc619e4cfdb1a to your computer and use it in GitHub Desktop.
Save kevashcraft/02b17a8d69238ac2fcfcc619e4cfdb1a to your computer and use it in GitHub Desktop.
Iterating Through TF Dataset
# if dataset is not batched
# this will take 1 example
with (ambient, target), label in dataset.take(1):
print("ambient shape", ambient.shape)
print("target shape", target.shape)
print("label shape", label.shape)
ambient_array = ambient.numpy()
target_array = target.numpy()
label_array = label.numpy()
# if dataset is batched
# this will take 1 batch
with features, labels in dataset.take(1):
# features is a tuple of tensors
ambients = features[0]
targets = features[1]
# get a single example (the first in the batch)
ambient = ambients[0] # tensors can be sliced
target = targets[0] # still a tensor until .numpy() is called
print("ambient shape", ambient.shape)
print("target shape", target.shape)
print("label shape", label.shape)
ambient_array = ambient.numpy()
target_array = target.numpy()
label_array = label.numpy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment