tensor乘运算
torch.mul(a, b) 是矩陣 對應位相乘,即點乘操作, a和b的維度必須相等,a的維度是(1,2), 則b的維度必須是(1,2), 返回還是(1,2)的矩陣
torch.mm(a,b)是矩陣a和b矩陣相乘,a的維度是(1,2),b的維度是(2,3),返回是(1,3)的矩陣
torch.bmm(a,b)是矩陣a和b在維度1、2上矩陣相乘,一般要求是 三維矩陣,a的維度是(64,1,2),b的維度是(64,2,3)返回的是(64,1,3)矩陣
import torchif __name__ == "__main__":x = torch.ones(1, 2)y = torch.ones(1, 2) * 2z = torch.mul(x, y)print("torch.mul() example ")print("x.shape is ", x.shape) print("y.shape is ", y.shape)print("z.shape is ", z.shape) ## [1, 2]x = torch.ones(1, 2)y = torch.ones(2, 3) z = torch.mm(x, y)print("torch.mm() example ")print("x.shape is ", x.shape)print("y.shape is ", y.shape)print("z.shape is ", z.shape) ## [1,3]x = torch.randn(64, 1, 2)y = torch.randn(64, 2, 3)z = torch.bmm(x, y)print("torch.bmm example ")print("x.shape is ", x.shape)print("y.shape is ", y.shape)print("z.shape is ", z.shape) ## [64, 1, 3]
轉載于:https://www.cnblogs.com/yeran/p/11288171.html
總結
- 上一篇: 鼋头渚樱花一般什么时候去看比较好
- 下一篇: 新车保险上户多少钱