Mastering Basic Tensor Operations in TensorFlow
Topic Overview
Welcome to the next step in our Introduction to TensorFlow Basics course! In this lesson, we're going to delve deeper into Tensor operations using TensorFlow. We'll learn about basic operations such as tensor addition, multiplication, and broadcasting operations. Let's get started!
Revision of TensorFlow Constant Tensors
Before we start with tensor operations, let's briefly review TensorFlow constant tensors. In the previous lesson, we had covered the creation of constant tensors using the tf.constant() function. In TensorFlow, these constant tensors allow us to store data in arrays of varying dimensions (1D, 2D, 3D, etc.), which are immutable.
For the purpose of this lesson, let's create two constant tensors using TensorFlow. Remember, we can specify the datatype of the tensors using the dtype keyword.
We've created two 2x2 tensors with integer elements: tensor_a and tensor_b. Now, let's do some operations on these tensors.
Tensor Addition
Tensor addition, akin to conventional matrix addition, is an element-wise operation — meaning that the addends must have the same shape. TensorFlow's tf.add() function allows us to perform this operation easily.
Here's how we apply tf.add() to our tensors:
The output of the above code will be:
This output demonstrates how tf.add() performs an element-wise addition, giving us a new tensor where each element is the sum of the corresponding elements in tensor_a and tensor_b.
Element-Wise Tensor Multiplication
Element-wise multiplication operates on corresponding elements of the matrices (or tensors). This means the shapes of the two tensors must match exactly.
The tf.multiply() function in TensorFlow helps us perform this operation, as shown in the following code snippet:
The output of the above code will be:
Here, tensor_product shows the product of corresponding elements from tensor_a and tensor_b. This example straightforwardly illustrates how element-wise multiplication works in TensorFlow.
