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.

import tensorflow as tf

# Creating two tensors
tensor_a = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
tensor_b = tf.constant([[5, 6], [7, 8]], dtype=tf.int32)

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:

# Addition
tensor_sum = tf.add(tensor_a, tensor_b)
print(f"Tensor Addition:\n{tensor_sum}\n")

The output of the above code will be:

Tensor Addition:
[[ 6  8]
 [10 12]]

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:

# Element-wise Multiplication
tensor_product = tf.multiply(tensor_a, tensor_b)
print(f"Element-wise Multiplication:\n{tensor_product}\n")

The output of the above code will be:

Element-wise Multiplication:
[[ 5 12]
 [21 32]]

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.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal