日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[pytorch] 通过一个例子分析torch.matmul矩阵与向量相乘的维度

發布時間:2024/9/20 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [pytorch] 通过一个例子分析torch.matmul矩阵与向量相乘的维度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

pytorch文檔中關于torch.matmul()的維度說明如下:

?

  • If both tensors are 1-dimensional, the dot product (scalar) is returned.
  • If both arguments are 2-dimensional, the matrix-matrix product is returned.
  • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.
  • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.
  • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (i.e. batch) dimensions are?broadcasted?(and thus must be broadcastable).

最后一項可能光看文檔難以理解,下面舉個例子:

  • >>a = torch.arange(0,18).view(2,3,3)

  • >>b = torch.ones(3,dtype=torch.long)

  • >>c = torch.matmul(a,b)

  • >>d = torch.matmul(b,a)

  • >>a.size()

  • torch.Size([2, 3, 3])

  • >>b.size()

  • torch.Size([3])

  • >>c.size()

  • torch.Size([2, 3])

  • >>d.size()

  • torch.Size([2, 3])

  • >>a[0]

  • tensor([[0, 1, 2],

  • [3, 4, 5],

  • [6, 7, 8]])

  • >>a[1]

  • tensor([[ 9, 10, 11],

  • [12, 13, 14],

  • [15, 16, 17]])

  • >>c

  • tensor([[ 3, 12, 21],

  • [30, 39, 48]])

  • >>d

  • tensor([[ 9, 12, 15],

  • [36, 39, 42]])

  • 通過分析上面的結果我們可以發現,c = torch.matmul(a,b)等價于將a[0]和a[1]這兩個(3,3)維的矩陣與b這個3維向量相乘:a[0]×b和a[1]×b,而d = torch.matmul(b,a)只是相乘順序相反:b×a[0]和b×a[1].

    來源:https://blog.csdn.net/CVAIDL/article/details/107752697

    總結

    以上是生活随笔為你收集整理的[pytorch] 通过一个例子分析torch.matmul矩阵与向量相乘的维度的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。