Skip to content
Related Articles

Related Articles

Python – tensorflow.math.cumprod()

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Jul, 2021

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.  cumprod() is used to calculate the cumulative product of input tensor.

Syntax: tensorflow.math.cumprod(   x, axis, exclusive, reverse, name)

Parameters: 

  • x: It’s the input tensor. Allowed dtype for this tensor are  float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
  • axis(optional): It’s a tensor of type int32. It’s value should  be in the range  A Tensor of type int32 (default: 0). Must be in the range [-rank(x), rank(x)).  Default value is 0.
  • exclusive(optional): It’s of type bool. Default value is False and if set to true then the output for input [a, b, c] will be [1, a, a*b].
  • reverse(optional): It’s of type bool. Default value is False and if set to true then the output for input [a, b, c] will be [a*b*c, a*b, a].
  • name(optional): It’s defines the name for the operation.

Returns: It returns a tensor of same dtype as x.

Example 1:

Python3




# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([1, 2, 4, 5], dtype = tf.int32) 
 
# Printing the input
print("Input: ",a)
 
# Cumulative product
res  = tf.math.cumprod(a)
 
# Printing the result
print("Output: ",res)


Output:

Input:  tf.Tensor([1 2 4 5], shape=(4,), dtype=int32)
Output:  tf.Tensor([ 1  2  8 40], shape=(4,), dtype=int32)

Example 2: In this example both reverse and exclusive are set to True.

Python3




# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([2, 3, 4, 5], dtype = tf.int32) 
 
# Printing the input
print("Input: ",a)
 
# Cumulative product
res  = tf.math.cumprod(a, reverse = True, exclusive = True)
 
# Printing the result
print("Output: ",res)


Output: 

Input:  tf.Tensor([2 3 4 5], shape=(4,), dtype=int32)
Output:  tf.Tensor([60 20  5  1], shape=(4,), dtype=int32)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!