📜  Python – TensorFlow math.accumulate_n() 方法

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

Python – TensorFlow math.accumulate_n() 方法

Tensorflow math.accumulate_n()方法执行传递的张量列表的元素总和。结果是执行操作后的张量。该操作是在 a 和 b 的表示上完成的。该方法属于数学模块。

让我们通过几个例子来看看这个概念:
示例 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]]