本文目录
一、numpy的矩阵乘法
numpy.dot(a, b, out=None)
当两个数据均为一维的时候,结果是内积,等效于np.matmul or a@b
。当两个数据均为二维的时候,结果是矩阵乘法,依然等效于np.matmul or a@b
# 1-D array
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result_ab = np.dot(a, b)
result_ba = np.dot(b, a)
print('result_ab: %s' % (result_ab))
print('result_ba: %s' % (result_ba))
'''
result_ab: 32
result_ba: 32
'''
# 2-D array
a = np.array([[1, 2, 3], [4, 5, 6]]) # 2-D array: 2 x 3
b = np.array([[1, 2], [3, 4], [5, 6]]) # 2-D array: 3 x 2
result_ab = np.dot(a, b)
result_ba = np.dot(b, a)
print('dot_result_ab:\n %s' %(result_ab))
print('dot_result_ba:\n %s' %(result_ba))
'''
dot_result_ab:
[[22 28]
[49 64]]
dot_result_ba:
[[ 9 12 15]
[19 26 33]
[29 40 51]]
'''
result_ab = np.matmul(a, b)
result_ba = np.matmul(b, a)
print('matmul_result_ab:\n %s' %(result_ab))
print('matmul_result_ba:\n %s' %(result_ba))
'''
matmul_result_ab:
[[22 28]
[49 64]]
matmul_result_ba:
[[ 9 12 15]
[19 26 33]
[29 40 51]]
'''
- 如果一个是向量,一个是标量,等价于
np.multiply or a*b
# array and scalar
a = np.array([[1, 2, 3], [4, 5, 6]]) # 2-D array: 2 x 3
b = 3 # 标量
result_ab = np.dot(a, b)
print('result_ab:\n %s' %(result_ab))
'''
result_ab:
[[ 3 6 9]
[12 15 18]]
'''
multiply_result_ab = np.multiply(a,b)
print('multiply_result_ab:\n %s' %(multiply_result_ab))
'''
multiply_result_ab:
[[ 3 6 9]
[12 15 18]]
'''
- 如果a是N-D数组,b是1-D数组,最终是a和b最后一个轴的乘积(a sum product over the last axis of a and b)
a = np.array([[1, 2, 3], [4, 5, 6]]) # array: 2 x 3
b = np.array([1, 2, 3]) # array: 1 x 3
result_ab = np.dot(a, b)
print('result_ab:\n %s' %(result_ab))
'''
result_ab:
[14 32]
'''
numpy.matmul(x1, x2)
面对2-D array与2-D array时与dot功能相同,同时类似于@,与dot不同的是matmul不能够进行矩阵与标量的乘法
a = np.array([[1, 0], [0, 1]]) # array: 2 x 2
b = np.array([[4, 1],[2, 2]]) # array: 2 x 2
result_ab = np.matmul(a, b)
print('result_ab:\n %s' %(result_ab))
'''
result_ab:
[[4 1]
[2 2]]
'''
a = np.array([[1, 0], [0, 1]]) # array: 2 x 2
b = np.array([1, 2]) # array: 1 x 2
result_ab = np.matmul(a, b)
print('result_ab:\n %s' %(result_ab))
'''
result_ab:
[1 2]
'''
numpy.multiply(x1, x2)
numtiply针对的是标量的运算,同时类似于*,当两个向量参与运算时,得到的结果是对应位置的乘积
result = np.multiply(2.0, 4.0)
print(result)
8.0
x1 = np.arange(9.0).reshape((3, 3))
x2 = np.arange(3.0)
result = np.multiply(x1, x2)
print(x1)
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
print(x2)
[0. 1. 2.]
print(result)
[[ 0. 1. 4.]
[ 0. 4. 10.]
[ 0. 7. 16.]]
二、torch的矩阵乘法
torch.mul(input, other, *, out=None) → Tensor
有两种情况
- input是一个向量,other是一个标量或整数即可直接相乘
- input是一个向量,other是一个向量,对应位相乘且遵从broadcast机制
# input:tensor--output:number
a = torch.randn(3)
a
tensor([ 0.2015, -0.4255, 2.6087])
torch.mul(a, 100)
tensor([ 20.1494, -42.5491, 260.8663])</code></pre>
# input:tensor--output:tensor
# -----with same dimension----
a = torch.rand(3, 2)
a
tensor([[0.8705, 0.0193],
[0.8379, 0.3961],
[0.4431, 0.4504]])
b = torch.rand(3, 2)
tensor([[0.9896, 0.3956],
[0.2082, 0.5905],
[0.6654, 0.0141]])
print(torch.mul(a, b))
tensor([[0.8615, 0.0076],
[0.1744, 0.2339],
[0.2949, 0.0064]])
# -----with different dimension---obey broadcast
a = torch.randn(4, 1)
a
tensor([[ 1.1207],
[-0.3137],
[ 0.0700],
[ 0.8378]])
b = torch.randn(1, 4)
b
tensor([[ 0.5146, 0.1216, -0.5244, 2.2382]])
torch.mul(a, b)
tensor([[ 0.5767, 0.1363, -0.5877, 2.5083],
[-0.1614, -0.0382, 0.1645, -0.7021],
[ 0.0360, 0.0085, -0.0367, 0.1567],
[ 0.4312, 0.1019, -0.4394, 1.8753]])</code></pre>
torch.mm(input, mat2, *, out=None) → Tensor
仅支持矩阵相乘,输入为(m,n)的矩阵,另一个矩阵维度必须为(n,p)的形式,因为此函数不支持broadcast机制
mat1 = torch.randn(2, 3)
mat2 = torch.randn(3, 3)
torch.mm(mat1, mat2)
tensor([[ 0.4851, 0.5037, -0.3633],
[-0.0760, -3.6705, 2.4784]])