📜  Python – tensorflow.math.cumulative_logsumexp()

📅  最后修改于: 2022-05-13 01:54:44.234000             🧑  作者: Mango

Python – tensorflow.math.cumulative_logsumexp()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

cumulative_logsumexp()用于计算输入张量的累积 log-sum-exp。此操作等效于tensorflow.math.log(tensorflow.math.cumsum(tensorflow.math.exp(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)