系列文章目录
本系列记录自己的代码学习知识
torch.matmul的前后两个矩阵维度不同的小结
前言
一、torch.matmul()
torch.matmul()也是一种类似于矩阵相乘操作的tensor联乘操作。但是它可以利用python 中的广播机制,处理一些维度不同的tensor结构进行相乘操作。这也是该函数与torch.bmm()区别所在。
torch.matmul(input,other)#我们现以不同的名字区分前后两个矩阵
来源
作者:top_小酱油
链接:https://www.jianshu.com/p/e277f7fc67b3
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
二、详解解释
1.input维度比other大
例如:
n = ([1,2,3],[1,2,3])
n=torch.tensor(n)
print('n.size',n.size())
n.size torch.Size([2, 3])
m = ([1,2,3])
m=torch.tensor(m)
print('m.size',m.size())
m.size torch.Size([3])
b=torch.matmul(n,m)
print('b',b)
print('b.shape',b.shape)
b tensor([14, 14])
b.shape torch.Size([2])
解释:
2.input维度比other小
例如:
import torch
x = ([1,2])
x=torch.tensor(x)
print('x.size',x.size())
x.size torch.Size([2])
y = ([1,2,3],[1,2,3])
y=torch.tensor(y)
print('y.size',y.size())
y.size torch.Size([2, 3])
a=torch.matmul(x,y)
print('a',a)
print('a.shape',a.shape)
a tensor([3, 6, 9])
a.shape torch.Size([3])
解释:
总结
结合看到的知识,自己重新整理。