Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.matmul function

PyTorch torch.matmul function


PyTorch torch reference manual PyTorch torch reference manual

torch.matmul is a function in PyTorch used to perform matrix multiplication. It supports inputs of different dimensions and can handle one-dimensional, two-dimensional, and higher-dimensional tensor multiplication.

This is one of the most commonly used operations in deep learning; the forward pass of a neural network is essentially a series of matrix multiplications.

Function definition

torch.matmul(input, other, out=None)

Parameters:

Return Value:


Usage examples

Example 1: Two-dimensional matrix multiplication

Example

import

torch

Create two two-dimensional matrices

a

=

torch.

randn

(

3

,

4

)

 

3x4 matrix

b

=

torch.

randn

(

4

,

5

)

 

4x5 matrix

Matrix multiplication

c

=

torch.

matmul

(

a

,

b

)

print

(

"Shape of a:"

,

a.

shape

)

print

(

"Shape of b:"

,

b.

shape

)

print

(

"Shape of c:"

,

c.

shape

)

The output is:

a 的形状: torch.Size([3, 4])

b 的形状: torch.Size([4, 5])

c 的形状: torch.Size([3, 5])

Example 2: Vector-matrix multiplication

Example

import

torch

Create a vector and a matrix

vector

=

torch.

randn

(

4

)

     

4-dimensional vector

matrix

=

torch.

randn

(

4

,

5

)

 

4x5 matrix

Vector-matrix multiplication

result

=

torch.

matmul

(

vector

,

matrix

)

print

(

"Shape of the vector:"

,

vector.

shape

)

print

(

"Shape of the matrix:"

,

matrix.

shape

)

print

(

"Shape of the result:"

,

result.

shape

)

print

(

result

)

The output is:

向量的形状: torch.Size([4])

矩阵的形状: torch.Size([4, 5])

结果的形状: torch.Size([5])

tensor([-0.7837,  0.3684, -0.6542, -0.4594,  1.5328])

示例 3: 批量矩阵乘法

实例

import torch

# 创建批量矩阵
batch_a = torch.randn(10, 3, 4)  # 10 个 3x4 矩阵
batch_b = torch.randn(10, 4, 5)  # 10 个 4x5 矩阵

# 批量矩阵乘法
batch_c = torch.matmul(batch_a, batch_b)

print("批量 a 的形状:", batch_a.shape)
print("批量 b 的形状:", batch_b.shape)
print("批量结果 c 的形状:", batch_c.shape)

输出结果为:


批量 a 的形状: torch.Size([10, 3, 4])

批量 b 的形状: torch.Size([10, 4, 5])

批量结果 c 的形状: torch.Size([10, 3, 5])

Batch matrix multiplication is a common operation in deep learning, for example in the attention mechanism of Transformers.

Example 4: Matrix multiplication in neural networks

Example

import

torch

Simulating a neural network layer: input xW + b

x

=

torch.

randn

(

32

,

128

)

 

Batch size 32, feature dimension 128

W

=

torch.

randn

(

128

,

256

)

 

Weight matrix

b

=

torch.

randn

(

256

)

     

bias vector

Linear transformation: y = x @ W^T + b (in PyTorch, W is usually transposed)

Here we demonstrate x @ W

y

=

torch.

matmul

(

x

,

W

)

  • b

print

(

"Input x shape:"

,

x.

shape

)

print

(

"Weight W shape:"

,

W.

shape

)

print

(

"Output y shape:"

,

y.

shape

)

The output is:

输入 x 形状: torch.Size([32, 128])

权重 W 形状: torch.Size([128, 256])

输出 y 形状: torch.Size([32, 256])

This example simulates the forward propagation process of a fully connected layer in a neural network.


Notes

  • torch.matmul Broadcasting is supported, but the last two dimensions of both inputs must satisfy the dimensional requirements for matrix multiplication.
  • For two-dimensional matrix multiplication, you can also use torch.mm(), but torch.matmul More general.
  • Note the difference torch.matmul (matrix multiplication) and torch.mul (Element-wise multiplication).

PyTorch torch reference manual Pytorch torch Reference Manual