>>> a
array([[1, 2],
[3, 4]])
>>> b
matrix([[1, 2],
[3, 4]])
>>> c
array([1, 2, 3, 4])
>>> d
array([[1, 2],
[3, 4],
[5, 6]])
>>> e
array([[1, 3, 5],
[2, 4, 6]])
>>> f
matrix([[1, 2],
[3, 4],
[5, 6]])
>>> g
matrix([[1, 3, 5],
[2, 4, 6]])
>>> h
array([1, 3, 5])
>>> i
array([[1],
[3],
[5]])
>>> j
array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]]])
>>> np.multiply(a,a)
array([[ 1, 4],
[ 9, 16]])
>>> np.multiply(b,b)
matrix([[ 1, 4],
[ 9, 16]])
>>> np.multiply(c,c)
array([ 1, 4, 9, 16])
>>> np.multiply(d,d)
array([[ 1, 4],
[ 9, 16],
[25, 36]])
>>> np.multiply(f,f)
matrix([[ 1, 4],
[ 9, 16],
[25, 36]])
>>> np.multiply(d,e)
ValueError: operands could not be broadcast together with shapes (3,2) (2,3)
>>> np.multiply(d,h)
ValueError: operands could not be broadcast together with shapes (3,2) (3,)
>>> np.multiply(d,i)
array([[ 1, 2],
[ 9, 12],
[25, 30]])
>>> np.multiply(e,h)
array([[ 1, 9, 25],
[ 2, 12, 30]])
>>> np.multiply(j,d)
array([[[ 1, 4],
[ 9, 16],
[25, 36]],
[[ 7, 16],
[27, 40],
[55, 72]]])
>>> np.multiply(j,i)
array([[[ 1, 2],
[ 9, 12],
[25, 30]],
[[ 7, 8],
[27, 30],
[55, 60]]])
结论:对向量、二维数组、矩阵都是元素乘法,可以和列向量或行向量广播
>>> np.dot(a,a)
array([[ 7, 10],
[15, 22]])
>>> np.dot(b,b)
matrix([[ 7, 10],
[15, 22]])
>>> np.dot(c,c)
30
>>> np.dot(d,d)
ValueError: shapes (3,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)
>>> np.dot(d,e)
array([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]])
>>> np.dot(f,g)
matrix([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]])
>>> np.dot(h,i)
array([35])
>>> np.dot(j,e)
array([[[ 5, 11, 17],
[ 11, 25, 39],
[ 17, 39, 61]],
[[ 23, 53, 83],
[ 29, 67, 105],
[ 35, 81, 127]]])
结论:对向量、二维数组、矩阵都是内积(即数量积或者说矩阵乘法)(注意内积运算数据类型要统一,比如同为long或同为int),可以广播
>>> a*a
array([[ 1, 4],
[ 9, 16]])
>>> b*b
matrix([[ 7, 10],
[15, 22]])
>>> c*c
array([ 1, 4, 9, 16])
>>> d*d
array([[ 1, 4],
[ 9, 16],
[25, 36]])
>>> f*f
ValueError: shapes (3,2) and (3,2) not aligned: 2 (dim 1) != 3 (dim 0)
>>> d*e
ValueError: operands could not be broadcast together with shapes (3,2) (2,3)
>>> f*g
matrix([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]])
>>> h*i
array([[ 1, 3, 5],
[ 3, 9, 15],
[ 5, 15, 25]])
>>> np.mat(h)*np.mat(i)
matrix([[35]])
>>> j*e
ValueError: operands could not be broadcast together with shapes (2,3,2) (2,3)
>>> np.mat(j)*np.mat(e)
ValueError: shape too large to be a matrix.
>>> j*j
array([[[ 1, 4],
[ 9, 16],
[ 25, 36]],
[[ 49, 64],
[ 81, 100],
[121, 144]]])
结论:对向量和二维数组只能计算可广播的元素乘法,对二维矩阵只能计算内积(即要求两个因式的尺寸互文)
>>> a
tensor([[1, 2],
[3, 4]], dtype=torch.int32)
>>> c
tensor([1, 2, 3, 4], dtype=torch.int32)
>>> d
tensor([[1, 2],
[3, 4],
[5, 6]], dtype=torch.int32)
>>> e
tensor([[1, 3, 5],
[2, 4, 6]], dtype=torch.int32)
>>> h
tensor([1, 3, 5], dtype=torch.int32)
>>> i
tensor([[1],
[3],
[5]], dtype=torch.int32)
>>> j
tensor([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]]], dtype=torch.int32)
>>> aa
tensor(indices=tensor([[0, 0, 1, 1],
[0, 1, 0, 1]]),
values=tensor([1, 2, 3, 4]),
size=(2, 2), nnz=4, dtype=torch.int32, layout=torch.sparse_coo)
>>> a*a
tensor([[ 1, 4],
[ 9, 16]], dtype=torch.int32)
>>> c*c
tensor([ 1, 4, 9, 16], dtype=torch.int32)
>>> d*d
tensor([[ 1, 4],
[ 9, 16],
[25, 36]], dtype=torch.int32)
>>> d*e
RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1
>>> d*h
RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at non-singleton dimension 1
>>> e*h
tensor([[ 1, 9, 25],
[ 2, 12, 30]], dtype=torch.int32)
>>> d*i
tensor([[ 1, 2],
[ 9, 12],
[25, 30]], dtype=torch.int32)
>>> j*d
tensor([[[ 1, 4],
[ 9, 16],
[25, 36]],
[[ 7, 16],
[27, 40],
[55, 72]]], dtype=torch.int32)
>>> j*i
tensor([[[ 1, 2],
[ 9, 12],
[25, 30]],
[[ 7, 8],
[27, 30],
[55, 60]]], dtype=torch.int32)
结论:只能计算元素乘法,二维及以上也可以和行向量或列向量广播
>>> a@a
tensor([[ 7, 10],
[15, 22]], dtype=torch.int32)
>>> c@c
tensor(30, dtype=torch.int32)
>>> d@d
RuntimeError: mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)
>>> d@e
tensor([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]], dtype=torch.int32)
>>> j@e
tensor([[[ 5, 11, 17],
[ 11, 25, 39],
[ 17, 39, 61]],
[[ 23, 53, 83],
[ 29, 67, 105],
[ 35, 81, 127]]], dtype=torch.int32)
结论:只能计算内积,可不同维度广播
>>> torch.mm(a,a)
tensor([[ 7, 10],
[15, 22]], dtype=torch.int32)
>>> torch.mm(c,c)
RuntimeError: self must be a matrix
>>> torch.mm(c,c.T)
RuntimeError: self must be a matrix
>>> torch.mm(d,d)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (3x2 and 3x2)
>>> torch.mm(d,e)
tensor([[ 5, 11, 17],
[11, 25, 39],
[17, 39, 61]], dtype=torch.int32)
>>> torch.mm(h,i)
RuntimeError: self must be a matrix
>>> torch.mm(i,h)
RuntimeError: mat2 must be a matrix
>>> torch.mm(j,e)
RuntimeError: self must be a matrix
结论:只能计算二维的内积
稠密矩阵的运算和torch.mm完全一致
>>> torch.spmm(aa,a)
tensor([[ 7, 10],
[15, 22]], dtype=torch.int32)
>>> torch.spmm(a,aa)
Traceback (most recent call last):
File "" , line 1, in <module>
RuntimeError: sparse tensors do not have strides
>>> torch.spmm(aa,aa)
Traceback (most recent call last):
File "" , line 1, in <module>
RuntimeError: sparse tensors do not have strides
结论:不支持交换律,只能按Sparse,Dense的顺序计算
>>> torch.bmm(j,jj)
tensor([[[ 5, 11, 17],
[ 11, 25, 39],
[ 17, 39, 61]],
[[113, 143, 173],
[143, 181, 219],
[173, 219, 265]]], dtype=torch.int32)
>>> torch.bmm(d,e)
RuntimeError: Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 'batch1' (while checking arguments for bmm)
>>> torch.bmm(torch.randn((1,2,2,5)),torch.randn((1,2,5,2)))
Traceback (most recent call last):
File "" , line 1, in <module>
RuntimeError: Expected 3-dimensional tensor, but got 4-dimensional tensor for argument #1 'batch1' (while checking arguments for bmm)
结论:只能计算三维的内积