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.
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
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)
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.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.