花书读书笔记(一)-线性代数(奇异值分解、伪逆、PCA)

全部笔记的汇总贴:《深度学习》花书-读书笔记汇总贴

《深度学习》PDF免费下载:《深度学习》

一、标量、向量、矩阵和张量

二、矩阵和向量相乘

三、单位矩阵和逆矩阵

四、线性相关和生成子空间

五、范数

六、特殊类型的矩阵和向量

七、特征分解

以上相关概念就直接看课本了。

八、奇异值分解

奇异值分解是将矩阵 A A A分解成三个矩阵的乘积:
A = U D V T A=UDV^T A=UDVT
假设 A A A是一个 m ∗ n m*n mn的矩阵,那么 U U U是一个 m ∗ m m*m mm的矩阵, D D D是一个 m ∗ n m*n mn的矩阵, V V V是一个 n ∗ n n*n nn的矩阵。

矩阵 U U U V V V都被定义为正交矩阵,而矩阵 D D D被定义为对角矩阵。注意,矩阵 D D D不一定是方阵。

对角矩阵 D D D对角线上的元素被称为矩阵 A A A的奇异值(singular value)。矩阵 U U U的列向量被称为左奇异向量(left singular vector),矩阵 V V V的列向量被称右奇异向量(right singular vector)。

A A A的左奇异向量(left singular vector)是 A A ⊤ AA^⊤ AA的特征向量。 A A A的右奇异向量(right singular vector)是 A ⊤ A A^⊤A AA的特征向量。 A A A的非零奇异值是 A ⊤ A A^⊤A AA特征值的平方根,同时也是 A A ⊤ AA^⊤ AA特征值的平方根。

下面给出一个计算实例:

