Python – tensorflow.math.cumsum()
Last Updated :
22 Mar, 2023
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
import tensorflow as tf
a = tf.constant([ 1 , 2 , 4 , 5 ], dtype = tf.int32)
print ( "Input: " ,a)
res = tf.math.cumsum(a)
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
import tensorflow as tf
a = tf.constant([ 2 , 3 , 4 , 5 ], dtype = tf.int32)
print ( "Input: " ,a)
res = tf.math.cumsum(a, reverse = True , exclusive = True )
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)
Share your thoughts in the comments
Please Login to comment...