📜  Python – tensorflow.math.cumprod()

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

Python – tensorflow.math.cumprod()

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

示例 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 product
res  = tf.math.cumprod(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 product
res  = tf.math.cumprod(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  2  8 40], 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 product
res  = tf.math.cumprod(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([60 20  5  1], shape=(4,), dtype=int32)