Python – TensorFlow math.accumulate_n() 方法
Tensorflow math.accumulate_n()
方法执行传递的张量列表的元素总和。结果是执行操作后的张量。该操作是在 a 和 b 的表示上完成的。该方法属于数学模块。
Syntax: tf.math.accumulate_n( inputs, shape=None, tensor_dtype=None, name=None)
Arguments
- inputs: This parameter takes a list of Tensor objects, and each of them with same shape and type.
- shape: This is optional parameter and it defines the expected shape of elements of inputs.
- dtype: This is optional parameter and it defines the expected data type of inputs.
- name: This is optional parameter and this is the name of the operation.
Return: It returns a Tensor having the same shape and type as the elements of inputs.
让我们通过几个例子来看看这个概念:
示例 1:
示例 1:
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[1, 3], [6, 7]])
b = tf.constant([[5, 2], [3, 8]])
# Applying the accumulate_n() function
# storing the result in 'c'
c = tf.math.accumulate_n([a, b, b])
# Initiating a Tensorflow session
with tf.Session() as sess:
print("Input 1", a)
print(sess.run(a))
print("Input 2", b)
print(sess.run(b))
print("Output: ", c)
print(sess.run(c))
输出:
Input 1 Tensor("Const_67:0", shape=(2, 2), dtype=int32)
[[1 3]
[6 7]]
Input 2 Tensor("Const_68:0", shape=(2, 2), dtype=int32)
[[5 2]
[3 8]]
Output: Tensor("AccumulateNV2_2:0", shape=(2, 2), dtype=int32)
[[11 7]
[12 23]]
示例 2:
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[2, 4], [1, 3]])
b = tf.constant([[5, 3], [4, 6]])
# Applying the accumulate_n() function
# storing the result in 'c'
c = tf.math.accumulate_n([b, a, b], shape =[2, 2], tensor_dtype = tf.int32)
# Initiating a Tensorflow session
with tf.Session() as sess:
print("Input 1", a)
print(sess.run(a))
print("Input 2", b)
print(sess.run(b))
print("Output: ", c)
print(sess.run(c))
输出:
Input 1 Tensor("Const_73:0", shape=(2, 2), dtype=int32)
[[2 4]
[1 3]]
Input 2 Tensor("Const_74:0", shape=(2, 2), dtype=int32)
[[5 3]
[4 6]]
Output: Tensor("AccumulateNV2_5:0", shape=(2, 2), dtype=int32)
[[12 10]
[ 9 15]]
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。