Python - Tensorflow math.add_n() method
Last Updated :
04 Jun, 2020
Tensorflow
math.add_n()
method adds the all passed tensors element-wise. The operation is done on the representation of a and b.
This method belongs to math module.
Syntax: tf.math.add_n(inputs, name=None)
Arguments
- inputs: It specifies a list of tf.Tensor or tf.IndexedSlices objects, and the shape and type of each must be same. tf.IndexedSlices objects converted automatically into dense tensors before applying method.
- 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 passed inputs.
Note: This method performs the same operation as tf.math.accumulate_n, but this method waits for the inputs to ready before starting to sum. So, this buffering results in more memory consumption when inputs might not ready at same time.
Let's see this concept with the help of few examples:
Example 1:
Python3
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[1, 3], [2, 8]])
b = tf.constant([[2, 1], [6, 7]])
# Applying the math.add_n() function
# storing the result in 'c'
c = tf.math.add_n([a, 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)
Output:
Input 1 Tensor("Const_99:0", shape=(2, 2), dtype=int32)
[[1 3]
[2 8]]
Input 2 Tensor("Const_100:0", shape=(2, 2), dtype=int32)
[[2 1]
[6 7]]
Output: Tensor("AddN:0", shape=(2, 2), dtype=int32)
[[ 3 4]
[ 8 15]]
Example 2:
Python3
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[1, 1], [2, 6]])
b = tf.constant([[5, 1], [8, 7]])
# Applying the math.add_n() function
# storing the result in 'c'
c = tf.math.add_n([a, b], name = "Add_N")
# 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))
Output:
Input 1 Tensor("Const_101:0", shape=(2, 2), dtype=int32)
[[1 1]
[2 6]]
Input 2 Tensor("Const_102:0", shape=(2, 2), dtype=int32)
[[5 1]
[8 7]]
Output: Tensor("Add_N:0", shape=(2, 2), dtype=int32)
[[ 6 2]
[10 13]]
Similar Reads
Python - Tensorflow math.add() method Tensorflow math.add() method returns the a + b of the passes inputs. The operation is done on the representation of a and b. This method belongs to math module. Syntax: tf.math.add(a, b, name=None) Arguments a: This parameter should be a Tensor and also from the one of the following types: bfloat16,
2 min read
Python - Tensorflow math.accumulate_n() method 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
2 min read
Python - tensorflow.math.divide_no_nan() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. divide_no_nan() is used to compute element wise safe division of x by y i.e it returns 0 if y is zero Syntax: tensorflow.math.divide_no_nan( x, y, name) Parameters: x:
2 min read
Python - tensorflow.math.divide() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. divide() is used to compute element wise style division of x by y. Syntax: tensorflow.math.divide( x, y, name) Parameters: x: It is a tensor.y: It is a tensor.name(opti
2 min read
Python - tensorflow.math.minimum() TensorFlow is open-source python library designed by Google to develop Machine Learning models and deep learning  neural networks. minimum() is used to find element wise maximum of x and y. Specifically, it returns x < y ? x : y. Syntax: tf.math.minimum(x, y, name) Parameter: x: It's the input te
2 min read