tensorflow dataset code example
Example 1: what is tf.data.dataset.reduce()
import tensorflow as tf
data = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5])
print(data.reduce(0, lambda x, y: x + y).numpy())
>> 15
print(data.reduce(1, lambda x, y: x + y).numpy())
>>16
import tensorflow as tf
data = tf.data.Dataset.from_tensor_slices([[5, 10], [3, 6]])
print(data.reduce(0, lambda x, y: x * y).numpy())
>>[0 0]
print(data.reduce(1, lambda x, y: x * y).numpy())
>>[15 60]
print(data.reduce(2, lambda x, y: x * y).numpy())
>>[30 120]
Example 2: tf.data.Dataset select files with labels filter
dataset = train_data.filter(lambda x,y: tf.reduce_all(tf.not_equal(y, [0,1,2]))).batch(200)