Thursday, November 20, 2025
HomeLanguagesPython – tensorflow.math.cumsum()

Python – tensorflow.math.cumsum()

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

Syntax: tensorflow.math.cumsum(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 [0, 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 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 sum
res  = tf.math.cumsum(a)
 
# Printing the result
print("Output: ",res)


Output:

Input:  tf.Tensor([1 2 4 5], shape=(4,), dtype=int32)
Output:  tf.Tensor([ 1  3  7 12], 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 sum
res  = tf.math.cumsum(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([12  9  5  0], shape=(4,), dtype=int32)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6778 POSTS0 COMMENTS
Nicole Veronica
11927 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7162 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS