目录
- numpy中的乘法
- tensorflow中的乘法
- matlab下的乘法
- 总结
矩阵乘法:
matlab=>∗*∗
numpy=> np.dot(a,b)np.dot(a,b)np.dot(a,b)
tensorflow=> tf.matmultf.matmultf.matmul
矩阵点乘:
matlab=> .∗.*.∗
numpy=> ∗*∗ 和 np.multiplynp.multiplynp.multiply
tensorflow=>∗*∗ 和 tf.multiplytf.multiplytf.multiply
1、numpy中的乘法
>>> a = np.array([[1,2,3,4],[2,3,4,5],[5,6,7,3],[2,3,1,5],[3,6,8,5]])
>>> a
array([[1, 2, 3, 4],
[2, 3, 4, 5],
[5, 6, 7, 3],
[2, 3, 1, 5],
[3, 6, 8, 5]])
>>> b = np.array([6,5,6,2]).T
>>> b
array([6, 5, 6, 2]) #shape为(4,)
>>> np.dot(a,b)
#要求第一个数组的列数等于第二个数组的行数,数学中的矩阵相乘,最后得到的数组是(第一个数组的行数,第二个数组的列数)
array([ 42, 61, 108, 43, 106]) #shape为(5,)
>>> bt = np.array([[6],[5],[6],[2]])
>>> bt
array([[6],
[5],
[6],
[2]])
>>> bt.shape
(4, 1)
>>> ndot = np.dot(a,bt)
>>> ndot
array([[ 42],
[ 61],
[108],
[ 43],
[106]])
>>> a*b #矩阵的对应元素相乘(点乘),第二个数组b使用了broadcast机制
array([[ 6, 10, 18, 8],
[12, 15, 24, 10],
[30, 30, 42, 6],
[12, 15, 6, 10],
[18, 30, 48, 10]])
>>> np.multiply(a,b)
array([[ 6, 10, 18, 8],
[12, 15, 24, 10],
[30, 30, 42, 6],
[12, 15, 6, 10],
[18, 30, 48, 10]])
>>> nmul = np.multiply(a,bt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (5,4) (4,1)
2、tensorflow中的乘法
>>> tfmul = tf.multiply(a,b)
>>> tfmul
<tf.Tensor 'Mul:0' shape=(5, 4) dtype=int64>
>>> sess = tf.InteractiveSession()
>>> tfmul.eval()
array([[ 6, 10, 18, 8],
[12, 15, 24, 10],
[30, 30, 42, 6],
[12, 15, 6, 10],
[18, 30, 48, 10]])
>>> a = tf.constant([[1,2,3,4],[2,3,4,5],[5,6,7,3],[2,3,1,5],[3,6,8,5]],dtype=tf.float32)
>>> b = tf.constant([6,5,6,2],dtype=tf.float32)
>>> sess.run(a*b)
array([[ 6., 10., 18., 8.],
[12., 15., 24., 10.],
[30., 30., 42., 6.],
[12., 15., 6., 10.],
[18., 30., 48., 10.]], dtype=float32)
>>> sess.run(tf.matmul(a,b))
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 686, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "/home/ubuntu/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 2 but is rank 1 for 'MatMul_2' (op: 'MatMul') with input shapes: [5,4], [4].
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul_2' (op: 'MatMul') with input shapes: [5,4], [4].
>>> sess.run(tf.multiply(a,b))
array([[ 6., 10., 18., 8.],
[12., 15., 24., 10.],
[30., 30., 42., 6.],
[12., 15., 6., 10.],
[18., 30., 48., 10.]], dtype=float32)
>>> bt = tf.constant([[6],[5],[6],[2]],dtype=tf.float32)
>>> sess.run(a*bt) / sess.run(tf.multiply(a,bt))
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 686, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "/home/ubuntu/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 5 and 4 for 'mul_1' (op: 'Mul') with input shapes: [5,4], [4,1].
ValueError: Dimensions must be equal, but are 5 and 4 for 'mul_1' (op: 'Mul') with input shapes: [5,4], [4,1].
>>> sess.run(tf.matmul(a,bt))
array([[ 42.],
[ 61.],
[108.],
[ 43.],
[106.]], dtype=float32)
3、matlab下的乘法
>> a = [1,2,3,4;2,3,4,5;5,6,7,3;2,3,1,5;3,6,8,5]
>> a = [1 2 3 4;2 3 4 5;5 6 7 3;2 3 1 5;3 6 8 5]
#matlab中空格和逗号能分隔输入变量和数组行元素
#分号用于分隔数组的列
a =
1 2 3 4
2 3 4 5
5 6 7 3
2 3 1 5
3 6 8 5
>> b = [6,5,6,2]
b =
6 5 6 2
>> a*b
错误使用 *
内部矩阵维度必须一致。
>> a.*b
#矩阵的点乘,要求两矩阵维数相等,矩阵对应位置元素相乘输出
ans =
6 10 18 8
12 15 24 10
30 30 42 6
12 15 6 10
18 30 48 10
>> bt = b'
bt =
6
5
6
2
>> a*bt
#矩阵乘法,a的列数必须等于bt的行数,线代里面矩阵的乘法相乘相加规则
ans =
42
61
108
43
106
>> a.*bt
矩阵维度必须一致。
4、总结
A=(a11a12a21a22)A = \begin{matrix}
\left(
\begin {array}{ccc}
a11 & a12 \\
a21 & a22 \\
\end{array}
\right)
\end{matrix}A=(a11a21a12a22)
B=(b11b12b21b22)B = \begin{matrix}
\left(
\begin {array}{ccc}
b11 & b12 \\
b21 & b22 \\
\end{array}
\right)
\end{matrix}B=(b11b21b12b22)
矩阵乘法:
(好像和向量的点积,又称为数量积和内积有点类似)
要求:前一个矩阵MN的列数必须和后一个矩阵NZ的行数相一致
过程:
1、将矩阵a的第一行乘以矩阵b的第一列,各个元素对应相乘然后求和得到结果矩阵的第一行第一个元素的值;
2、将矩阵a的第一行乘以矩阵b的第二列,各个元素对应相乘然后求和得到结果矩阵的第一行第二个元素的值,
以此类推…
结果:得到的结果矩阵维数为M*Z
matlab=>∗*∗
numpy=> np.dot(a,b)np.dot(a,b)np.dot(a,b)
tensorflow=> tf.matmultf.matmultf.matmul
A∗B=(a11∗b11+a12∗b21a11∗b12+a12∗b22a21∗b11+a22∗b21a21∗b12+a22∗b22) A*B = \begin{matrix}
\left(
\begin{array}{ccc}
a11*b11+a12*b21 & a11*b12+a12*b22 \\
a21*b11+a22*b21 & a21*b12+a22*b22\\
\end{array} \right)\end{matrix}A∗B=(a11∗b11+a12∗b21a21∗b11+a22∗b21a11∗b12+a12∗b22a21∗b12+a22∗b22)
矩阵点乘:
要求:两矩阵必须相同大小,或者某一个矩阵可以通过广播机制扩张成相同大小
过程:两个矩阵对应位置上的元素相乘,得到结果矩阵对应位置上元素的值
结果:和输入矩阵的大小一致
备注:向量的点乘又叫向量的内积或数量积
matlab=> .∗.*.∗
numpy=> ∗*∗ 和 np.multiplynp.multiplynp.multiply
tensorflow=>∗*∗ 和 tf.multiplytf.multiplytf.multiply
A.∗B=(a11∗b11a12∗b12a21∗b21a22∗b22) A.*B = \begin{matrix}
\left(
\begin{array}{ccc}
a11*b11 & a12*b12 \\
a21*b21 & a22*b22\\
\end{array} \right)\end{matrix}A.∗B=(a11∗b11a21∗b21a12∗b12a22∗b22)
大概就是这样了,有什么不对的地方请指正,谢谢~