線性代數基礎知識的復習
機器學習需要一些線性代數的基礎知識,
matrix:矩陣
\[A= \begin{bmatrix} 1402 & 191\\ 1371 & 821\\ 949 & 1437\\ 147&1448\\ \end{bmatrix} \]
\[B= \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{bmatrix} \]
- A是一個\(4\times2\)的矩陣,由4行2列組成,并且由兩個中括號括起來,記作\(R^{4\times2}\).
- B是一個\(2\times3\)的矩陣,由2行3列組成,并且由兩個中括號括起來,記作\(R^{2\times3}\).
- \(A_{ij}\)用來表示矩陣中的某一個元素,其中\(i\)代表矩陣的行,\(j\)代表矩陣的列
- \(A_{11}=1402\)
- \(A_{12}=191\)
- \(A_{132}=1437\)
- \(A_{41}=147\)
- \(A_{43}=undefined\)
vector:向量
\[y= \begin{bmatrix} 460\\ 232\\ 315\\ 178\\ \end{bmatrix} \]
-
\(y\)是一組向量,可以把向量看作是一個\({n\times1}\)的矩陣,此處n=4,所以記作\(R^{4}\),
-
\(y_i\)是向量中的第\(i^{th}\)個元素
- \(y_1=460\)
- \(y_2=232\)
- \(y_3=315\)
-
學習過高級語言的朋友一定知道,例如c++中的STL標準庫中vector的index是從0開始算的,而在人們實際生活學習中,大部分人習慣從1開始,因此,在學習機器學習中,我們一般用1作為起始,而在撰寫程式實作的時候,則切換回0,
-
附上一段MATLAB的程式
% The ; denotes we are going back to a new row. A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12] % Initialize a vector v = [1;2;3] % Get the dimension of the matrix A where m = rows and n = columns [m,n] = size(A) % You could also store it this way dim_A = size(A) % Get the dimension of the vector v dim_v = size(v) % Now let's index into the 2nd row 3rd column of matrix A A_23 = A(2,3)A = 1 2 3 4 5 6 7 8 9 10 11 12 v = 1 2 3 m = 4 n = 3 dim_A = 4 3 dim_v = 3 1 A_23 = 6
矩陣加法
\[\begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} + \begin{bmatrix} 4 & 0.5 \\ 2 & 5 \\ 0 & 1 \\ \end{bmatrix} = \begin{bmatrix} 5 & 0.5 \\ 4 & 10 \\ 3 & 2 \\ \end{bmatrix} \]
-
上面有一個矩陣加法的例子,
-
首先,兩個矩陣維度必須相同,即相同的行數相同的列數,
-
兩個矩陣加法就是將對位置的數字加起來,然后得到一個新的矩陣,且這個矩陣和原來兩個矩陣維度相同,
-
在維度不同的情況下無法進行加法運算,例如:
\[\]
1 & 0 \
2 & 5 \
3 & 1 \
\end{bmatrix}\begin{bmatrix}
4 & 0.5 \
2 & 5 \
\end{bmatrix}\mathop{error}
\[ \]
\[3\times \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} = \begin{bmatrix} 3 & 0 \\ 6 & 15 \\ 9 & 3 \\ \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 2 & 5 \\ 3 & 1 \\ \end{bmatrix} \times3 \]
-
上面有一個矩陣乘法的例子,注意是實數乘矩陣,
-
結果是直接將矩陣的各個元素與實數相乘,得到一個新的矩陣,維數一定相同
-
對于實數乘矩陣來說,是先乘還是后乘不影響結果
-
除法類似于乘法:
\[\]
4 & 0 \
6 & 3 \
\end{bmatrix}
\setminus
4\frac{1}{4}
\times
\begin{bmatrix}
3 & 0 \
6 & 15 \
\end{bmatrix}\begin{bmatrix}
1 & 0 \
\frac{3}{2} & \frac{3}{4} \
\end{bmatrix}
\times3\[ \]
\[\begin{eqnarray} & & 3 \times \begin{bmatrix} 1 \\ 4 \\ 2 \\ \end{bmatrix} + \begin{bmatrix} 0\\ 0 \\ 5 \\ \end{bmatrix} - \begin{bmatrix} 3 \\ 0 \\ 2 \\ \end{bmatrix} \setminus 3 \\ & = & \begin{bmatrix} 3 \\ 12 \\ 6 \\ \end{bmatrix} + \begin{bmatrix} 0 \\ 0 \\ 5 \\ \end{bmatrix} - \begin{bmatrix} 1 \\ 0 \\ \frac{2}{3} \\ \end{bmatrix}\\ & = & \begin{bmatrix} 2 \\ 12 \\ \frac{31}{3} \\ \end{bmatrix}\\ \end{eqnarray} \]
-
MATLAB代碼:
% Initialize matrix A and B A = [1, 2, 4; 5, 3, 2] B = [1, 3, 4; 1, 1, 1] % Initialize constant s s = 2 % See how element-wise addition works add_AB = A + B % See how element-wise subtraction works sub_AB = A - B % See how scalar multiplication works mult_As = A * s % Divide A by s div_As = A / s % What happens if we have a Matrix + scalar? add_As = A + sA = 1 2 4 5 3 2 B = 1 3 4 1 1 1 s = 2 add_AB = 2 5 8 6 4 3 sub_AB = 0 -1 0 4 2 1 mult_As = 2 4 8 10 6 4 div_As = 0.5000 1.0000 2.0000 2.5000 1.5000 1.0000 add_As = 3 4 6 7 5 4
矩陣與向量相乘
\[\begin{bmatrix} 1 & 3 \\ 4 & 0 \\ 2 & 1 \\\end{bmatrix} \times\begin{bmatrix} 1 \\ 5 \\\end{bmatrix} =\begin{bmatrix} 16^{(1)} \\ 4^{(2)} \\ 7^{(3)} \\\end{bmatrix} \\\begin{eqnarray} 1 \times 1 + 3 \times 5 = 16 \tag{1}\\ 4 \times 1 + 0 \times 5 = 4 \tag{2}\\ 2 \times 1 + 1 \times 5 = 7 \tag{3}\\\end{eqnarray} \]
-
上面有一個特殊例子,展示了矩陣與向量相乘的等式和程序
-
相乘的條件:
- 設矩陣為\(A\),向量為\(B\),
- \(A_j=B_i\)(A的列數等于B的行數)
-
將A的一行和B的一列的每個元素相乘,并相加得到一個數值,
-
新的得到的矩陣的行數與矩陣相同,列數與向量相同,
可以參考一下下面這個例子:
\[\]
\[ \]
% Initialize matrix A A = [1, 2, 3; 4, 5, 6;7, 8, 9] % Initialize vector v v = [1; 1; 1] % Multiply A * v Av = A * vA = 1 2 3 4 5 6 7 8 9 v = 1 1 1 Av = 6 15 24
矩陣與矩陣相乘
我們現在開始計算這樣一個算式
\[\begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 & 3 \\ 0 & 1 \\ 5 & 2 \\\end {bmatrix} \]
用剛剛學過的矩陣乘向量,將第二個矩陣拆成兩個向量
\[\begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 \\ 0 \\ 5 \\\end {bmatrix} =\begin {bmatrix} 11 \\ 9 \\\end {bmatrix} \]
\[\begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 3 \\ 1 \\ 2 \\\end {bmatrix} =\begin {bmatrix} 10 \\ 14 \\\end {bmatrix} \]
其實我們已經計算完成了,只差最后一步,按原來列的順序將答案合并,可以得到
\[\begin {bmatrix} 1 & 3 & 2 \\ 4 & 0 & 1 \\\end {bmatrix} *\begin {bmatrix} 1 & 3 \\ 0 & 1 \\ 5 & 2 \\\end {bmatrix} =\begin {bmatrix} 11 & 10 \\ 9 & 14 \\\end {bmatrix} \]
-
相乘的條件:
- 設矩陣1為\(A\),矩陣2為\(B\),
- \(A_j=B_i\)(A的列數等于B的行數)
-
將A的一行和B的一列的每個元素相乘,并相加得到一個數值,
-
新的得到的矩陣的行數與A相同,列數與B相同,即\(R^{m*n} \times R^{n*o} = R^{m*o}\)
可以參考一下下面這個例子:
\[\]
a & b \\ c & d \\ e & f \\\end {bmatrix}
*
\begin {bmatrix}
w & x \
y & z \
\end {bmatrix}
=
\begin {bmatrix}
aw + by & ax + bz\
cw + dy & cx + dz\
ew + fy & ex + fz\
\end {bmatrix}\[ \]
% Initialize a 3 by 2 matrix A = [1, 2; 3, 4;5, 6] % Initialize a 2 by 1 matrix B = [1; 2] % We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1) mult_AB = A*B % Make sure you understand why we got that resultA = 1 2 3 4 5 6 B = 1 2 mult_AB = 5 11 17
矩陣乘法的一些性質
-
不可交換(in general)
在實數乘法中,兩個數交換之后結果相同是一個常識:
\[\]
\[\]
我們用上面的矩陣乘法嘗試一下:
\[\]
1 & 1 \\ 0 & 0 \\\end {bmatrix}
*
\begin {bmatrix}
0 & 0 \
2 & 0 \
\end {bmatrix}
=
\begin {bmatrix}
2 & 0 \
0 & 0 \
\end {bmatrix}\[ \]
\begin {bmatrix}
0 & 0 \
2 & 0 \
\end {bmatrix}
*
\begin {bmatrix}
1 & 1 \
0 & 0 \
\end {bmatrix}
=
\begin {bmatrix}
0 & 0 \
2 & 2 \
\end {bmatrix}\[ \]
但是這是一般情況,有一種情況,是可以交換的,
-
可交換的特殊情況(Identity matrix)
有一種矩陣我們叫做單位矩陣(Identity matrix),其特點是:
-
矩陣一定是\(n \times n\)的,記作$I \space or \space I_{n \times n} $
-
矩陣對角線一定是1,其他部分一定是0
\[\]
\begin {bmatrix} 1 & 0 \\ 0 & 1 \\ \end {bmatrix}}\limits_{2 \times 2}
\space \space \space \space \space \space \space \space \space \space
\mathop{
\begin {bmatrix}
1 & 0 & 0 \
0 & 1 & 0 \
0 & 0 & 1 \
\end {bmatrix}
}\limits_{3 \times 3}
\space \space \space \space \space \space \space \space \space \space
\mathop{
\begin {bmatrix}
1 & 0 & 0 & 0 \
0 & 1 & 0 & 0 \
0 & 0 & 1 & 0 \
0 & 0 & 0 & 1 \
\end {bmatrix}
}\limits_{4 \times 4}
\space \space \space \space \space \space \space \space \space \space
\mathop{
\begin {bmatrix}
1 & & & & & \
& 1 & & & & \
& & 1 & & & \
& & & 1 & & \
& & & & \ddots & \
& & & & & 1 \
\end {bmatrix}
}\limits_{n \times n}\[ \]
-
MATLAB代碼:
% Initialize random matrices A and B A = [1,2;4,5] B = [1,1;0,2] % Initialize a 2 by 2 identity matrix I = eye(2) % The above notation is the same as I = [1,0;0,1] % What happens when we multiply I*A ? IA = I*A % How about A*I ? AI = A*I % Compute A*B AB = A*B % Is it equal to B*A? BA = B*A % Note that IA = AI but AB != BAA = 1 2 4 5 B = 1 1 0 2 I = Diagonal Matrix 1 0 0 1 IA = 1 2 4 5 AI = 1 2 4 5 AB = 1 5 4 14 BA = 5 7 8 10
-
矩陣的倒數(逆矩陣)
倒數的概念很熟悉吧,一個數和另一個數相乘等與1我們就認為這對數字互為倒數,
\[3 \times (3^{-1}) = 1 \\ 5 \times (5^{-1}) = 1 \\ \]
對于矩陣,我們也有同樣的概念,由于我們認為單位矩陣和實數中1的地位相同,因此它是這樣表述的:
\[A(A^{-1})=(A^{-1})A=I \]
我們稱\(A^{-1}\)為逆矩陣,
\[\mathop{ \begin {bmatrix} 3 & 4 \\ 2 & 16 \\ \end {bmatrix} }\limits_A \mathop{ \begin {bmatrix} 0.4 & -0.1 \\ -0.05 & 0.075 \\ \end {bmatrix} }\limits_{A^{-1}} = \mathop{ \begin {bmatrix} 1 & 0 \\ 0 & 1 \\ \end {bmatrix} }\limits_{AA^{-1}} = I_{2 \times 2} \]
一些要注意的點:
- 存在逆矩陣的矩陣一定是方陣
- \(\begin {bmatrix} 0 & 0 \\ 0 & 0 \\ \end {bmatrix}\)像這樣的0矩陣是沒有的逆矩陣的,因為無論如何都無法讓它變成單位矩陣,你可以將沒有逆矩陣的方陣近似成零矩陣看,
- 沒有逆矩陣的矩陣我們稱之為奇異矩陣或者是退化矩陣
矩陣的倒置
我們現在有一個矩陣:
\[A= \begin {bmatrix} 1 & 2 & 0 \\ 3 & 5 & 9 \\ \end {bmatrix} \]
而它的倒置矩陣就是:
\[A^T = \begin {bmatrix} 1 & 3 \\ 2 & 5 \\ 0 & 9 \\ \end {bmatrix} \]
-
這個操作可以看成是,把A的每一個行向量改成值相同的列向量,再按順序拼接起來,
-
\(A\)經過轉置之后,\(A\)和\(A^T\)中每個元素的對應關系是
\[\]
\[ \]
% Initialize matrix A A = [1,2,0;0,5,6;7,0,9] % Transpose A A_trans = A' % Take the inverse of A A_inv = inv(A) % What is A^(-1)*A? A_invA = inv(A)*AA = 1 2 0 0 5 6 7 0 9 A_trans = 1 0 7 2 5 0 0 6 9 A_inv = 0.348837 -0.139535 0.093023 0.325581 0.069767 -0.046512 -0.271318 0.108527 0.038760 A_invA = 1.00000 -0.00000 0.00000 0.00000 1.00000 -0.00000 -0.00000 0.00000 1.00000
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/43582.html
標籤:其他
下一篇:自然語言處理中預訓練模型一覽
