Last active
September 16, 2018 04:39
-
-
Save raytroop/05f4f697c014ee991597c12c6a25aa2a to your computer and use it in GitHub Desktop.
map and flat_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
# >1 | |
input = [10, 20, 30] | |
ds = tf.data.Dataset.from_tensor_slices(input) | |
ds = ds.flat_map(lambda x: tf.data.Dataset.from_tensor_slices([x, x+1, x+2])) | |
element = ds.make_one_shot_iterator().get_next() | |
with tf.Session() as sess: | |
for _ in range(9): | |
print(sess.run(element)) | |
# 10 | |
# 11 | |
# 12 | |
# 20 | |
# 21 | |
# 22 | |
# 30 | |
# 31 | |
# 32 | |
# >2 | |
input = [10, 20, 30] | |
ds = tf.data.Dataset.from_tensor_slices(input) | |
ds = ds.flat_map(lambda x: tf.data.Dataset.from_tensors([x, x+1, x+2])) | |
element = ds.make_one_shot_iterator().get_next() | |
with tf.Session() as sess: | |
for _ in range(9): | |
print(sess.run(element)) | |
# [10 11 12] | |
# [20 21 22] | |
# [30 31 32] | |
# OutOfRangeError | |
# >3 | |
input = [10, 20, 30] | |
ds = tf.data.Dataset.from_tensor_slices(input) | |
ds = ds.map(lambda x: [x, x+1, x+2]) | |
element = ds.make_one_shot_iterator().get_next() | |
with tf.Session() as sess: | |
for _ in range(9): | |
print(sess.run(element)) | |
# (10, 11, 12) | |
# (20, 21, 22) | |
# (30, 31, 32) | |
# OutOfRangeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/50533300/8037585
It seems that
tf.data.Dataset.from_tensors
usually work withflat_map