EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PyTorch bmm

Home » Data Science » Data Science Tutorials » Machine Learning Tutorial » PyTorch bmm

PyTorch bmm

Introduction to PyTorch bmm

PyTorch bmm is used for matrix multiplication in batches where the scenario involves that the matrices to be multiplied have the size of 3 dimensions that is x, y, and z and the dimension of the first dimension for matrices to be multiplied should be the same. Broadcasting is not supported by using this way of matrix multiplication. In this article, we will try to gain more knowledge about PyTorch bmm and will study what is PyTorch bmm, PyTorch bmm function, PyTorch bmm code example, the difference between PyTorch mm and bmm, and conclusion about the same.

What is PyTorch bmm?

PyTorch bmm is used for matrix multiplication in cases where the dimensions of both matrices are 3 dimensional and the value of dimension for the last dimension for both matrices is the same. The syntax of the bmm function that can be used in PyTorch is as shown below –
Torch.bmm(input tensor 1, input tensor 2, deterministic = false, out = None)

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The parameter named deterministic has a Boolean value specified in it which specified whether to go for non-deterministic that is a value set to false which usually is faster in calculations or go for the slow calculations where the value of the parameter is set to true and process of matrix multiplication is carried out in a deterministic way.

It is important the input tensor 1 and input tensor 2 parameters that are supplied should have a tensor of 3 dimensions so that both can have an equal number of matrices.

For example, when the input tensor 1 will be (b * n * m) and the input tensor 2 is (b * m * p) then the resultant matrix obtained by matrix multiplication of both the input tensors will be (b * n * p) tensor.

Outi = input tensor 1 i @input tensor 2 i

Popular Course in this category
Sale
Machine Learning Training (20 Courses, 29+ Projects)19 Online Courses | 29 Hands-on Projects | 178+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.7 (14,249 ratings)
Course Price

View Course

Related Courses
Deep Learning Training (17 Courses, 24+ Projects)Artificial Intelligence Training (5 Courses, 2 Project)

The bmm matrix multiplication does not support broadcast. When you want to have the matrix multiplication that support broadcasting you can go for torch.matmul() function.

PyTorch bmm function

The bmm function can be used by following the below syntax –

Torch.bmm(input tensor 1, input tensor 2, deterministic = false, out = None)

The parameters used in the above syntax are described below in detail –

  • Input tensor 1 – This is tensor value that acts as the first batch that contains the matrices which are to be further multiplied with other ones.
  • Input tensor 2 – This is tensor value that acts as the second batch that contains the matrices which are to be multiplied with previous ones.
  • Deterministic – It is an optional Boolean parameter which when not specified ahs its default value set to false which means that the default behavior is nondeterministic for faster calculations. When you set it to true the calculations of matrix multiplication are made at a deterministic and in slower pace. We can only make use of this argument when dealing with sparse dense CUDA bmm.
  • Out – This is also an optional tensor parameter that helps us to specify the reference where the output tensor after matrix multiplication of tensor 1 and tensor 2 will be created.

PyTorch bmm Code Example

Example 1

Let us try to understand the implementation of bmm matrix multiplication with the help of a simple example where we will create two random valued tensors of 3-dimensional size that are to be multiplied and will print the output tensor after bmm matrix multiplication –

import torch
#We will take two matrices of 3 dimensional size which are to be multiplied
sampleEducbaMatrix1 = torch.randn(2, 3, 3)
sampleEducbaMatrix2 = torch.randn(2, 3, 4)
print("First input matrix - \n",sampleEducbaMatrix1)
print("\nSecond input matrix - \n",sampleEducbaMatrix2)
print("\nResultant output matrix - \n",torch.bmm(sampleEducbaMatrix1,sampleEducbaMatrix2))

The output of the code after executing is as shown below. Note that due to random generation of tensors the values may differ each time you run for input matrices as well as output matrix.

3

Example #2

Now, let us consider one example where we will observe the change in the size of the resulting tensor depending on the input tensor matrices –

sampleInputMatrix1 = torch.randn(10, 3, 4)
sampleInputMatrix2 = torch.randn(10, 4, 5)
sampleOutputtMatrix = torch.bmm(sampleInputMatrix1, sampleInputMatrix2)
sampleOutputtMatrix.size()

After executing the above code snippet, we get the following output –

1

Example 3 –

Let’s take one more example to understand the changes in the size of the tensor batches when the dimensions are specified.

