Tensorflow 2 – Arithmetic Operations on Tensors

Last updated on August 24, 2021 Guest Contributor Loading... Post a comment

This guide shows you how to perform the four arithmetic operations on tensors in Tensorflow 2: addition, subtraction, multiplication, and division.

Addition

You can add values to a tensor using the Python math addition operator or Tensorflow’s built-in tf.add function.

Example:

import tensorflow as tf

x1 = tf.constant([
                        [1, 2],
                        [2, 1]
])

x2 = tf.constant([
                        [0, 1],
                        [1, 1]
])

print(x1 + x2)
print(x1 + 5)

print(tf.add(x1, x2))
print(tf.add(x1, 10))

Output:

tf.Tensor(
[[1 3]
 [3 2]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[6 7]
 [7 6]], shape=(2, 2), dtype=int32)

tf.Tensor(
[[1 3]
 [3 2]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[11 12]
 [12 11]], shape=(2, 2), dtype=int32)

Note that the built-in function helps your code run faster on a GPU (or TPU), especially with large tensors.

Subtraction

With subtraction, we have the Python math subtraction operator and the tf.subtract function.

Example:

import tensorflow as tf

x1 = tf.constant([
                        [1, 2],
                        [3, 4]
])

x2 = tf.constant([
                        [0, 1],
                        [1, 0]
])

print(x1 - x2)
print(x1 - 15)

print(tf.subtract(x1, x2))
print(tf.subtract(x1, 10))

Output:

tf.Tensor(
[[1 1]
 [2 4]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[-14 -13]
 [-12 -11]], shape=(2, 2), dtype=int32)

tf.Tensor(
[[1 1]
 [2 4]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[-9 -8]
 [-7 -6]], shape=(2, 2), dtype=int32)

Multiplication

You can use the Python multiplication operator or the tf.multiply function.

Example:

import tensorflow as tf

x1 = tf.constant([
                        [0.5, 2.1],
                        [3.2, 4.5]
])

x2 = tf.constant([
                        [0., 1.],
                        [1., 0.]
])

print(x1 * x2)
print(x1 * 2)

print(tf.multiply(x1, x2))
print(tf.multiply(x1, 1.5))

Output:

tf.Tensor(
[[0.  2.1]
 [3.2 0. ]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[1.  4.2]
 [6.4 9. ]], shape=(2, 2), dtype=float32)

tf.Tensor(
[[0.  2.1]
 [3.2 0. ]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[0.75      3.1499999]
 [4.8       6.75     ]], shape=(2, 2), dtype=float32)

Division

You can use the math division operator or the tf.divide function.

Example:

import tensorflow as tf

x1 = tf.constant([
                        [1, 2, 3],
                  [4, 5, 6]
])

x2 = tf.constant([
                        [1, 2, 3],
                  [3, 2, 1]
])

print(x1 / x2)
print(x1 / 2)

print(tf.divide(x1, x2))
print(tf.divide(x1, 0))

Output:

tf.Tensor(
[[1.         1.         1.        ]
 [1.33333333 2.5        6.        ]], shape=(2, 3), dtype=float64)
tf.Tensor(
[[0.5 1.  1.5]
 [2.  2.5 3. ]], shape=(2, 3), dtype=float64)

tf.Tensor(
[[1.         1.         1.        ]
 [1.33333333 2.5        6.        ]], shape=(2, 3), dtype=float64)
tf.Tensor(
[[inf inf inf]
 [inf inf inf]], shape=(2, 3), dtype=float64)

Conclusion

We’ve gone through the four basic math operations in Tensorflow 2. If you would like to explore more about machine learning and Python stuff, take a look at the following articles:

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

References

To find more information about the mentioned functions, you can see the documentation:

Happy coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles