Python – Tensorflow math.accumulate_n() method
Last Updated :
02 Jun, 2022
Tensorflow math.accumulate_n() method performs the element-wise sum of a list of passed tensors. The result is a tensor after performing the operation. The operation is done on the representation of a and b. This method belongs to math module.
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.
Let’s see this concept with the help of few examples: Example 1:
Python3
import tensorflow as tf
a = tf.constant([[ 1 , 3 ], [ 6 , 7 ]])
b = tf.constant([[ 5 , 2 ], [ 3 , 8 ]])
c = tf.math.accumulate_n([a, b, b])
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))
|
Output:
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]]
Example 2:
Python3
import tensorflow as tf
a = tf.constant([[ 2 , 4 ], [ 1 , 3 ]])
b = tf.constant([[ 5 , 3 ], [ 4 , 6 ]])
c = tf.math.accumulate_n([b, a, b], shape = [ 2 , 2 ], tensor_dtype = tf.int32)
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))
|
Output:
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]]
Share your thoughts in the comments
Please Login to comment...