educbaBatchTensor1 = torch.randn(10, 3, 4)
educbaBatchTensor2 = torch.randn(10, 4, 5)
resultingTensor = torch.bmm(educbaBatchTensor1, educbaBatchTensor2)
resultingTensor.size()

After the execution of the above program, we get the following output –

2

We can observe that in both examples 2 and 3 when the input first tensor will be (b * n * m) and the second input tensor is (b * m * p) then the resultant matrix obtained by matrix multiplication of both the input tensors will be (b * n * p) tensor

Difference between PyTorch mm and bmm

Let us try to understand the difference that lies between the two functions of PyTorch namely mm and bmm by using the comparison table –

Torch.mm() Torch.bmm()
Matrix multiplication is carried out between the tensor of m*n and n*p size. Matrix multiplication is carried out between the matrices of size (b * n * m) and (b * m * p) where b is the size of the batch.
It is only used for matrix multiplication where both matrices are 2 dimensional. It is only used for matrix multiplication where both matrices are 3 dimensional.
It is not necessary for the first dimension of both matrices to be of the same value. The first dimension of both the input matrices should be of the same value.
Broadcasting where tensors of different shapes are allowed and smaller ones are broadcasted to the bigger ones for suiting the shape is not supported. Broadcasting is not supported here as well.

Conclusion

PyTorch bmm is used for the matrix multiplication of batches where the tenors or matrices are 3 dimensional in nature. Also, one more condition for matrix multiplication is that the first dimension of both the matrices being multiplied should be the same. The bmm matrix multiplication does not support broadcasting.

Recommended Articles

