PyTorch .mm()
Published Oct 17, 2024
Contribute to Docs
In PyTorch, the .mm() method calculates the matrix product of two given tensors.
Syntax
torch.mm(ten1, ten2, *, out=None)
ten1: The first tensor to be multiplied.ten2: The second tensor to be multiplied.out(Optional): The output tensor to be used. The default value isNone.
Note: If
ten1is a(m x n)tensor andten2is a(n x p)tensor, thenoutwill be a(m x p)tensor.
Example
The following example demonstrates the usage of the .mm() method:
import torch# Define two tensorsten1 = torch.tensor([[1, 2, 3],[4, 3, 8],[1, 7, 2]])ten2 = torch.tensor([[2, 4, 1],[1, 3, 6],[2, 6, 5]])# Multiply the tensorsout = torch.mm(ten1, ten2)print(out)
The above code produces the following output:
tensor([[10, 28, 28],[27, 73, 62],[13, 37, 53]])
Codebyte Example
The following codebyte example shows the use of the .mm() method:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours