pytorch矩阵相乘详解

矩阵相乘详解

pytorch矩阵相乘详解_第1张图片

已知三个矩阵 A , B , C A,B,C A,B,C

pytorch矩阵相乘详解_第2张图片

数学上的矩阵相乘 C = A × \times × B

数学表示

在这里插入图片描述

程序表示

多维矩阵:torch.matmul(A,B)

if: A ∈ R n × m , B ∈ R m × n A\in R^{n\times m},B\in R^{m\times n} ARn×m,BRm×n

then: torch.matmul(A, B) ∈ R n × n \in R^{n\times n} Rn×n

二维矩阵相乘:torch.mm(A,B)

# 矩阵相乘
x = tensor([[1, 2, 3],
            [3, 3, 4],
            [3, 3, 3]])

# torch.matmul表示矩阵的乘法
torch.matmul(x,x)
Out[1]: 
tensor([[16, 17, 20],
        [24, 27, 33],
        [21, 24, 30]])
        
# 两个维度对上就可以进行运算

x = tensor([[1, 2, 3],
            [3, 3, 4],
            [3, 3, 3]])
            
y = tensor([[1, 2],
            [3, 3],
            [4, 4]])
torch.matmul(x, y)
Out[2]: 
tensor([[19, 20],
        [28, 31],
        [24, 27]])

数学上的矩阵对位相乘

数学表示

在这里插入图片描述

程序表示

torch.mul(A,B)


# 表示矩阵对位相乘
x = tensor([[1, 2, 3],
            [3, 3, 4],
            [3, 3, 3]])
# 方法1
x * x
Out[3]: 
tensor([[ 1,  4,  9],
        [ 9,  9, 16],
        [ 9,  9,  9]])

# 方法2        
torch.mul(x,x)
Out[4]: 
tensor([[ 1,  4,  9],
        [ 9,  9, 16],
        [ 9,  9,  9]])

带有batch的三维就一阵相乘

torch.bmm(A, B)

A ∈ R B × n × m A\in R^{B\times n\times m} ARB×n×m, B ∈ R B × m × d B\in R^{B\times m\times d} BRB×m×d

torch.bmm(A, B) ∈ R B × n × d \in R^{B\times n\times d} RB×n×d

t = tensor([[[1, 2, 3],
             [3, 3, 4],
             [3, 3, 3]],
             
            [[1, 2, 3],
             [3, 3, 4],
             [3, 3, 3]]])

T = torch.bmm(t, t)
T.shape

Out[5]: torch.Size([2, 3, 3])

T
Out[6]: 
tensor([[[16, 17, 20],
         [24, 27, 33],
         [21, 24, 30]],
        [[16, 17, 20],
         [24, 27, 33],
         [21, 24, 30]]])
         
         
# 两个维度不同
u = tensor([[[1, 2],
             [3, 3],
             [4, 4]],
            [[1, 2],
             [3, 3],
             [4, 4]]])
t = tensor([[[1, 2, 3],
             [3, 3, 4],
             [3, 3, 3]],
            [[1, 2, 3],
             [3, 3, 4],
             [3, 3, 3]]])
             
u.shape
Out[7]: torch.Size([2, 3, 2])
t.shape
Out[8]: torch.Size([2, 3, 3])

torch.bmm(t, u)
Out[9]: 
tensor([[[19, 20],
         [28, 31],
         [24, 27]],
        [[19, 20],
         [28, 31],
         [24, 27]]])

torch.bmm(t, u).shape
Out[10]: torch.Size([2, 3, 2])

你可能感兴趣的:(论文笔记,Python,矩阵相乘,torch,人工智能)