dot的用法比较搞,主要是因为要分情况,a,b的位置不同,结果就不同。 其中重要的不仅仅是对于a,b的维度判断,因为这对于a,b哪个axis做alignment很重要(否则就要报错),然后对于产生结果的shape也有直接影响。废话不说,直接列出以下所有情况:
#%% dot of array:
# 情况1: If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
# 本来也就是处理a,b形状一样的情况 也就是shape=(n,)
# 不需要转置直接内积,最后得到一个scalar
# a,b 严格来说既不是column vector也不是row vector, 但是可以act like either one based on the position in a dot product.
a = np.array([1,2,3])
b = np.array([1,2,3])
c = np.dot(a,b)
print("c as inner product of vectors:", c) # c = 14
# 情况2:If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
a = np.arange(1,5).reshape((2,2))
b = np.arange(5,9).reshape((2,2))
c = np.dot(a,b)
print(c) # [[19 22],[43 50]]
# 情况3:If both a and b are 0-D arrays, it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
a = 1
b = 2
c = np.dot(1,2)
print(c) # 2
# 情况4: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=np.arange(3).reshape((1,3))
b= np.arange(3) # 这里b是1D,shape(3,)
c= np.dot(a,b) # 用a的最后一个axis也就是3 去align b的唯一axis3, 匹配,然后分别乘法相加
print(c) # 5 as shape(1,) 因为上一步a和b的3都align 相当于抵了,剩下一个a的(1,)就作为c的shape
print(c.shape)
# 情况5: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:
# 这种情况就是需要第一个变量的最后axis去匹配第二个变量的倒数第二个axis
d=np.arange(12).reshape((3,4))
c= np.dot(b,d) # b: shape(3,) d: shape(3,4)
print(c) # array([20, 23, 26, 29]) 其中用b的3 去匹配倒数第二个axis也就是3,那么匹配,所以相互乘法后相加
print(c.shape) # 相互约去3之后,只有d剩一个(,4)这种情况放在shape里就是(4,)