numpy和torch的乘法函数

numpy

>>> 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

>>> 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 和 np.matmul 和 .dot 和 @

>>> 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]]])

结论:对向量和二维数组只能计算可广播的元素乘法,对二维矩阵只能计算内积(即要求两个因式的尺寸互文)

torch

>>> 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)

* 和 torch.mul

>>> 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)

结论:只能计算元素乘法,二维及以上也可以和行向量列向量广播

@ 和 torch.matmul

>>> 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

>>> 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.spmm

稠密矩阵的运算和torch.mm完全一致

>>> torch.spmm(aa,a)
tensor([[ 7, 10],
        [15, 22]], dtype=torch.int32)
>>> torch.spmm(a,aa)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: sparse tensors do not have strides
>>> torch.spmm(aa,aa)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: sparse tensors do not have strides

结论:不支持交换律,只能按Sparse,Dense的顺序计算

torch.bmm

>>> 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 "<stdin>", line 1, in <module>
RuntimeError: Expected 3-dimensional tensor, but got 4-dimensional tensor for argument #1 'batch1' (while checking arguments for bmm)

结论:只能计算三维的内积

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值