SoFunction
Updated on 2025-03-02

Pytorch matrix multiplication (the difference between  ,   )

1. Introduction

In the world of deep learning and neural networks, matrix multiplication is a crucial operation. As one of the most popular deep learning frameworks at present, PyTorch provides a variety of implementation methods for matrix multiplication. in,()()and()are three commonly used functions, but their usage and function are different. This article will explain the differences between these three functions in detail and demonstrate their usage through examples.

2. (): Multiplication at the element level

()Functions are used to executeElement levelmultiplication of , that is, multiplication of elements at the corresponding position. This function is especially useful for two tensors of the same shape.

import torch

# Create two tensors with the same shapetensor1 = ([[1, 2], [3, 4]])
tensor2 = ([[5, 6], [7, 8]])

# Use() to perform element-level multiplicationresult_mul = (tensor1, tensor2)
print(result_mul)

Output:

tensor([[19, 22],
        [43, 50]])

As you can see,()Willtensor1andtensor2Multiply the elements at the corresponding position to obtain a new tensor.

3. (): Matrix multiplication (only applicable to two-dimensional tensors)

()Functions are used to perform matrix multiplication, but it only works with two-dimensional tensors (i.e. matrices). If you are trying to use tensors above two-dimensional(), will get an error.

# Create two 2D tensorsmatrix1 = ([[1, 2], [3, 4]])
matrix2 = ([[5, 6], [7, 8]])

# Use() for matrix multiplicationresult_mm = (matrix1, matrix2)
print(result_mm)

Output:

tensor([[19, 22],
        [43, 50]])

Note that the rule of matrix multiplication is that the number of columns of the first matrix must be the same as the number of rows of the second matrix. In the above example,matrix1is a 2x2 matrix.matrix2It is also a 2x2 matrix, so they can be used for matrix multiplication.

4. (): Generalized matrix multiplication (applicable to any dimension tensor)

()Functions provide a wider range of matrix multiplication functions, which can handle tensors of any dimension. This function will automatically perform appropriate multiplication operations according to the dimensions of the tensor.

import torch


# Create two 2D tensorsmatrix1 = ([[1, 2], [3, 4]])
matrix2 = ([[5, 6], [7, 8]])

# Use() for matrix multiplicationresult_mm = (matrix1, matrix2)
print(result_mm)

# For two-dimensional tensors, () behaves the same as ()result_matmul_2d = (matrix1, matrix2)
print(result_matmul_2d)

# For tensors above two-dimensional, () can perform broadcast and batch matrix multiplicationtensor3d_1 = (3, 2, 4)  # 3 2x4 matricestensor3d_2 = (3, 4, 5)  # 3 4x5 matrices
# Batch matrix multiplicationresult_matmul_3d = (tensor3d_1, tensor3d_2)
print(result_matmul_3d.shape)  # The output should be (3, 2, 5), representing 3 2x5 matrices

Output:

tensor([[19, 22],
        [43, 50]])
tensor([[19, 22],
        [43, 50]])
([3, 2, 5])

()Functions are very flexible and can handle various complex tensor multiplication scenarios.

5. Summary and precautions

To sum up,()()and()The main difference between these three functions is that they handle tensors differently and their dimension requirements.()Perform element-level multiplication, requiring the input tensor to be the same shape;()Performing standard matrix multiplication, which is only applicable to two-dimensional tensors;()A more generalized matrix multiplication is provided, which can handle tensors of any dimension, including batch matrix multiplication.

When using these functions, you need to pay attention to the following points:

  • Make sure the shape of the input tensor meets the function's requirements, otherwise an error may be thrown.
  • For matrix multiplication, you need to pay attention to the dimension matching problem of the matrix, that is, the number of columns of the first matrix must be equal to the number of rows of the second matrix.
  • When multiplication of batch matrix, use()Multiplication operations of multiple matrices can be easily processed.

This is the end of this article about the difference between Pytorch matrix multiplication ((), () and ()). For more related content on Pytorch matrix multiplication, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!