What is broadcasting in torch tensor?

This recipe explains what is broadcasting in torch tensor

Recipe Objective

What is broadcasting in torch tensor?

In this if the PyTorch operations are supporting the broadcast then its tensor arguments can be automatically expanded to be of equal sizes. There are some rules that to be followed if two tensors are broadcastable:
-- Each tensor at least has one dimension.
-- Starting at the trailing dimension, when iterating over the dimension sizes then dimension sizes must be either equal, one of them is 1, or one of them does not exist.

Step 1 - Import library

import torch

Step 2 - Same shape are always broadcastable

a = torch.tensor([3,3]) b = torch.tensor([2,2])
print("These are the same shapes tensor:","\n",a, a.shape, "\n", b,b.shape)

These are the same shapes tensor: 
 tensor([3, 3]) torch.Size([2]) 
 tensor([2, 2]) torch.Size([2])

Step 3 - Not broadcastable

a2 = torch.empty((0,))
b2 = torch.empty(2,2)
print("The a and b are not broadcastable because a2 is not having atleast 1 dimension:","\n", a2,"\n", b2)

The a and b are not broadcastable because a2 is not having atleast 1 dimension: 
 tensor([]) 
 tensor([[1.5481e-35, 0.0000e+00],
        [1.3873e-43, 0.0000e+00]])

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.