我们把矩阵 A A A定义为: A = ( 0 1 1 1 1 0 ) A=\begin{pmatrix} 0 & 1 \\ 1 & 1 \\ 1 & 0 \end{pmatrix} A=011110
我们首先求出 A T A A^TA ATA A A T AA^T AAT
A T A = ( 0 1 1 1 1 0 ) ⋅ ( 0 1 1 1 1 0 ) = ( 2 1 1 2 ) A^TA=\begin{pmatrix} 0 & 1 & 1 \\ 1 & 1 & 0 \end{pmatrix}\cdot \begin{pmatrix} 0 & 1 \\ 1 & 1 \\ 1 & 0 \end{pmatrix} =\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} ATA=(011110)011110=(2112)
A A T = ( 0 1 1 1 1 0 ) ⋅ ( 0 1 1 1 1 0 ) = ( 1 1 0 1 2 1 0 1 1 ) AA^T=\begin{pmatrix} 0 & 1 \\ 1 & 1 \\ 1 & 0 \end{pmatrix}\cdot\begin{pmatrix} 0 & 1 & 1 \\ 1 & 1 & 0 \end{pmatrix}=\begin{pmatrix} 1 & 1 & 0 \\ 1 & 2 & 1 \\ 0 & 1 & 1 \end{pmatrix} AAT=011110(011110)=110121011
进而求出 A T A A^TA ATA的特征值和特征向量:
λ 1 = 3 ; ν 1 = ( 1 2 1 2 ) ; λ 2 = 1 ; ν 2 = ( − 1 2 1 2 ) \lambda_1=3;\nu _1=\begin{pmatrix} \frac1{\sqrt{2}} \\ \frac1{\sqrt{2}} \end{pmatrix} ;\lambda_2=1;\nu _2=\begin{pmatrix} -\frac1{\sqrt{2}} \\ \frac1{\sqrt{2}} \end{pmatrix} λ1=3;ν1=(2 12 1);λ2=1;ν2=(2 12 1)
接着求 A A T AA^T AAT的特征值和特征向量:
λ 1 = 3 ; υ 1 = ( 1 6 2 6 1 6 ) ; λ 2 = 1 ; υ 2 = ( 1 2 0 − 1 2 ) ; λ 3 = 0 ; υ 3 = ( 1 3 − 1 3 1 3 ) \lambda_1=3;\upsilon _1=\begin{pmatrix} \frac1{\sqrt{6}} \\ \frac2{\sqrt{6}} \\\frac1{\sqrt{6}} \end{pmatrix} ;\lambda_2=1;\upsilon _2=\begin{pmatrix} \frac1{\sqrt{2}} \\ 0\\-\frac1{\sqrt{2}} \end{pmatrix} ;\lambda_3=0;\upsilon _3=\begin{pmatrix} \frac1{\sqrt{3}} \\ -\frac1{\sqrt{3}} \\\frac1{\sqrt{3}} \end{pmatrix} λ1=3;υ1=6 16 26 1;λ2=1;υ2=2 102 1;λ3=0;υ3=3 13 13 1
利用 A ν i = σ i υ i , i = 1 , 2 A\nu_i =\sigma_i\upsilon_i,i=1,2 Aνi=σiυi,i=1,2求奇异值:
( 0 1 1 1 1 0 ) ⋅ ( 1 2 1 2 ) = σ 1 ( 1 6 2 6 1 6 ) ⇒ σ 1 = 3 \begin{pmatrix} 0 & 1 \\ 1 & 1 \\ 1 & 0 \end{pmatrix}\cdot\begin{pmatrix} \frac1{\sqrt{2}} \\ \frac1{\sqrt{2}} \end{pmatrix}=\sigma_1\begin{pmatrix} \frac1{\sqrt{6}} \\ \frac2{\sqrt{6}} \\\frac1{\sqrt{6}} \end{pmatrix}\Rightarrow \sigma_1=\sqrt{3} 011110(2 12 1)=σ16 16 26 1σ1=3
( 0 1 1 1 1 0 ) ⋅ ( − 1 2 1 2 ) = σ 2 ( 1 2 0 − 1 2 ) ⇒ σ 2 = 1 \begin{pmatrix} 0 & 1 \\ 1 & 1 \\ 1 & 0 \end{pmatrix}\cdot\begin{pmatrix} -\frac1{\sqrt{2}} \\ \frac1{\sqrt{2}} \end{pmatrix}=\sigma_2\begin{pmatrix} \frac1{\sqrt{2}} \\ 0\\-\frac1{\sqrt{2}} \end{pmatrix}\Rightarrow \sigma_2=1 011110(2 12 1)=σ22 102 1σ2=1
当然,我们也可以用 σ i = λ i \sigma_i=\sqrt{\lambda_i} σi=λi 直接求出奇异值为 3 \sqrt{3} 3 1 1 1
最终,可以得到 A A A的奇异值分解为:
A = U D V T = ( 1 6 1 2 1 3 2 6 0 − 1 3 1 6 − 1 2 1 3 ) ( 3 0 0 1 0 0 ) ( 1 2 1 2 − 1 2 1 2 ) A=UDV^T=\begin{pmatrix} \frac1{\sqrt{6}} & \frac1{\sqrt{2}} & \frac1{\sqrt{3}} \\ \frac2{\sqrt{6}}& 0 & -\frac1{\sqrt{3}}\\ \frac1{\sqrt{6}} & -\frac1{\sqrt{2}} & \frac1{\sqrt{3}} \end{pmatrix}\begin{pmatrix} \sqrt{3} & 0 \\ 0 & 1 \\ 0 & 0 \end{pmatrix}\begin{pmatrix} \frac1{\sqrt{2}} & \frac1{\sqrt{2}} \\ -\frac1{\sqrt{2}} &\frac1{\sqrt{2}} \end{pmatrix} A=UDVT=6 16 26 12 102 13 13 13 13 00010(2 12 12 12 1)

九、Moore-Penrose 伪逆

对于非方矩阵而言,其逆矩阵没有定义。假设在下面的问题中,我们希望通过矩阵 A A A的左逆 B B B来求解线性方程, A x = y Ax=y Ax=y
等式两边左乘左逆 B B B后,我们得到 x = B y x=By x=By取决于问题的形式,我们可能无法设计一个唯一的映射将 A A A映射到 B B B
如果矩阵 A A A的行数大于列数,那么上述方程可能没有解。如果矩阵 A A A的行数小于列数,那么上述矩阵可能有多个解。

