EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials PyTorch Tutorial PyTorch bmm
Secondary Sidebar
PyTorch Tutorial
  • PyTorch
    • PyTorch Image Classification
    • PyTorch Random
    • PyTorch Variable
    • PyTorch Activation Function
    • Python Formatted String
    • PyTorch GPU
    • PyTorch CUDA
    • PyTorch DataLoader
    • PyTorch LSTM
    • PyTorch Pad
    • PyTorch OpenCL
    • PyTorch Lightning
    • PyTorch SoftMax
    • PyTorch Flatten
    • PyTorch gan
    • PyTorch max
    • PyTorch pip
    • PyTorch Parameter
    • PyTorch Load Model
    • PyTorch Distributed
    • PyTorch BERT
    • PyTorch interpolate
    • PyTorch JIT
    • PyTorch expand
    • PyTorch AMD
    • PyTorch GRU
    • PyTorch rnn
    • PyTorch permute
    • PyTorch argmax
    • PyTorch SGD
    • PyTorch nn
    • PyTorch One Hot Encoding
    • 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

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)

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

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)

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)

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
Popular Course in this category
Machine Learning Training (20 Courses, 29+ Projects)
  19 Online Courses |  29 Hands-on Projects |  178+ Hours |  Verifiable Certificate of Completion
4.7
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
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
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*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.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & 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

*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.

Let’s Get Started

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