python中Hadamard product和matrix product的区分

时间:2021-03-28 16:12:21

先简单说一下Hadamard product: 
python中Hadamard product和matrix product的区分 
(参照*:https://en.wikipedia.org/wiki/Hadamard_product_(matrices)
而matrix product为: 
python中Hadamard product和matrix product的区分 
(参照*:matrix product) 
这两者是完全不一样的,但为什么python都是用∗∗表示呢?通过查阅官方文档(PEP465),我们可以得到如下解释: 
For numpy.ndarray objects, * performs elementwise multiplication, and matrix multiplication must use a function call (numpy.dot). For numpy.matrix objects, * performs matrix multiplication, and elementwise multiplication requires function syntax. 
也就是说,当变量类型为 numpy.ndarray 时,∗表示的是Hadamard product;当变量类型为 numpy.matrix 时,∗表示的是matrix product。而LSTM源码中变量类型为 numpy.ndarray ,所以使用∗操作自然是表示Hadamard product,问题解决。 
下面,举个简单例子对这两种操作进行区分:

## Hadamard product
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a * b
Out[5]:
array([[ 5, 12],
[21, 32]]) ## matrix product
c = np.matrix(a)
d = np.matrix(b)
c * d
Out[8]:
matrix([[19, 22],
[43, 50]])

  

 

可以看到,相同的∗操作得到的结果是不同的,原因就是变量类型不同。 
当然,变量类型为numpy.array的变量也可以通过numpy.dot()函数实现矩阵乘法的操作:

np.dot(a,b)
Out[9]:
array([[19, 22],
[43, 50]])

  

参考:https://blog.csdn.net/flying_sfeng/article/details/79330336