Moore-Penrose 伪逆(Moore-Penrose pseudoinverse)使我们在这类问题上取得了一定的进展。矩阵 A A A的伪逆定义为: A + = lim ⁡ a → 0 ( A T A + α I ) − 1 A T A^+=\lim_{a\rightarrow 0}(A^TA+\alpha I)^{-1}A^T A+=a0lim(ATA+αI)1AT计算伪逆的实际算法没有基于这个定义,而是使用下面的公式: A + = V D + U T A^+=VD^+U^T A+=VD+UT

其中,矩阵 U U U D D D V V V是矩阵 A A A奇异值分解后得到的矩阵。对角矩阵 D D D的伪逆 D + D^+ D+是其非零元素取倒数之后再转置得到的。

所以对于八中的例子,可以得到 A A A的伪逆为:
A + = V D + U T = ( 1 2 − 1 2 1 2 1 2 ) ( 1 3 0 0 0 1 0 ) ( 1 6 2 6 1 6 1 2 0 − 1 2 1 3 − 1 3 1 3 ) A^+=VD^+U^T=\begin{pmatrix} \frac1{\sqrt{2}} & -\frac1{\sqrt{2}} \\ \frac1{\sqrt{2}} &\frac1{\sqrt{2}} \end{pmatrix}\begin{pmatrix} \frac1{\sqrt{3}} & 0 & 0 \\ 0 & 1& 0 \end{pmatrix}\begin{pmatrix} \frac1{\sqrt{6}} & \frac2{\sqrt{6}} &\frac1{\sqrt{6}} \\ \frac1{\sqrt{2}}& 0 & -\frac1{\sqrt{2}}\\ \frac1{\sqrt{3}}& -\frac1{\sqrt{3}} & \frac1{\sqrt{3}} \end{pmatrix} A+=VD+UT=(2 12 12 12 1)(3 100100)6 12 13 16 203 16 12 13 1

十、迹运算

迹运算返回的是矩阵对角元素的和,迹运算在转置运算下是不变的,标量在迹运算后仍然是它自己: a = T r ( a ) a = Tr(a) a=Tr(a)

十一、行列式

行列式,记作 d e t ( A ) det(A) det(A),是一个将方阵 A A A映射到实数的函数。行列式等于矩阵特征值的乘积。行列式的绝对值可以用来衡量矩阵参与矩阵乘法后空间扩大或者缩小了多少。

如果行列式是 0 0 0,那么空间至少沿着某一维完全收缩了,使其失去了所有的体积。如果行列式是 1 1 1,那么这个转换保持空间体积不变。

十二、主成分分析(PCA)

主成分分析(principal components analysis, PCA)是一个简单的机器学习算法,可以通过基础的线性代数知识推导。也就是寻找一个低维平面,使得各个数据点到平面的投影距离最小,换句话说,就是寻找 k k k个向量,作为子空间,将数据映射到这个子空间上,则数据的维度转换为 k k k

PCA的流程:

  1. 数据预处理(均值归一化)
  2. 计算低维空间向量(计算协方差 → \rightarrow 奇异值分解 → \rightarrow 计算低维矩阵 → \rightarrow 转换为低维数据)

数据预处理主要是进行均值归一化,对第 j j j个特征值进行如下变化:
μ j = 1 m ∑ i = 1 m x j ( i ) x j ( i ) = x j ( i ) − μ j s j \mu_j=\frac1m\sum_{i=1}^mx_j^{(i)}\\x_j^{(i)}=\frac{x_j^{(i)}-\mu_j}{s_j} μj=m1i=1mxj(i)xj(i)=sjxj(i)μj
均值归一化可使得特征的均值为 0 0 0,其中 s j s_{j} sj为特征缩放(取值范围的最大值减去最小值,使之取值范围接近 [ − 1 , 1 ] [-1,1] [1,1])。
计算低维空间向量,首先计算数据的协方差矩阵、进行奇异值分解。
   \;
我们已知主成分分析是要寻找一个低维平面,使得各个数据点到这个平面的距离最小,这个距离可采用平均投影误差的平方(average squared projection error)量化。

用主成分分析PCA防止过拟合是不适合的,过拟合应该采用正则化。

下一章传送门:花书读书笔记(二)-概率与信息论

猜你喜欢

转载自blog.csdn.net/qq_41485273/article/details/112686375