torch示例
#input
>>>U = torch.ones((2,3))
>>>V = torch.arange(6,dtype=torch.float32).reshape(U.shape)
>>>v = torch.arange(3,dtype = torch.float32)
>>>print(f'U={U},\nV={V},\nv={v}')
#output
U=tensor([[1., 1., 1.],
[1., 1., 1.]]),
V=tensor([[0., 1., 2.],
[3., 4., 5.]])
v=tensor([0., 1., 2.])
torch.mm
矩阵乘法要求第一个矩阵的列等于第二个矩阵的行,e.g., U ∈ R m , n , V ∈ R m , n U\in\mathcal{R}^{m,n},V\in\mathcal{R}^{m,n} U∈Rm,n,V∈Rm,n为两个矩阵, O = t o r c h ( U , V . T ) ∈ R m , m O = torch(U,V.T)\in\mathcal{R}^{m,m} O=torch(U,V.T)∈Rm,m
#input
>>>torch.mm(U,V.T)#先对V进行转置,满足矩阵乘法要求,否则会报错
#output
tensor([[ 3., 12.],
[ 3., 12.]])
# input,当不满足矩阵乘法相乘要求时,报错
>>>torch.mm(U,V)
#output
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-309-5ac72b41dcde> in <module>
----> 1 torch.mm(U,V)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3)
torch.mv
矩阵与向量相乘,e.g. U ∈ R m , n , v ∈ R n U\in\mathcal{R}^{m,n},v\in\mathcal{R}^{n} U∈Rm,n,v∈Rn分别对应一个矩阵和向量
#input
>>>torch.mv(U,v)
#output
tensor([3., 3.])
torch.mul
矩阵与矩阵点乘(对应位置相乘),要求矩阵的形状完全一样,e.g., U ∈ R m , n , V ∈ R m , n U\in\mathcal{R}^{m,n},V\in\mathcal{R}^{m,n} U∈Rm,n,V∈Rm,n为两个矩阵
#input
>>>torch.mul(U,V)
#output
tensor([[0., 1., 2.],
[3., 4., 5.]])
##另一种表示方法
#input
>>>U*V
#output
tensor([[0., 1., 2.],
[3., 4., 5.]])
torch.norm
对应向量的 L 2 \mathcal{L}_{2} L2范数,即 ∥ v ∥ 2 \Vert v\Vert_{2} ∥v∥2
#input
>>>torch.norm(v)
#output
tensor(2.2361)
##另一种表示方法
#input
>>>torch.sqrt(sum(v**2))
#output
tensor(2.2361)
区分torch.mm、torch.matmul和torch.bmm
1. torch.mm
- 功能描述:
torch.mm
用于两个二维矩阵的矩阵乘法。它接受两个形状为(m, n)
和(n, p)
的张量,返回一个形状为(m, p)
的张量。 - 限制: 它仅限于二维张量,如果尝试使用不是二维的张量,将会抛出错误。
- 用法示例:
# 假设 mat1 和 mat2 都是二维张量
result = torch.mm(mat1, mat2)
2. torch.matmul
- 功能描述:
torch.matmul
是一个更通用的矩阵乘法运算符,它支持张量点乘、矩阵乘法以及批量矩阵乘法。它可以自动处理两个张量的维度,执行相应的乘法操作。 - 灵活性: 它可以处理两个以上维度的张量,并且在遇到高维张量时,它会应用广播规则。
- 用法示例:
# 对于二维张量,它等同于 torch.mm
result = torch.matmul(mat1, mat2)
# 对于高维张量,它会执行批量矩阵乘法或点乘
result = torch.matmul(tensor1, tensor2)
3. torch.bmm
- 功能描述:
torch.bmm
用于两批三维张量的矩阵乘法。它接受两个形状为(b, m, n)
和(b, n, p)
的张量,返回一个形状为(b, m, p)
的张量,其中b
是批次大小。 - 限制: 它仅限于批量的三维张量,如果输入张量的维度不是三维的,将会抛出错误。
- 用法示例:
# 假设 batch1 和 batch2 都是三维张量
result = torch.bmm(batch1, batch2)
结论:
torch.mm
仅用于二维矩阵乘法。torch.matmul
是一个更通用的矩阵乘法接口,可以处理高维张量,并且在必要时应用广播规则。torch.bmm
专门用于批量的三维张量矩阵乘法。
在选择使用哪一个函数时,应根据张量的维度和所需的操作来决定。如果你正在处理的是标准的二维矩阵乘法,torch.mm
就足够了。如果你的矩阵乘法涉及到高维张量或者需要广播支持,那么 torch.matmul
会是更好的选择。而当你需要对一批三维张量进行矩阵乘法时,应该使用 torch.bmm
。
numpy 示例
#input
>>> U = U.numpy()
>>>V = V.numpy()
>>>v = v.numpy()
>>>print(f'U={U},\nV={V},\nv={v}')
#output
U=[[1. 1. 1.]
[1. 1. 1.]],
V=[[0. 1. 2.]
[3. 4. 5.]],
v=[0. 1. 2.]
np.multipy
矩阵点乘,同torch.mul
#input,矩阵与矩阵点乘
>>>import numpy as np
>>>np.multiply(U,V)
#output
array([[0., 1., 2.],
[3., 4., 5.]], dtype=float32)
##另一种表示方法
#input
>>>U*V
#output
array([[0., 1., 2.],
[3., 4., 5.]], dtype=float32)
#input,矩阵向量点乘,向量自动广播
>>>U*v
#output
array([[0., 1., 2.],
[0., 1., 2.]], dtype=float32)
np.dot
矩阵和向量乘法,同torch.mm,torch.mv
#input,矩阵与矩阵相乘
>>>np.dot(U,V.T)
#output
array([[ 3., 12.],
[ 3., 12.]], dtype=float32)
#input,矩阵向量相乘
>>>np.dot(U,v)
#output
array([3., 3.], dtype=float32)