This is a guide to PyTorch bmm. Here we discuss Introduction, What is PyTorch bmm, function, examples with code implementation. You may also have a look at the following articles to learn more –

  1. PyTorch Detach
  2. pytorch gan
  3. PyTorch Flatten
  4. PyTorch CUDA

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Machine Learning Tutorial
  • PyTorch
    • PyTorch Tensors
    • What is PyTorch?
    • PyTorch MSELoss()
    • PyTorch NLLLOSS
    • PyTorch MaxPool2d
    • PyTorch Pretrained Models
    • PyTorch Squeeze
    • PyTorch Reinforcement Learning
    • PyTorch zero_grad
    • PyTorch norm
    • PyTorch VAE
    • PyTorch Early Stopping
    • PyTorch requires_grad
    • PyTorch MNIST
    • PyTorch Conv2d
    • Dataset Pytorch
    • PyTorch tanh
    • PyTorch bmm
    • PyTorch profiler
    • PyTorch unsqueeze
    • PyTorch adam
    • PyTorch backward
    • PyTorch concatenate
    • PyTorch Embedding
    • PyTorch Tensor to NumPy
    • PyTorch Normalize
    • PyTorch ReLU
    • PyTorch Autograd
    • PyTorch Transpose
    • PyTorch Object Detection
    • PyTorch Autoencoder
    • PyTorch Loss
    • PyTorch repeat
    • PyTorch gather
    • PyTorch sequential
    • PyTorch U-NET
    • PyTorch Sigmoid
    • PyTorch Neural Network
    • PyTorch Quantization
    • PyTorch Ignite
    • PyTorch Versions
    • PyTorch TensorBoard
    • PyTorch Dropout
    • PyTorch Model
    • PyTorch optimizer
    • PyTorch ResNet
    • PyTorch CNN
    • PyTorch Detach
    • Single Layer Perceptron
    • PyTorch vs Keras
    • torch.nn Module
  • Basic
    • Introduction To Machine Learning
    • What is Machine Learning?
    • Uses of Machine Learning
    • Applications of Machine Learning
    • Naive Bayes in Machine Learning
    • Dataset Labelling
    • DataSet Example
    • Dataset ZFS
    • Careers in Machine Learning
    • What is Machine Cycle?
    • Machine Learning Feature
    • Machine Learning Programming Languages
    • What is Kernel in Machine Learning
    • Machine Learning Tools
    • Machine Learning Models
    • Machine Learning Platform
    • Machine Learning Libraries
    • Machine Learning Life Cycle
    • Machine Learning System
    • Machine Learning Datasets
    • Machine Learning Certifications
    • Machine Learning Python vs R
    • Optimization for Machine Learning
    • Types of Machine Learning
    • Machine Learning Methods
    • Machine Learning Software
    • Machine Learning Techniques
    • Machine Learning Feature Selection
    • Ensemble Methods in Machine Learning
    • Support Vector Machine in Machine Learning
    • Decision Making Techniques
    • Restricted Boltzmann Machine
    • Regularization Machine Learning
    • What is Regression?
    • What is Linear Regression?
    • Dataset for Linear Regression
    • Decision tree limitations
    • What is Decision Tree?
    • What is Random Forest
  • Algorithms
    • Machine Learning Algorithms
    • Apriori Algorithm in Machine Learning
    • Types of Machine Learning Algorithms
    • Bayes Theorem
    • AdaBoost Algorithm
    • Classification Algorithms
    • Clustering Algorithm
    • Gradient Boosting Algorithm
    • Mean Shift Algorithm
    • Hierarchical Clustering Algorithm
    • Hierarchical Clustering Agglomerative
    • What is a Greedy Algorithm?
    • What is Genetic Algorithm?
    • Random Forest Algorithm
    • Nearest Neighbors Algorithm
    • Weak Law of Large Numbers
    • Ray Tracing Algorithm
    • SVM Algorithm
    • Naive Bayes Algorithm
    • Neural Network Algorithms
    • Boosting Algorithm
    • XGBoost Algorithm
    • Pattern Searching
    • Loss Functions in Machine Learning
    • Decision Tree in Machine Learning
    • Hyperparameter Machine Learning
    • Unsupervised Machine Learning
    • K- Means Clustering Algorithm
    • KNN Algorithm
    • Monty Hall Problem
  • Supervised
    • What is Supervised Learning
    • Supervised Machine Learning
    • Supervised Machine Learning Algorithms
    • Perceptron Learning Algorithm
    • Simple Linear Regression
    • Polynomial Regression
    • Multivariate Regression
    • Regression in Machine Learning
    • Hierarchical Clustering Analysis
    • Linear Regression Analysis
    • Support Vector Regression
    • Multiple Linear Regression
    • Linear Algebra in Machine Learning
    • Statistics for Machine Learning
    • What is Regression Analysis?
    • Clustering Methods
    • Backward Elimination
    • Ensemble Techniques
    • Bagging and Boosting
    • Linear Regression Modeling
    • What is Reinforcement Learning
  • Classification
    • Kernel Methods in Machine Learning
    • Clustering in Machine Learning
    • Machine Learning Architecture
    • Automation Anywhere Architecture
    • Machine Learning C++ Library
    • Machine Learning Frameworks
    • Data Preprocessing in Machine Learning
    • Data Science Machine Learning
    • Classification of Neural Network
    • Neural Network Machine Learning
    • What is Convolutional Neural Network?
    • Single Layer Neural Network
    • Kernel Methods
    • Forward and Backward Chaining
    • Forward Chaining
    • Backward Chaining
  • Deep Learning
    • What Is Deep learning
    • Overviews Deep Learning
    • Application of Deep Learning
    • Careers in Deep Learnings
    • Deep Learning Frameworks
    • Deep Learning Model
    • Deep Learning Algorithms
    • Deep Learning Technique
    • Deep Learning Networks
    • Deep Learning Libraries
    • Deep Learning Toolbox
    • Types of Neural Networks
    • Convolutional Neural Networks
    • Create Decision Tree
    • Deep Learning for NLP
    • Caffe Deep Learning
    • Deep Learning with TensorFlow
  • RPA
    • What is RPA
    • What is Robotics?
    • Benefits of RPA
    • RPA Applications
    • Types of Robots
    • RPA Tools
    • Line Follower Robot
    • What is Blue Prism?
    • RPA vs BPM
  • UiPath
    • What is UiPath
    • UiPath Action Center
    • UiPath?Orchestrator
    • UiPath web automation
    • UiPath Orchestrator API
    • UiPath Delay
    • UiPath Careers
    • UiPath Insights
    • UiPath Split String
    • UiPath Installation
    • UiPath Filter Data Table
    • UiPath Test Suite
    • UiPath Competitors
    • UiPath Architecture
    • UiPath version
    • Uipath Reframework
    • UiPath Studio
  • Interview Questions
    • Deep Learning Interview Questions And Answer
    • Machine Learning Cheat Sheet

Related Courses

Machine Learning Training

Deep Learning Training

Artificial Intelligence Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Independence Day Offer - Machine Learning Training Learn More