也称为点积、内积、数量积、标量积,向量a[a1,a2,an]和向量b[b1,b2b .bn]的点乘的结果是标量,a.b; 得到价值。
叉(也称为向量积、外积、叉积。 叉乘是向量a[x1,y1,z1]和向量b[x2,y2,z2]的叉乘的运算结果为一个向量。 而且,两个向量的叉积垂直于由这两个向量组成的坐标平面。 得到记为axb)的向量。
a=torch.tensor ([ 1,2,3 ] ) b=torch.tensor ([ 2,3,4 ] [ 2,3,4 ] ) (print ) (torch.dot ) a,b ) ) torch ) torch ) tid1. 二维矩阵乘法 torch.mm()
torch.mm(mat1,mat2,out=None ) )。
2. 三维带batch的矩阵乘法 torch.bmm()
神经网络训练一般采用mini-batch,由于经常输入时采用三维带batch的矩阵,所以提供
torch.BMM(BMAT1,bmat2,out=None ) ) )。
一般不怎么用,只是在训练一个模型的时候用。
3. 多维矩阵乘法 torch.matmul()
torch.matmul(input,other,out=None ) )支持broadcast操作,有关乘以一般矩阵乘法,推荐使用这个matmul多维数据matmul ()的信息,请参见如果两个输入的维度分别是input(10005009911 ),other ) 5001199 ),则torch.matmul ) input,other,out=None )平方分析这两个参数的batch size分别为[1000500]和500x1,由于广播为[1000500],因此最终输出维数为10005009999。
4. 矩阵逐元素(Element-wise)乘法 torch.mul()
torch.mul(mat1,other,out=None ) )。
这里,other乘数可以是标量,也可以是任意维的矩阵,只要满足最终的乘法可以broadcast即可。
5. dot 函数
对等级为1的数组执行相应位置的乘法运算,然后进行加法运算。等价于向量的点乘;
如果对等级不为1的二维数组执行矩阵乘法,则为等价于矩阵的叉乘;
6. * 和 @
*表示矩阵每个元素的乘法
@表示矩阵乘法
x=torch.tensor ([ 1,2 ],[ 3,4 ] ) y=torch.tensor ([ 1,2 ],[ 3,4 ] ) print ) x*y ) print ) x@ )4)
1 .将向量视为1*n的矩阵,但要增加维数需要采用unsqueeze ) )函数。
置换的话,一般采用a.t (),或者A.Transpose ),1 )
2 .用于矩阵点乘法的torch.mul
3 .用于矩阵乘法的torch.matmul
importtorcha=torch.tensor ([ 1,2,3 ] )1* 3c=torch.tensor ([ 1,2,3 ],[ 4,5,6 ] )2*3print ) 4,4