In this post, you will learn about the concepts of Tensor Broadcasting with the help of Python Numpy examples. Recall that Tensor is defined as the container of data (primarily numerical) most fundamental data structure used in Keras and Tensorflow. You may want to check out a related article on Tensor – Tensor explained with Python Numpy examples.
Broadcasting of tensor is borrowed from Numpy broadcasting. Broadcasting is a technique used for performing arithmetic operations between Numpy arrays / Tensors having different shapes. In this technique, the following is done:
Take a look at the following example where there are two Numpy arrays, X and Y having shapes as (2, 2) and (1, ) respectively. Let’s perform the operation to add X and Y and understand the broadcasting concept in detailed manner. Here is the code representing X & Y created using np.random.random
X = np.random.random((2, 2))
Y = np.random.random((1,))
#
# Print arrays shape
#
print('Shape of X: ', X.shape, '\nShape of Y: ', Y.shape, '\n')
#
# Print the array values
#
print('Array X: ', X, '\n\nArray Y: ', Y)
Here is the output of X & Y including their shapes. You may note that the shape of X is (2, 2) – 2D Tensor and Y is (1, ) – 1D Tensor. We will try and perform arithmetic operations on 2D Tensor and 1D Tensor. They have different ranks and shapes. This is where Tensor Broadcasting would come into picture where the Tensors / Numpy arrays will be transformed into compatible shapes to perform the arithmetic operations.
In order to bring the Tensors / Numpy Arrays into compatible shape, this is what is done (as part of the broadcasting process):
Based on above, Y will be internally transformed to the following before arithmetic operation is performed:
Y1 = np.array([[0.94992891, 0.94992891], [0.94992891, 0.94992891]])
Lets validate this by performing arithmetic operation X + Y and X + Y1. You may find that both X + Y and X + Y1 ends up giving same result.
Here is the summary of what you learned about the Tensor broadcasting:
Last updated: 15th May, 2024 Have you ever wondered how your bank decides what to…
In this fast-changing world, the ability to learn effectively is more valuable than ever. Whether…
Last updated: 13th May, 2024 Whether you are a researcher, data analyst, or data scientist,…
Last updated: 12th May, 2024 Data lakehouses are a relatively new concept in the data…
Last updated: 12th May 2024 In this blog, we get an overview of the machine…
Last updated: 12th May, 2024 In the world of generative AI models, autoencoders (AE) and…