Tensorflow Add : Know to Add Multiple Tensors With Examples

Know to Add Multiple Tensors With Examples

Tensorflow has many inbuilt functions that allow manipulating tensors. The TensorFlow add is one of them. It allows you to add two tensors. In this entire tutorial, you will learn how to add multiple tensors in Tensorflow with examples.

Examples on tensorflow.add() method

In this section, you will know all the examples of adding multiple tensors in  TensorFlow. Please note that I am doing all the coding work on the Jupiter notebook. So make sure that you should do the same for better understanding.

Example 1 . Adding tensor with a scalar value

The first example is adding a list of numeric values with a Scalar value. Here I will use the tf.add() method. Inside the method, I will pass the tensors that I want to do an addition operation.

Execute the below lines of code.

import tensorflow as tf
x = [1, 2, 3, 4, 5]
y = 10
tf.add(x,y)

You will get the following output.

Output

Adding tensor with a scalar value
Adding tensor with a scalar value

Example 2: Add tensors using binary addition operator

You can also add multiple tensors using the binary + operator. But firstly, you have to convert each input list to tensors. Then after you can add the tensors using the binary operator. You can convert list to tensors using tf.convert_to_tensor() method.

Execute the below lines of code and see the output.

import tensorflow as tf
x = tf.convert_to_tensor([1, 2, 3, 4, 5])
y = tf.convert_to_tensor(10)
x+y

Output

Add tensors using binary addition operator
Add tensors using binary addition operator

Example 3: Addition of tensors of the same dimension

The above example was the addition of the same dimension tensors. Both the input tensor will be of list type and of the same dimension. Copy, paste, and run the code given below.

import tensorflow as tf
x = [10, 20, 30, 40, 50]
y = [10, 20, 30, 40, 50]
tf.add(x,y)

Output

Addition of tensors of same dimension
Addition of tensors of the same dimension

These are examples of how to add multiple tensors in TensorFlow. In this tutorial, I have used two tensors for demonstration. But you can use more than two. The second example will be easy for more than two tensors. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

TensorFlow documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner