Open In App

Python – tensorflow.math.cumulative_logsumexp()

Improve
Improve
Like Article
Like
Save
Share
Report

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 

cumulative_logsumexp() is used to calculate the cumulative log-sum-exp of input tensor. This operation is equivalent to tensorflow.math.log( tensorflow.math.cumsum( tensorflow.math.exp(x))) but it is numerically more stable.

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

Parameters: 

  • x: It’s the input tensor. Allowed dtypes for this tensor are  float16, float32, float64.
  • 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.
  • reverse(optional): It’s of type bool. Default value is False.
  • 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.float64) 
 
# Printing the input
print("Input: ",a)
 
# Cumulative log-sum-exp
res  = tf.math.cumulative_logsumexp(a)
 
# Printing the result
print("Output: ",res)


Output:

Input:  tf.Tensor([1. 2. 4. 5.], shape=(4,), dtype=float64)
Output:  tf.Tensor([1.         2.31326169 4.16984602 5.36184904], shape=(4,), dtype=float64)

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.float64) 
 
# Printing the input
print("Input: ",a)
 
# Cumulative log-sum-exp
res  = tf.math.cumulative_logsumexp(a, reverse = True, exclusive = True)
 
# Printing the result
print("Output: ",res)


Output: 

Input:  tf.Tensor([2. 3. 4. 5.], shape=(4,), dtype=float64)
Output:  tf.Tensor([ 5.40760596e+000  5.31326169e+000  5.00000000e+000 -1.79769313e+308], shape=(4,), dtype=float64)


Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads