📜  Python – tensorflow.math.cumsum()

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

Python – tensorflow.math.cumsum()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 cumsum()用于计算输入张量的累积和。

示例 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)


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)


输出:

Input:  tf.Tensor([1 2 4 5], shape=(4,), dtype=int32)
Output:  tf.Tensor([ 1  3  7 12], shape=(4,), dtype=int32)

示例 2:在此示例中,反向和排他都设置为 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)

输出:

Input:  tf.Tensor([2 3 4 5], shape=(4,), dtype=int32)
Output:  tf.Tensor([12  9  5  0], shape=(4,), dtype=int32)