访问量: 10 次浏览
tensorflow.math.cumprod()TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 cumprod() 用于计算输入张量的累积乘积。
语法:
tensorflow.math.cumprod( x, axis, exclusive, reverse, name)
参数:
dtype 是float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half。int32 类型的张量。它的值应该在一个 int32 类型的张量的范围内(默认:0)。必须在[-rank(x), rank(x)]范围内。默认值是0。返回:它返回一个与x具有相同 dtype 的张量。
示例 1:
# 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)
输出:
Input: tf.Tensor([1 2 4 5], shape=(4,), dtype=int32)
Output: tf.Tensor([ 1 2 8 40], shape=(4,), dtype=int32)
例子2:
在这个例子中,反向和排他都被设置为 “真”。
# 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)