Python – tensorflow.math.cumprod()
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 cumprod()用于计算输入张量的累积积。
Syntax: tensorflow.math.cumprod( x, axis, exclusive, reverse, name)
Parameters:
- x: It’s the input tensor. Allowed dtype for this tensor are float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
- 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 and if set to true then the output for input [a, b, c] will be [1, a, a*b].
- reverse(optional): It’s of type bool. Default value is False and if set to true then the output for input [a, b, c] will be [a*b*c, a*b, a].
- 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.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)