Navigation

    Gpushare.com

    • Register
    • Login
    • Search
    • Popular
    • Categories
    • Recent
    • Tags

    矩阵相乘详解

    语音识别与语义处理领域
    1
    1
    53
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 175****1239
      175****1239 last edited by

      矩阵相乘详解

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

      在这里插入图片描述

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

      数学表示

      在这里插入图片描述

      程序表示

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

      if: A∈Rn×m,B∈Rm×nA\in R^{n\times m},B\in R^{m\times n}A∈Rn×m,B∈Rm×n

      then: torch.matmul(A, B) ∈Rn×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∈RB×n×mA\in R^{B\times n\times m}A∈RB×n×m,B∈RB×m×dB\in R^{B\times m\times d}B∈RB×m×d

      torch.bmm(A, B) ∈RB×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])
      
      1 Reply Last reply Reply Quote 1
      • First post
        Last post