矩阵的运算
学会矩阵的运算对于学习计算机的课程十分重要。
矩阵在人工神经网络中扮演着重要的角色。一张图片在计算机中用矩阵表示,通过对矩阵的运算来实现各种图像的操作。神经网络的权重和偏置通常表示为矩阵,通过矩阵乘法和激活函数的组合,可以实现神经网络的前向传播和反向传播。
矩阵运算包括加法运算,减法运算,乘法运算,倒置,逆矩阵
下文所使用矩阵例子
A
=
[
a
11
a
12
.
.
.
a
1
m
a
21
a
22
.
.
.
a
2
m
.
.
.
.
.
.
.
.
.
.
.
.
a
n
1
a
n
2
.
.
.
a
n
m
]
A=\begin{bmatrix} a_{11}&a_{12} &...&a_{1m} \\ a_{21}&a_{22} &...&a_{2m} \\ ...& ... & ... &... \\ a_{n1}&a_{n2} &...&a_{nm} \end{bmatrix}
A=
a11a21...an1a12a22...an2............a1ma2m...anm
1. 加法运算
与普通的代数加法类似。行和列数相同的矩阵相加,把A,B矩阵内的对应元素的值相加,作为结果矩阵的值。
A
+
B
=
[
a
11
+
b
11
a
12
+
b
12
.
.
.
a
1
m
+
b
1
m
a
21
+
b
21
a
22
+
b
22
.
.
.
a
2
m
+
b
2
m
.
.
.
.
.
.
.
.
.
.
.
.
a
n
1
+
b
n
1
a
n
2
+
b
n
2
.
.
.
a
n
m
+
b
n
m
]
A+B=\begin{bmatrix} a_{11}+b_{11} &a_{12}+b_{12} &... &a_{1m}+b_{1m} \\ a_{21}+b_{21} &a_{22}+b_{22} &... &a_{2m}+b_{2m}\\ ... &... &... &... \\ a_{n1}+b_{n1} &a_{n2}+b_{n2} &... &a_{nm}+b_{nm} \end{bmatrix}
A+B=
a11+b11a21+b21...an1+bn1a12+b12a22+b22...an2+bn2............a1m+b1ma2m+b2m...anm+bnm
2. 减法运算
与矩阵加法类似,需要行列数相同。
A
−
B
=
[
a
11
−
b
11
a
12
−
b
12
.
.
.
a
1
m
−
b
1
m
a
21
−
b
21
a
22
−
b
22
.
.
.
a
2
m
−
b
2
m
.
.
.
.
.
.
.
.
.
.
.
.
a
n
1
−
b
n
1
a
n
2
−
b
n
2
.
.
.
a
n
m
−
b
n
m
]
A-B=\begin{bmatrix} a_{11}-b_{11} &a_{12}-b_{12} &... &a_{1m}-b_{1m} \\ a_{21}-b_{21} &a_{22}-b_{22} &... &a_{2m}-b_{2m}\\ ... &... &... &... \\ a_{n1}-b_{n1} &a_{n2}-b_{n2} &... &a_{nm}-b_{nm} \end{bmatrix}
A−B=
a11−b11a21−b21...an1−bn1a12−b12a22−b22...an2−bn2............a1m−b1ma2m−b2m...anm−bnm
3.乘法运算
分为一个常数与矩阵相乘的情况,和两个矩阵之间相乘的情况
常数与矩阵相乘
k
⋅
A
=
[
k
⋅
a
11
k
⋅
a
12
.
.
.
k
⋅
a
1
m
k
⋅
a
21
k
⋅
a
22
.
.
.
k
⋅
a
2
m
.
.
.
.
.
.
.
.
.
.
.
.
k
⋅
a
n
1
k
⋅
a
n
2
.
.
.
k
⋅
a
n
m
]
k\cdot A=\begin{bmatrix} k\cdot a_{11}&k\cdot a_{12} &...&k\cdot a_{1m} \\ k\cdot a_{21}&k\cdot a_{22} &...&k\cdot a_{2m} \\ ...& ... & ... &... \\ k\cdot a_{n1}&k\cdot a_{n2} &...&k\cdot a_{nm} \end{bmatrix}
k⋅A=
k⋅a11k⋅a21...k⋅an1k⋅a12k⋅a22...k⋅an2............k⋅a1mk⋅a2m...k⋅anm
矩阵与矩阵相乘
给定两个矩阵A*B的运算。
A
=
[
a
11
a
12
a
13
a
21
a
22
a
23
]
A=\begin{bmatrix} a_{11}&a_{12}&a_{13} \\ a_{21}&a_{22}&a_{23}\\ \end{bmatrix}
A=[a11a21a12a22a13a23]
B
=
[
b
11
b
12
b
21
b
22
b
31
b
32
]
B=\begin{bmatrix} b_{11}&b_{12} \\ b_{21}&b_{22}\\ b_{31}&b_{32} \end{bmatrix}
B=
b11b21b31b12b22b32
矩阵A,B需要满足A的列数等于B的行数
A ⋅ B = [ a 11 ⋅ b 11 + a 12 ⋅ b 21 a 11 ⋅ b 12 + a 12 ⋅ b 22 a 21 ⋅ b 11 + a 22 ⋅ b 21 a 21 ⋅ b 12 + a 22 ⋅ b 22 ] A\cdot B=\begin{bmatrix} a_{11}\cdot b_{11}+a_{12}\cdot b_{21} &a_{11}\cdot b_{12}+a_{12}\cdot b_{22} \\ a_{21}\cdot b_{11}+a_{22}\cdot b_{21} &a_{21}\cdot b_{12}+a_{22}\cdot b_{22} \\ \end{bmatrix} A⋅B=[a11⋅b11+a12⋅b21a21⋅b11+a22⋅b21a11⋅b12+a12⋅b22a21⋅b12+a22⋅b22]
A ⋅ B = [ a 11 ⋅ b 11 + a 12 ⋅ b 21 + a 13 ⋅ b 31 a 11 ⋅ b 12 + a 12 ⋅ b 22 + a 13 ⋅ b 32 a 21 ⋅ b 21 + a 22 ⋅ b 21 + a 23 ⋅ b 31 a 21 ⋅ b 22 + a 22 ⋅ b 22 + a 23 ⋅ b 32 ] A\cdot B=\begin{bmatrix} a_{11}\cdot b_{11}+a_{12}\cdot b_{21}+a_{13}\cdot b_{31} & a_{11}\cdot b_{12}+a_{12}\cdot b_{22}+a_{13}\cdot b_{32} \\ a_{21}\cdot b_{21}+a_{22}\cdot b_{21}+a_{23}\cdot b_{31} & a_{21}\cdot b_{22}+a_{22}\cdot b_{22}+a_{23}\cdot b_{32}\\ \end{bmatrix} A⋅B=[a11⋅b11+a12⋅b21+a13⋅b31a21⋅b21+a22⋅b21+a23⋅b31a11⋅b12+a12⋅b22+a13⋅b32a21⋅b22+a22⋅b22+a23⋅b32]
矩阵乘法不满足交换律,但是满足分配率和结合律
4.矩阵的转置
例如一个具体的矩阵A为
A
=
[
a
11
a
12
a
13
a
21
a
22
a
23
]
A=\begin{bmatrix} a_{11}&a_{12}&a_{13} \\ a_{21}&a_{22}&a_{23}\\ \end{bmatrix}
A=[a11a21a12a22a13a23]
则他的转置矩阵为
A
T
=
[
a
11
a
21
a
12
a
22
a
13
a
23
]
A^{T} =\begin{bmatrix} a_{11}&a_{21} \\ a_{12}&a_{22}\\ a_{13}&a_{23} \end{bmatrix}
AT=
a11a12a13a21a22a23
5.矩阵的逆运算
首先需要知道单位矩阵的概念
是对角线上的元素为1,除了对角线上以外的元素都为零。
E
=
[
1
0
.
.
.
0
0
1
.
.
.
0
.
.
.
.
.
.
.
.
.
.
.
.
0
0
.
.
.
1
]
E =\begin{bmatrix} 1&0&...&0 \\ 0&1&...&0 \\ ...&...&...&... \\ 0&0&...&1 \\ \end{bmatrix}
E=
10...001...0............00...1
A
⋅
A
−
1
=
E
A\cdot A^{-1} =E
A⋅A−1=E