In this tutorial, we will use an example to show you how to use tf.reduce_prod() correctly. This function is very similar to tf.reduce_mean() or tf.reduce_sum().
Syntax
tf.reduce_prod() is defined as:
tf.reduce_prod( input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None )
It can compute the product of elements across dimensions of a tensor.
Parameters:
input_tensor: the tensor we will pass
axis: the dimensions to compute. If None (the default), compute all dimensions
keepdims: If true, retains computed dimensions with length 1
How to use tf.reduce_prod()?
Here is an example:
import tensorflow as tf import numpy as np data = tf.convert_to_tensor(np.array([[2,3,4],[3,2,2]]), dtype = tf.float32) re = tf.reduce_prod(data, axis= - 1) init = tf.global_variables_initializer() init_local = tf.local_variables_initializer() with tf.Session() as sess: sess.run([init, init_local]) np.set_printoptions(precision=4, suppress=True) a =sess.run(re) print(a)
Here data is 2*3, we will compute the product of elements on axis = -1.
Run this code, we will see:
[24. 12.]
If we ignore axis, we will get:
re = tf.reduce_prod(data)
The final result is: 288.0