numpy和tensorflow中矩阵乘法的区别

np.dot

 

numpy.dot(a, b, out=None)

~~~Dot product of two arrays. Specifically,

  • If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).[one dimension的话(两个都是1维),就是数组内各个元素的乘积]
  • If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.[two dimension的话(两个都是2维)就是矩阵乘法,但是推荐用np.matmull()这个方法]
  • If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.[如果两个中有一个或者都是标量的话,就直接认为是数乘即可,用np.multiply(a, b)即可,或者直接用符号*]
  • If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.[如果a是N维,b是1维(并不是随便的1维就行,要和a匹配上),最后就是a和b在最后axis上的积的和]

    例如: 
    a = [[1,0],[4,5]] 
    b = [3,10] 
    np.dot(a, b) 
    output:[3,62]

  • If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:

 

np.matmull()

~~~Matrix product of two arrays.

  • If both arguments are 2-D they are multiplied like conventional matrices.
  • If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
  • If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
  • If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed. 
    Note:np.matmull()平常最常用的就是矩阵的乘法。
 

tf.matmull()

~~~Multiplies matrix a by matrix b, producing a * b. 
Note:tf.matmull()做的也是矩阵的乘法。但是np.matmull()和tf.matmull()还是有区别的,最起码他们各自的返回值是不一样的,tf返回的是tensor,np返回的是ndarray。

原文地址:https://www.cnblogs.com/ZT-SummerRain/p/8301166.html