Python – tensorflow.math.cumulative_logsumexp()
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。
cumulative_logsumexp()用于计算输入张量的累积 log-sum-exp。此操作等效于tensorflow.math.log(tensorflow.math.cumsum(tensorflow.math.exp(x))) ,但它在数值上更稳定。
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’s defines the name for the operation.
Returns:
It returns a tensor of same dtype as x.
示例 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)
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)
输出:
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)
示例 2:在此示例中,反向和排他都设置为 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)
输出:
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)