Flux tensor | tf.data.Dataset.reduce ()

With the tf.data.Dataset.reduce() method, we can get a shorthand transformation of all elements in the dataset using tf.data.Dataset. reduce () .

Syntax: tf.data.Dataset.reduce ()
Return: Return combined single result after transformation.

Notes:
These examples will demonstrate the use of the new version of tensorflow 2.0, therefore, if you want to run these examples, run the following commands on the command line.

 pip install tensorflow == 2.0.0-rc2 

Example # 1:
In this example, we can see that using tf.data.Dataset.reduce () we can get a reduced transformation of all elements from the dataset.

# import tensor by current

import tensorflow as tf

 
# using the tf.data.Dataset.reduce () method

gfg = tf.data.Dataset.from_tensor_slices ([ 1 , 2 , 3 , 4 , 5 ])

 

print (gfg. reduce ( 0 , lambda x, y: x + y) .numpy ())

Output:

15

Example # 2:

# import flow tensor

import tensorflow as tf

 
# using the tf.data.Dataset.reduce () method

gfg = tf.data.Dataset.from_tensor_slices ( [[ 5 , 10 ], [ 3 , 6 ]])

 

pri nt (gfg. reduce ( 0 , lambda x, y: x * y) .numpy ())

Output:

[15, 60]