Yinghai Yu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 1. TODO # 2. Linear Algebra ## 2.1. Scalars, Vectors, Matrices, and Tensors * Scalars - 是一个number。一般用非粗体小写字母表示,如$\text{s}$ * Vectors - 是一组number的阵列,an array of numbers。也叫向量。一般用粗体小写字母表示,如$\textbf{x}$ * Matrices - 是二维的一组number阵列。也叫矩阵。一般用大写加粗的字母表示,如$\textbf{A}$ * Tensors - 是三维或者更高维的number阵列 ## 2.2.Multiplying Matrices and Vectors ### 2.2.1. 普通矩阵乘法 $\textbf{C=AB}=C_{i,j}=\Sigma_k A_{i,k}B_{k,j}$ 意思就是$\textbf{A}$的行数要等于$\textbf{B}$的列数 ### 2.2.2. 矩阵元素乘法 (element-wise product) 也叫**element-wise product** 或者**Hadamard product**,记做$\textbf{A}\odot \textbf{B}$ ### 2.2.3. 矩阵点乘 (dot product) 两个同纬度向量$\textbf{x}, \textbf{y}$之间的点乘可以写做$\textbf{x}^T\textbf{y}$,我们可以直接理解成矩阵乘法$\textbf{C=AB}$的一种特殊情况。 ## 2.3. Identity and Inverse Matrices 对于线性方程组$\textbf{Ax}=\textbf{b}$,我们可以使用**matrix inversion**轻松获得该方程组的解析解。再次直线需要先介绍 单位矩阵 **identity matrix**, $\textbf{I}$。它的定义是: $\textbf{A}^{-1}\textbf{A}=\textbf{I}_n$,那我们可以如下求解: $$ \textbf{Ax}=\textbf{b} \\ \textbf{A}^{-1}\textbf{Ax}=\textbf{A}^{-1}\textbf{b} \\ \textbf{I}_n\textbf{x}=\textbf{A}^{-1}\textbf{b} \\ \textbf{x}=\textbf{A}^{-1}\textbf{b} $$ 上述过程取决于$\textbf{A}^{-1}$的存在性。当它存在的时候,我们有若干种算法求得它的闭合解(closed form)。理论上说,同一个$\textbf{A}^{-1}$可以多次用来求解不同的$\textbf{b}$值。但是$\textbf{A}^{-1}$只是一个理论上的工具,在很多软件上并不能直接使用$\textbf{A}^{-1}$,因为$\textbf{A}^{-1}$在计算机中位数精度的限制。 ## 2.4. Linear Dependence and Span(线性依赖和线性生成空间) 为了让$\textbf{A}^{-1}$存在,$\textbf{Ax}=\textbf{b} (2.11)$必须对每一个$\textbf{b}$都有且仅有一个解。但实际很多时候是$\textbf{b}$是无解的,或有无穷个解。(有有限个解的情况是不存在的)。如果$\textbf{x}$和$\textbf{y}$都是$\textbf{Ax}=\textbf{b} (2.11)$的解,那么有,对于任意实数$\alpha$,$\textbf{z}=\alpha \textbf{x}+(1-\alpha)\textbf{y}$都是解。 > [name=Yinghai Yu]这里的解释很巧妙 > 如果想知道$\textbf{Ax}=\textbf{b} (2.11)$有多少解,我们可以把$\textbf{A}$的列视作 可以从原点出发的方向(每个列都是一个vector,代表了一个方向),然后找从原点到$\textbf{b}$有多少可行的路径。如此看来,每一个$\textbf{x}$代表的是在某个方向上需要travel的距离,$x_i$代表了在方向$i$(或者column $i$)上要travel的距离。即, $\textbf{Ax}=\Sigma_i x_i\textbf{A}_{:,i} (2.27)$ 上面的(2.27)通常被叫做**linear combination**。更正式的说法是一组向量$\{v^{(1)}, v^{(2)}, ..., v^{(n)} \}$的linear combination乘上一个scalar $c_i$。于是有:$\Sigma_ic_i \textbf{v}^{(i)}$ **span** -> 一组向量的span(生成空间)是指,通过原有向量的线性组合 能得到的所有点的集合。 因此要确定$\textbf{Ax}=\textbf{b} (2.11)$是否有解就等同于检测$\textbf{b}$是否在$\textbf{A}$的columns(向量)的生成空间(**span**)内。这个**span**也叫**column space**或者**range of** $\textbf{A}$。 为了让$\textbf{Ax}=\textbf{b} (2.11)$对于所有的$\textbf{b}\in \mathbb{R}^m$有解,我们需要让$\textbf{A}$**的span**是所有的$\mathbb{R}^m$。如果$\mathbb{R}^m$任意一点被从$\textbf{A}$**的span**剔除,那么被剔除的点 就是$\textbf{b}$中 没有解的点。 > [name=Yinghai Yu]我的理解就是,向量$\textbf{b}$中的某个值,无法从原点按照$\textbf{A}$的列向量的方向抵达。 此外,你会发现$n>=m$是必须要满足的条件;否则column space (span)的维度$n$会小于$m$。 但这还不够,因为矩阵本身可能会有冗余。例如这样的矩阵: $$\textbf{A} = \begin{bmatrix}1 & 2\\ 5 & 10\\ 2 & 4 \end{bmatrix}$$ 这样的矩阵,我们叫它有**linear dependence**,意味着$\textbf{A}$的列和列之间存在着线性关系。一个列可以被另一列通过线性变换来表示。假设$v^1$, $v^2$是$\textbf{A}$的两列,那么添加$v^2$并不能对**column space**添加任何的点。那么$\textbf{A}$就不能表示$\mathbb{R}^2$(二维实数空间)的所有值,而是只能表示$\mathbb{R}^1$(一维实数空间)的所有值。 > [name=Yinghai Yu]我的理解就类似于机器学习中$\textbf{X}_m$的$m$个列之间存在线性关系,或者multicolinearity Formally, 这种冗余性也叫做**linear dependence**. A set of vectors is **lienarly independent** if no vector in the set is a linear combination of the other vectors。 > 这意味着,如果我们想要有一个矩阵,让它的column space包含$m$维空间中所有的实数$\mathbb{R}^m$,那么该矩阵至少要有$m$个linearly independent的列。这是$\textbf{Ax}=\textbf{b} (2.11)$对于每个$\textbf{b}$都有解的充要条件。 所以,为了让矩阵有inverse, $\textbf{A}^{-1}$,我们还需要确保的是$\textbf{Ax}=\textbf{b} (2.11)$对于每个$\textbf{b}$都有最多一个解。为此,我们要确保矩阵最多只能有$m$列。否则,我们就有不止一种parametrizing每个解的方法。 总结来说,这意味着矩阵$\textbf{A}$必须是**方阵**,也就是$m=n$,且所有的列之间都只能是**linearly independent**的。<span style="color:blue;font-weight: bold">一个有linearly dependent列的方阵被称为singular</span> 如果$\textbf{A}$不是方阵 或者是方阵但是一个singular,那么它仍可以用来求解$\textbf{Ax}=\textbf{b} (2.11)$。但是,我们就不能用matrix inversion的方法来求解了。 ## 2.5. Norms 范式 有些时候我们需要测量一个向量的长度或大小。在machine learning中我们通常称为Norm $||\textbf{x}||_p=( \Sigma_{i}|x_i|^p )^{\frac{1}{p}}\space\space\space (2.30)$, for $p\in \mathbb{R}, p >=1$. 包括$L^p$ norm在内,Norms就是用来将向量变换成非负值的函数。直观上来理解,一个向量$\textbf{x}$的norm就是从原点到点$\textbf{x}$的距离。严谨的来说,norm是任何满足如下性质方程$f$: * $f(\textbf{x})=0 =>\textbf{x}=\textbf{0}$ * $f(\textbf{x+y}) <= f(\textbf{x}) + f(\textbf{y})$ (the **triangle inequality**) * $\forall \alpha \in \mathbb{R}, f(\alpha \textbf{x})=|\alpha|f(\textbf{x})$ 对于$L^2$ norm来说,$p=2$,也叫**Euclidean Norm**。也可以理解成从原点到$\textbf{x}$的距离。在machine learning中,$L^2$ norm也记做$||\textbf{x}||$,可以通过$\textbf{x}^T\textbf{x}$来计算 squared $L^2$ norm比$L^2$ norm更容易在数学上和计算上进行处理。例如squared $L^2$ norm对每个$\textbf{x}$求导 只取决于相应元素$\textbf{x}$。然而所有$L^2$ norm对于$\textbf{x}$的导数取决于整个向量。在许多情况下,squared $L^2$ norm也可能不太好用,因为它靠近原点的速度很慢。在有些machine learning的应用中,<span style="color:blue; font-weight: bold">区分0和非0是非常重要的,这种情况下,通常会使用$L^1$ norm</span>,可以写成: $||\textbf{x}||_1=\Sigma_i|x_i| (2.31)$ > [name=Yinghai Yu]这里对于L1正则的解释很好,尤其是它的应用场景。区分于L2正则 有时候你会看到有些作者提到$L^0$ norm,这是错误的术语。它指的是一个向量中非零值的个数。而这个非零值的个数并不是norm,因为如果我们对该向量缩放$\alpha$倍并不能改变 非零值的个数。 这里还提到了另一种常用的norm,**max norm**, $L^{\infty}$ norm. $||\textbf{x}||_{\infty}=max_i{x_i}$ 在有些时候我们可能也会想要去测量矩阵的大小,此时我们会用到**Frobenius norm**: $||\textbf{A}||_F=\sqrt{\Sigma_{i,j}A_{i,j}^2} (2.33)$, 这可以类比到一个向量的$L^2$ norm。 两个向量之间的点积可以用norm来重写成:$\textbf{x}^T\textbf{y}=||\textbf{x}||_2||\textbf{y}||_2 \cos \theta (2.34)$, where $theta$ is the angle between $\textbf{x}$ and $\textbf{y}$ ## 2.6. Special Kinds of Matrices and Vectors 有些特殊种类的矩阵和向量非常有用: 对角矩阵(**Diagonal matrices**),这种矩阵的元素大多是0,非0元素只分布在主对角线上。 我们记 $diag(\textbf{v})$作为一个方形对角矩阵,对角线元素用向量$\textbf{v}$表示。要计算$diag(\textbf{v})\textbf{x}$,我们只需要将每一个元素$x_i$缩放$v_i$倍即可。换句话说,$diag(\textbf{v})\textbf{x}=\textbf{v} \odot \textbf{x}$。 同样地,对角矩阵的inverse也很好计算,$diag(\textbf{v})^{-1}=diag([1/v_1,...,1/v_n]^T)$。在许多情况下,我们可以根据任意矩阵推导出一些非常通用的机器学习算法,但通过将一些矩阵限制为对角线来获得更便宜(且描述性更少)的算法。 **并不是所有的对角矩阵都需要是方阵**。非方阵对角矩阵是没有逆矩阵的,但我们仍可以很快算出这些矩阵的对角矩阵。对于非方阵的对角矩阵$\textbf{D}$,矩阵乘积$\textbf{Dx}$将会包含对$\textbf{x}$每个元素的缩放。如果$\textbf{D}$高于自己的宽,那么就会合并一些0;如果$\textbf{D}$宽于自己的高,那么就会舍弃掉最后的几个元素 对称矩阵(**symmetric matrix**),矩阵的转置等于本身, $\textbf{A}=\textbf{A}^T$ 对称矩阵通常是由两个参数的函数生成的,不依赖于参数的顺序。例如,如果$\textbf{A}$是用来衡量距离的矩阵,$\textbf{A}_{i,j}$表示了点$i$, $j$之间的距离,那么有$\textbf{A}_{i,j}=\textbf{A}_{j,i}$(因为矩阵是对称的)。 单位向量(**unit vector**)是$||\textbf{x}||_2=1$ 当满足$\textbf{x}^T\textbf{y}=0$,则我们说这两个向量是orthogonal的。如果两个向量都是非0的norm,这意味着在空间中它们两个是90度的关系。在$n$纬实数空间$\mathbb{R}^n$中,最多有$n$个向量可能是相互与非0 norm 正交的。如果向量不仅正交,而且是unit norm,那我们称它们**orthonormal** > [name=Yinghai Yu]<span style="color:blue">这里要好好理解一下正交</span> 一个**orthogonal matrix**是一个方阵,它的行都相互正交,且它的列也都相互正交,即: $\textbf{A}^T\textbf{A}=\textbf{A}\textbf{A}^T=\textbf{I} (2.37)$ 这意味着: $\textbf{A}^{-1}=\textbf{A}^T (2.38)$ 所以说正交矩阵的有趣之处在于,他们的逆矩阵$\textbf{A}^{-1}$很容易计算。 <span style="color:blue">但是需要注意的是,虽然很反直觉,但是它们的行不仅是正交(orthogonal)的,而且是完全标准正交(orthonormal)的。对于行或列正交(orthogonal)但不标准正交(orthonormal)的矩阵,没有特殊的术语来描述它们</span> ## 2.7. Eigendecomposition 我们知道整数可以分解成素数因子。例如我们描述12这个数 取决于我们所选的进制,但12永远都可以写成$12=2\times 2 \times 3$,如此一来我们就发现了12的性质,例如不能被5整除,或者它可以被3整除。所以在矩阵中,我们也可以通过类似的分解方法来找到矩阵的一些性质和信息。其中一种非常常用的矩阵分解方法就是**特征值分解(eigen-decomposition)**,它会将一个矩阵分解成一组eigenvectors和eigenvalues。 矩阵$\textbf{A}$的eigenvector是一个非0向量$\textbf{v}$,使得矩阵与它的乘积 只会在对它进行缩放而不发生旋转: $\textbf{Av}=\lambda \textbf{v} (2.39)$ 这里的$\lambda$就是一个scalar,也叫做**eigenvalue**, 与eigenvector $\textbf{v}$相对。 不难看出,如果scalar $\lambda$是eigenvalue, $\textbf{v}$是eigenvector,那么对$\textbf{v}$缩放任意非0实数$\textbf{s}$倍后,得到的$\textbf{sv}$仍然可以作为eigenvector。所以求解eigenvector时,通常只需要得到unit eigenvector 这里我们将从几何上理解eigenvector和eigenvalue。对于矩阵$\textbf{A}$,假设它有$n$个linearly independent eigenvectors, $\{\textbf{v}^{(1)}, ..., \textbf{v}^{(n)}\}$,与其对应的是$n$个eigenvalues: $\{\lambda_1, ..., \lambda_n\}$。下图展示了eigenvector和eigenvalue的作用。黑色向量是$\textbf{A}$的两个eigenvectors, $\textbf{v}_1$, $\textbf{v}_2$(相互标准正交, orthogonormal)。(左图)表示所有unit vectors $\textbf{u}\in \mathbb{R}^2$所形成的单位圆。(右图)表示所有点$\textbf{Au}$。通过观察发现$\textbf{A}$会将单位圆沿着$\textbf{v}^{(i)}$的方向拉伸$\lambda_i$倍。 ![](https://hackmd.io/_uploads/HJaWYlyqh.png) 我们现在将所有的eigenvectors合并,形成一个矩阵$\textbf{V}$,它的每一列都是一个eigenvector,即$\textbf{V}=[\textbf{v}^{(1)}, ..., \textbf{v}^{(n)}]$。同样的,我们将所有eigenvalues合并成一个向量$\boldsymbol{\lambda}=[\lambda_1,...,\lambda_n]^T$。所以矩阵$\textbf{A}$的eigendecomposition(特征分解)可以写成: $\textbf{A}=\textbf{V} diag(\boldsymbol{\lambda})\textbf{V}^{-1} (2.40)$ > [name=Yinghai Yu]所以将矩阵分解成eigenvalue和eigenvector可以让我们在空间中对矩阵沿某方向进行拉伸 <span style="color:blue">但一般来说,更常用的就只是分解后得到的eigenvalue和eigenvector。当然,也不是所有的矩阵都能被eigendecompose</span> Deep Learning这本书里用到的只是实数对称矩阵(real symmetric matrix)。这些矩阵都可以背特征分解成实数的eigenvalue和eigenvector: $\textbf{A}=\textbf{Q}\boldsymbol{\Lambda}\textbf{Q}^T (2.41)$ 这里的$\textbf{Q}$ 是由 $\textbf{A}$ 的特征向量组成的正交矩阵; $\Lambda$是对角矩阵;$\Lambda_{i,i}$(对角线上的元素)对应着$\textbf{Q}$第$i$列上的eigenvector。因为$\textbf{Q}$的每两列之间都相互正交,所以可以认为$\textbf{A}$是在方向$\textbf{v}^{(i)}$上由$\lambda_i$缩放得到的。 尽管实数对称矩阵$\textbf{A}$一定有eigendecomposition,但特征分解并不一定是unique的。如果任意两个或多个特征向量共享相同的特征值,则任意一组正交向量位于其linear span内也是具有该特征值的特征向量,我们可以等效地选择一个 $\textbf{Q}$使用这些特征向量代替。按照惯例,我们通常对 $\boldsymbol{\Lambda}$ 的元素进行排序按降序排列。在此约定下,特征分解仅是唯一的如果所有特征值都是唯一的。 矩阵的eigendecomposition可以告诉我们很多关于该矩阵的信息: * 当且仅当任意一个eigenvalue为0时,矩阵是singular(奇异矩阵) * 实数对称矩阵的eigendecomposition也可以被用来优化形如$f(\textbf{x})=\textbf{x}^T\textbf{Ax},s.t. ||\textbf{x}||_2=1$的quadratic expression。每当$\textbf{x}$等于 $\textbf{A}$ 的特征向量,$f$ 取相应特征值的值。$f$在约束区域内的最大值就是最大特征值,其在约束区域内的最小值即为最小特征值。 特征值全部为正的矩阵称为正定矩阵(**positive definite**)。 特征值全部为正或零值的矩阵称为半正定矩阵(**positive semidefinite**) 如果所有特征值都为负,则矩阵是负定矩阵(**negative definite**) 如果所有特征值均为负值或零值,则为半负定矩阵(**negative semidefinite**) 半正定矩阵很有趣,因为它们保证$\forall\textbf{x}, \textbf{x}^T\textbf{Ax}\geq 0$。 正定矩阵保证 $\textbf{x}^T\textbf{Ax}=0 \Rightarrow\textbf{x}=\boldsymbol{0}$ > [name=Yinghai Yu] <span style="color:blue">这里很好理解,上面倒数第二条就是由(2.41)加上半正定矩阵的性质而来</span> ## 2.8. Singular Value Decomposition (SVD) 另一种常见的factorize一个矩阵的方法是**SVD (Singular Value Decomposition)**。也就是将矩阵分解成**singular vectors**和**singular values**。相比起特征分解,SVD更通用,尤其是在当背分解的矩阵不是方阵的时候,我们就要用到SVD。我们回想之前特征分解方法:$\textbf{A}=\textbf{V}diag(\boldsymbol{\lambda})\textbf{V}^{(-1)} (2.42)$ SVD使用的方法如下: $\textbf{A}=\textbf{UDV}^T (2.43)$ 这里$\textbf{A}$是一个$m\times n$的矩阵;$\textbf{U}$是一个$m\times m$的方阵;$\textbf{D}$是一个$m\times n$的矩阵;$\textbf{V}$是一个$n\times n$的方阵。且矩阵$\textbf{U}$, $\textbf{V}$都被定义为orthogonal matrix。$\textbf{D}$是一个对角矩阵,且可以不是方阵。 $\textbf{D}$对角线上的元素被称为$\textbf{A}$的**singular values**(奇异值)。$\textbf{U}$的列被称为**left-singular vectors**(左奇异向量);$\textbf{V}$的列被称为**right-singular vectors**(右奇异向量)。 实际上也可以按照矩阵$\textbf{A}$的特征分解方程来解释SVD。$\textbf{A}$的左奇异向量就是$\textbf{AA}^T$;$\textbf{A}$的右奇异向量就是$\textbf{A}^T\textbf{A}$。$\textbf{A}$的非0奇异值就是$\textbf{AA}^T$或者$\textbf{AA}^T$的平方根。 ## 2.9. The Moore-Penrose Pseudoinverse 逆矩阵不是为非方阵而定义的。假设我们现在想要一个矩阵$\textbf{A}$的左逆矩阵(left-inverse matrix) $\textbf{B}$,从而来求解线性等式$\textbf{Ax}=\textbf{y} (2.44)$。 通过左乘得到: $\textbf{x}=\textbf{By} (2.45)$ 取决于问题的结构,有时可能无法构建一个从$\textbf{A}$到$\textbf{B}$的映射。如果$\textbf{A}$高于自己的宽,那么上面的等式(2.45)可能无解。如果$\textbf{A}$宽于自己的高,那么(2.45)可能有多个解。 **Moore-Penronse pseudoinverse**摩尔-彭罗斯伪逆使我们能够在这些情况下取得一些进展。$\textbf{B}$ 的伪逆被定义为矩阵: $$ A^+=\lim_{\alpha\searrow 0}(\textbf{A}^T\textbf{A}+\alpha \textbf{I})^{-1}\textbf{A}^{-1} \space\space\space\space\space (2.46) $$ (2.45)并不是实际应用中Moore-Penrose pseudoinverse的算法实现,而是使用了$\textbf{A}^+=\textbf{VD}^+\textbf{U}^T, (2.47)$ 这里的$\textbf{U}$, $\textbf{D}$, $\textbf{V}$还是SVD中的矩阵,$\textbf{U}^+$是对角矩阵$\textbf{D}$的伪逆(pseudoinverse),<span style="color:blue">是通过将$\textbf{D}$对角线上的非0元素取倒数然后转置得到的</span>。 当$\textbf{A}$的列数多于行数,用pseudoinverse求解线性等式会得到多个可行解。具体来说,它可以提供$\textbf{x}=\textbf{A}^+\textbf{y}$在Euclidean norm $||\textbf{x}||_2$中的所有可行解。 当$\textbf{A}$的行数多于列数,等式可能无解。此时,使用pseudoinverse会给我们一个$\textbf{x}$,使得$\textbf{Ax}$在Euclidean norm $||\textbf{Ax}-\textbf{y}||_2$中尽可能接近$\textbf{y}$。 ## 2.10. The Trace Operator <span style="color:blue; font-weight: bold">迹运算是对一个矩阵的对角线元素进行求和的运算</span>,写成: $Tr(\textbf{A})=\Sigma_i\textbf{A}_{i,i}\space\space\space\space (2.48)$ 一些不借助求和符号就很难指定的运算可以使用矩阵乘积和迹运算符来指定。例如,迹运算符提供了另一种编写矩阵 Frobenius 范数的方法:$||A||_F=\sqrt{Tr(\textbf{AA}^T)}\space\space\space\space (2.49)$ 根据迹运算符表达式为使用许多有用的恒等式操作表达式提供了机会。例如,迹运算对于转置运算是不变的:$Tr(\textbf{A})=Tr(\textbf{A}^T) (2.50)$ 如果相应矩阵的形状允许定义结果乘积,则由许多因子组成的方阵的迹对于将最后一个因子移动到第一个位置也是不变的(也就是交换律): $Tr(\textbf{ABC}=Tr(\textbf{CAB})=Tr(\textbf{BCA}) (2.51)$。或者具体写成:$Tr(\boldsymbol{\prod_{i=1}^nF^{(i)}})=Tr(F^{(n)} \prod_{i=1}^{n-1}F^{(i)})$ 这种不变性即使当$\textbf{A}\in \mathbb{R}^{m\times n}$,$\textbf{B}\in \mathbb{R}^{n\times m}$时,我们也有$Tr(\textbf{AB})=Tr(\textbf{BA})$。$\textbf{AB}\in \mathbb{R}^{m\times m}$,$\textbf{BA}\in\mathbb{R}^{n\times n}$。 ## 2.11. The Determinant(行列式) 一个方阵的行列式记为$det(\textbf{A})$,<span style="color:blue; font-weight: bold">这是一个从矩阵到实数的映射方程。矩阵的行列式等于矩阵所有eigenvalues的乘积。行列式的绝对值可以被认为是矩阵乘法扩展或收缩空间的量度。</span>如果行列式为 0,则空间至少沿着一维,导致其失去所有体积。如果行列式为 1,则变换将保留体积。 ## 2.12. Example: Principal Components Analysis (PCA) machine learning 很常用的一项技术就是PCA,它源自线性代数基础。假设我们现在在$n$维实数空间$\mathbb{R}^n$中有$m$个数据点$\{x^{(1)}, ..., x^{(m)}\}$。如果我们想要有损压缩(Lossy Compression)这些点。 > 有损压缩意味着以需要较少内存但可能会损失一些精度的方式存储点。我们希望损失尽可能少的精度。 其中比较常见的方法是将高维数据在低纬表示。例如,对于每个数据点$\textbf{x}^{(i)}\in \mathbb{R}^n$,需要找到对应的编码向量(code vector)$\textbf{c}^{(i)}\in \mathbb{R}^l$。很明显,$l<n$,因为我们要从高维进入低纬。如此我们只需要更少的内存存储编码点(code point)。 <span style="color:blue; font-weight:bold;">为了完成这种高到低的映射,就需要一个编码方程(encoding function)$f(\textbf{x})=\textbf{c}$和一个解码方程(decoding function) $\textbf{x}\approx g(f(\textbf{x}))$。PCA就是能够实现上述要求的decoding function。</span>为了让decoding更简单,我们会选择矩阵乘法来将code(也就是低纬空间的数据点)映射回 高维$\mathbb{R}^n$。我们让$g(\textbf{c})=\textbf{Dc}$,$\textbf{D}\in \mathbb{R}^{n\times l}$作为定义decoding的那个矩阵。 要为该decoder计算最佳的code(低纬空间数据点)会非常困难。所以为了让encoding问题变得简单,PCA通常要将$\textbf{D}$的列限制为俩俩orthogonal的(要注意,这里的$\textbf{D}$并不是严格意义上的orthogonal matrix,除非$l=n$。) 有了这个限制条件,问题就简单了,因为我们发现,如果对每个数据点成比例地降低$\textbf{c}_i$,就能成倍提高$\textbf{D}_{:,i}$的值。为了让问题有唯一解,就需要将所有$\textbf{D}_{:,i}$的列限制为有unit norm。 更进一步,为了让我们能够实现该算法,首先要知道如何生成每个高维空间点$\textbf{x}$的最佳code(低纬空间点)$\textbf{c}^*$。其中一个方法就是最小化 $\textbf{x}$和它的重构方程(reconstruction function) $g(\textbf{c}^*)$。我们可以使用norm来衡量该距离。在PCA中,我们用的是$L^2$ norm: $\textbf{c}^*=\arg \min_c||\textbf{x}-g(\textbf{c})||_2 \space\space\space\space (2.54)$ 这里我们可以选用squared $L^2$ norm而不是$L^2$ norm本身,因为二者都可以在给定$\textbf{c}$时,最小化$\textbf{c}^*$。对于同一个$\textbf{c}$来说,$L^2$ norm本身是非负的,加平方不会改变单调性,所以我们有$\textbf{c}^*=\arg \min_c||\textbf{x}-g(\textbf{c})||_2^2 \space\space\space\space (2.55)$。那么由线性代数我们得到: $(\textbf{x}-g(\textbf{c}))^T(\textbf{x}-g(\textbf{c}))\space\space\space (2.56)$ 根据之前对于范式norm的理解,我们有 $||\textbf{x}||_p=( \Sigma_{i}|x_i|^p )^{\frac{1}{p}}\space\space\space (2.30)$ 结合(2.56)得到 $$(2.56) \\ =\textbf{x}^T\textbf{x}-\textbf{x}^T g(\textbf{c})-g(\textbf{c})^Tg(\textbf{c}) \space (2.57) \\ = \textbf{x}^T\textbf{x}- 2\textbf{x}^Tg(\textbf{c}) + g(\textbf{c})^Tg(\textbf{c}) \space\space (2.58) \\ $$ ($g(\textbf{c})^T\textbf{x}$这个scalar等于它自身的转置) 那么我们把上面的(2.58)进行最小化,忽略第一项,因为该项与$\textbf{c}$无关,所以有: $\textbf{c}^*=\arg\min_c(-2\textbf{x}^Tg(\textbf{c})+g(\textbf{c})^Tg(\textbf{c})) \space\space\space (2.59)$ 为了进一步计算,我们必须按照$g(\textbf{c})$的定义对其进行代换: $$ \textbf{c}^*=\arg\min_c(-2\textbf{x}^T\textbf{Dc}+\textbf{c}^T\textbf{D}^T\textbf{Dc}) \space\space\space (2.60) \\ =\arg\min_c (-2\textbf{x}^T\textbf{Dc} + \textbf{c}^T\textbf{I}_l\textbf{c}) \space\space\space (2.61) \\ = \arg\min_c (-2\textbf{x}^T\textbf{Dc} + \textbf{c}^T\textbf{c}) \space\space\space (2.62) \\ $$ 我们可以用vector calculus进一步优化该问题(如果你不了解的话,可以看section 4.3): $$ \nabla_c(-2\textbf{x}^T\textbf{Dc}+\textbf{c}^T\textbf{c}) = \textbf{0} \space\space\space (2.63) \\ -2\textbf{D}^T\textbf{x}+2\textbf{c} = \textbf{0}\space\space\space (2.64) \\ \textbf{c} = \textbf{D}^T\textbf{x} \space\space\space (2.65) $$ 如此一来,算法效率可被提高: 我们仅通过一个矩阵乘法就得到了optimally encode的$\textbf{x}$。对于一个vector的encode,我们可以用encoder function: $f(\textbf{x}) = \textbf{D}^T \textbf{x} \space\space\space (2.66)$ 对上式再左乘一个矩阵$\textbf{D}$得到: $r(\textbf{x}) = g(f(\textbf{x}))=\textbf{DD}^T\textbf{x} \space\space\space(2.67)$ 接着我们要确定encoding matrix $\textbf{D}$。我们再回想一下最小化inputs 和reconstructions之间的$L^2$距离。由于我们将使用同一个matrix $\textbf{D}$来decode所有点,那我们不再需要单独考虑每个点,而是直接最小化误差矩阵的Frobenium norm: $\textbf{D}^*=\arg\min_\textbf{D}\sqrt{\Sigma_{i,j}(x_j^{(i)}-r(\textbf{x}^{(i)})_j)^2}, \space\space\space s.t. \textbf{D}^T\textbf{D} = \textbf{I}_l \space\space\space (2.68)$ 要推导上面的式子求$\textbf{D}^*$,可以首先考虑当$l=1$的情况。此时,$\textbf{D}$是一个vector,$\textbf{d}$。将(2.68)和(2.67)替换,简化$\textbf{D}$为$\textbf{d}$,就能将问题化简为: $\textbf{d}^*=\arg\min_\textbf{d}\Sigma_i||\textbf{x}^{(i)}-\textbf{dd}^T\textbf{x}^{(i)}||^2_2 \space\space\space s.t. ||\textbf{d}||_2=1 \space\space\space (2.69)$ 上面等式所进行的替换虽然很直接,但并不是最流行的方法。<span style="color:blue">由于$\textbf{d}^T\textbf{x}^{(i)}$是一个scalar</span>,通常我们会写成: $\textbf{d}^*=\arg\min_\textbf{d}\Sigma_i||\textbf{x}^{(i)}-\textbf{d}^T\textbf{x}^{(i)}\textbf{d}||^2_2 \space\space\space s.t. ||\textbf{d}||_2=1 \space\space\space (2.70)$ 或者根据<span style="color:blue">“scalar”等于它自身的转置</span>这一性质写成: $\textbf{d}^*=\arg\min_\textbf{d}\Sigma_i||\textbf{x}^{(i)}-\textbf{x}^{(i)T}\textbf{dd}||^2_2 \space\space\space s.t. ||\textbf{d}||_2=1 \space\space\space (2.71)$ 这里我们需要把求和符号通过矩阵的方式表示,以此来方便我们进行后续计算。我们让$\textbf{X}\in \mathbb{R}^{m\times n}$为矩阵,该矩阵是通过将所有描述原数据点的向量上下堆叠而成,使得$\textbf{X}_{i,:}=\textbf{x}^{(i)T}$。那我们可以吧(2.71)重写为: $\textbf{d}^*=\arg\min_\textbf{d}||\textbf{X}-\textbf{Xdd}^T||^2_F \space\space\space s.t. \textbf{d}^T\textbf{d}=1 \space\space\space (2.72)$ 如果忽略掉限制条件,我们将上面的Frobenius Norm的部分简化成: $$\arg\min_{\textbf{d}}||\textbf{X}-\textbf{Xdd}^T||^2_F \space\space\space (2.73) \\ =\arg\min _{\textbf{d}} Tr((\textbf{X}-\textbf{Xdd}^T)^T(\textbf{X}-\textbf{Xdd}^T)) \space\space\space (2.74) \\ 根据(2.49): \\ =\arg\min _{\textbf{d}} Tr(\textbf{X}^T\textbf{X}-\textbf{X}^T\textbf{Xdd}^T-\textbf{dd}^T\textbf{X}^T\textbf{X}+\textbf{dd}^T\textbf{X}^T\textbf{Xdd}^T) \space\space\space (2.75) \\ =\arg\min _{\textbf{d}} Tr(\textbf{X}^T\textbf{X})-Tr(\textbf{X}^T\textbf{Xdd}^T)-Tr(\textbf{dd}^T\textbf{X}^T\textbf{X})+Tr(\textbf{dd}^T\textbf{X}^T\textbf{Xdd}^T) \space\space\space (2.76) \\ =\arg\min _{\textbf{d}}-Tr(\textbf{X}^T\textbf{Xdd}^T)-Tr(\textbf{dd}^T\textbf{X}^T\textbf{X})+Tr(\textbf{dd}^T\textbf{X}^T\textbf{Xdd}^T) \space\space\space (2.77) \\ 由于不含\textbf{d}的项不影响 \arg\min的结果,所以有:\\ =\arg\min _{\textbf{d}}-2Tr(\textbf{X}^T\textbf{Xdd}^T)+Tr(\textbf{dd}^T\textbf{X}^T\textbf{Xdd}^T) \space\space\space (2.78) \\ 由于Trace的特性,我们可以把Trace内部的矩阵做一个环形排序,得到:\\ =\arg\min _{\textbf{d}}-2Tr(\textbf{X}^T\textbf{Xdd}^T)+Tr(\textbf{X}^T\textbf{X}\textbf{dd}^T\textbf{dd}^T) \space\space\space (2.79) \\ 使用同样的特性,然后再加回限制条件,得到:\\ =\arg\min _{\textbf{d}}-2Tr(\textbf{X}^T\textbf{Xdd}^T)+Tr(\textbf{X}^T\textbf{X}\textbf{dd}^T\textbf{dd}^T) \space\space\space s.t. \textbf{d}^T\textbf{d}=\textbf{1}\space\space\space (2.80) \\ =\arg\min _{\textbf{d}}-2Tr(\textbf{X}^T\textbf{Xdd}^T)+Tr(\textbf{X}^T\textbf{X}\textbf{dd}^T) \space\space\space s.t. \textbf{d}^T\textbf{d}=\textbf{1}\space\space\space (2.81) \\ 根据限制条件,我们有:\\ =\arg\min _{\textbf{d}}-Tr(\textbf{X}^T\textbf{X}\textbf{dd}^T) \space\space\space s.t. \textbf{d}^T\textbf{d}=\textbf{1}\space\space\space (2.82) \\ =\arg\max _{\textbf{d}}Tr(\textbf{X}^T\textbf{X}\textbf{dd}^T) \space\space\space s.t. \textbf{d}^T\textbf{d}=\textbf{1}\space\space\space (2.83) \\ =\arg\max _{\textbf{d}}Tr(\textbf{d}^T\textbf{X}^T\textbf{Xd}) \space\space\space s.t. \textbf{d}^T\textbf{d}=\textbf{1}\space\space\space (2.84) \\ $$ > [name=Yinghai Yu] $||A||_F=\sqrt{Tr(\textbf{AA}^T)}\space\space\space\space (2.49)$ 该优化问题可以通过特征分解来解决。具体说,给定eigenvector $\textbf{X}^T\textbf{X}$及其对应的eigenvalue时的最优解$\textbf{d}$。 该推导过程仅仅是针对于$l=1$的情况。更广义上,当我们想要恢复principal components的basis的时候,矩阵$\textbf{D}$由对应于最大特征值的 $l$ 个特征向量给出。这可以通过归纳证明来证明。我们建议将此证明作为练习来写。线性代数是理解深度学习所必需的基础数学学科之一。机器学习中普遍存在的另一个数学关键领域是概率论. # 3. Probability and Information Theory ## 3.2 Random Variables 这里简单讲述了随机变量的分类等。 分类上,分为discrete和continuous。核心区别是是否有有限个状态infinite states ## 3.3. Probability Distributions ### 3.3.1 Discrete Variables and Probability Mass Function [TODO](/M1UASTgdTGC5DPTtD59YRQ) 离散变量的概率分布可以通过**probability mass function** (PMF)来描述,可以简写成$P$。 ### 3.3.2 Coninuous Variables and Probability Density Functions [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.4. Marginal Probability [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.5. Conditional Probability [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.6. The Chain Rule of Conditional Probabilities 任何 联合概率分布(joint probability distribution),无论有多少个随机变量,都可以被分解成只含一个变量的条件分布: $P(x^{(1)},...x^{(n)})=P(x^{(1)})\prod_{i=2}^nP(x^{(i)}|x^{(1)},...,x^{(i-1)}) \space\space\space\space (3.6)$ > [name=Yinghai Yu]上面所描述的就是概率上的chain rule或者product rule。 ## 3.7. Independence and Conditional Independence 如果概率分布可以表示为如下形式,则两个随机变量$x, y$之间是**independent(相互独立)**,记做$\text{x}\perp\text{y}$: $\forall x\in \text{x}, \space y\in \text{y}, p(\text{x}=x, \text{y}=y)=p(\text{x}=x)p(\text{y}=y)\space\space\space\space (3.7)$ 如果两个随机变量$\text{x}, \text{y}$满足下面条件,即给定一个随机变量$\text{z}$,如果$\text{x}, \text{y}$二者的条件概率分布可以进行如下因式分解,则二者是**(conditionally independent)条件独立的**, 记做$\text{x}\perp\text{y}|\text{z}$: $\forall x\in\text{x}, y\in\text{y},z\in\text{z},p(\text{x}=x,\text{y}=y|\text{z}=z)=p(\text{x}=x|\text{z}=z)p(\text{y}=y|\text{z}=z) \space\space\space (3.8)$ ## 3.8. Expectation, Variance, and Covariance [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.9. Common Probability Distributions [TODO](/M1UASTgdTGC5DPTtD59YRQ) ### 3.9.1. Bernoulli Distribution ### 3.9.2. Multinoulli Distribution ### 3.9.3. Gaussian Distribution ### 3.9.4. Exponential and Laplace Distributions ### 3.9.5. The Dirac Distribution and Empirical Distribution ### 3.9.6. Mistures of Distributions ## 3.10. Useful Properties of Common Functions [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.11. Bayes' Rule [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.12. Technical Details of Continuous Variables [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.13. Information Theory [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 3.14. Structured Probabilistic Models [TODO](/M1UASTgdTGC5DPTtD59YRQ) # 4. Numerical Computation ## 4.1. Overflow and Underflow [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 4.2. Poor Conditioning [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 4.3. Gradient-Based Optimization [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 4.4. Constrained Optimization [TODO](/M1UASTgdTGC5DPTtD59YRQ) ## 4.5. Example Linear Least Squares [TODO](/M1UASTgdTGC5DPTtD59YRQ) # 5. Machine Learning Basics ## 5.1. Learning Algorithms 主要讲述了什么是学习算法?——当一个算法能够从data中学到东西。 如何定义“学习”——A computer program is said to learn from experience $E$ with respect to some class of tasks $T$ and performance measure $P$, if its performance at tasks in $T$, as measured by $P$, improves with experience $E$. ### 5.1.1. The Task, $T$ 机器学习算法的分类,或者说,上述定义中“Task” $T$的分类: 1. Classification 2. Classification with missing inputs 3. Regression 4. Transcription 5. Machine translation 6. Structured output 7. Anomaly detection 8. Synthesis and sampling 9. Imputation of missing values 10. Denoising 11. Density estimation or probability mass function estimation ### 5.1.2. The Performance measure, $P$ [TODO](/M1UASTgdTGC5DPTtD59YRQ) ### 5.1.3. The Experience, $E$ [TODO](/M1UASTgdTGC5DPTtD59YRQ) 从大类上,根据"Experience"可以将机器学习算法分为**unsupervised**和**supervised**。 * **Unsupervised learning algorithms** experience a dataset containing many features, then learn useful properties of the structure of this dataset.在深度学习的背景下,我们通常希望学习生成数据集的整个概率分布,无论是在密度估计中显式学习,还是在合成或去噪等任务中隐式学习。其他一些无监督学习算法执行其他角色,例如聚类,其中包括将数据集划分为相似数据点的聚类 * **Supervised learning algorithms** experience a dataset containing features, but each example is also associated with a **label** or **target**. ### 5.1.4. Example: Linear regression 该模型是用来解决回归问题的。换句话说,我们的目的是搭建一个系统,使得它“吃”了一个vector$textbf{x}\in\mathbb{R}$后,可以给出一个预测值$y\in\mathbb{R}$。这里的$y$是一个scalar(常量)。那么我们有: $\hat{y}=\boldsymbol{w}^T\boldsymbol{x} \space\space\space\space (5.3)$ , where $\boldsymbol{w}\in\mathbb{R}^n$是一个**参数向量**(vector of **parameters**) Parameters是用来控制该系统行为的一组值。$w_i$是$x_i$的系数。也可以把$\boldsymbol{w}$视为每个feature将会如何影响预测值的“**权重**”。如果一个feature $x_i$收到一个正权重$w_i$,那么该feature $x_i$对于预测值$\hat{y}$有正向的影响。若为0,则无影响。 我们因此可以这样定义task $T$: to predict $y$ from $\boldsymbol{x}$ by outputting $\hat{y}=\boldsymbol{w}^T\boldsymbol{x}$. 接下来,我们要定义我们的performance measure, $P$. 假设我们设计了一个矩阵,它有$m$个数据点。我们不用这些数据点train模型,而是evaluate模型。我们还有一个regression targets向量,该向量提供了正确的(真实的)$y$值。由于数据集将只用来做evaluation,我们叫它**test set**。将该矩阵记做$\textbf{X}^{(test)}$,将vector of regression targets记做$\boldsymbol{y}^{(test)}$。 其中一个常见的用来描述模型性能的指标是mean squared error (MSE): $\text{MSE}_{test}=\frac{1}{m}\Sigma_i(\hat{\boldsymbol{y}}^{(test)}-\boldsymbol{y}^{(test)})_i^2 \space\space\space\space (5.4)$ 也可以写成: $\text{MSE}_{test}=\frac{1}{m}||\hat{\boldsymbol{y}}^{(test)}-\boldsymbol{y}^{(test)}||_2^2 \space\space\space\space (5.5)$ > [name=Yinghai Yu] 非常直观,在二维空间中如果预测值越接近真实值,那么模型性能越好。注意,为了防止大样本数据的影响,这里要除以数据点的个数$m$ 为了搭建一个机器学习算法,那我们就需要让算法能够通过改善$\boldsymbol{w}$来降低$\text{MSE}_{test}$。最直观的方法当然就是找$\text{MSE}$在training set上的最小值$\text{MSE}_{train}$。那我们可以求梯度,令梯度为$\textbf{0}$: $$ \nabla_\boldsymbol{w}\text{MSE}_{train}=0 \space\space\space\space (5.6) \\ \Rightarrow\nabla_\boldsymbol{w}\frac{1}{m}||\hat{\boldsymbol{y}}^{(train)}-\boldsymbol{y}^{(train)}||^2_2=0 \space\space\space\space (5.7) \\ \Rightarrow\frac{1}{m}\nabla_\boldsymbol{w}||\textbf{X}^{(train)}\boldsymbol{w}-\boldsymbol{y}^{(train)}||_2^2=0 \space\space\space\space (5.8) \\ \Rightarrow\nabla_\boldsymbol{w}(\textbf{X}^{(train)}\boldsymbol{w}-\boldsymbol{y}^{(train)})^T(\textbf{X}^{(train)}\boldsymbol{w}-\boldsymbol{y}^{(train)})=0 \space\space\space\space (5.9) \\ \Rightarrow\nabla_\boldsymbol{w}(\boldsymbol{w}^T\textbf{X}^{(train)T}\textbf{X}^{(train)}\boldsymbol{w}-2\boldsymbol{w}^T\textbf{X}^{(train)T}\boldsymbol{y}^{(train)}+\boldsymbol{y}^{(train)T}\boldsymbol{y}^{(train)})=0 \space\space\space\space (5.10) \\ \Rightarrow2\textbf{X}^{(train)T}\textbf{X}^{(train)}\boldsymbol{w}-2\textbf{X}^{(train)T}\boldsymbol{y}^{(train)}=0 \space\space\space\space (5.11) \\ \Rightarrow \boldsymbol{w}=(\textbf{X}^{(train)T}\textbf{X}^{(train)})^{-1}\textbf{X}^{(train)T}\boldsymbol{y}^{(train)}\space\space\space\space (5.12) $$ ![](https://hackmd.io/_uploads/rJXCcl852.png) > [name=Yinghai] ok,如此我们就有了对于linear regression的解。<span style="color:blue">(5.12)也叫做normal equations</span>。 值的注意的是,线性回归中通常会引入一个额外的参数intercept term $b$,从而有$\hat{y}=\boldsymbol{w}^T\boldsymbol{x}+b \space\space\space\space (5.13)$。顾名思义,$b$就是直线在$y$轴上的截距。<span style="color:blue;">$b$通常被叫做bias parameter</span>,该术语是因为,所有的预测值都会在原有的基础上偏移$b$。<span style="color:blue; font-weight:bold;">注意,statistical bias指的是statistical estimation algorithm's expected estimate of a quantity is not equal to the true quantity。与这里的bias $b$不同</span>。 # 6. Deep Feedforward Networks **Deep Feedforward Networks = Feedforward Neural Networks = Multilayer Perceptrons (MLP)** 是深度学习模型的原型。MLP的目标是估计某个函数$f^*$。例如对于一个classifier,$y=f^*(\boldsymbol{x})$是input $\boldsymbol{x}$到category $y$的一个映射。MLP会定义一个映射关系$\boldsymbol{y}=f(\boldsymbol{x};\boldsymbol{\theta})$并学习能够得到最优估计的参数$\boldsymbol{\theta}$。 Feedforward neural networks之所以是网络,是因为它们通常由多个不同的方程组合而成。模型与DAG (Directed Acyclic Graph)相关,用来描述这些方程之间是如何被组合到一起的。例如我们有三个方程$f^{(1)}, f^{(2)}, f^{(3)}$被连接在一个chain上,形成了$f(\boldsymbol{x})=f^{(3)}(f^{(2)}(f^{(1)}(\boldsymbol{x})))$。该chain结构在神经网络中非常常见。这里,$f^{(1)}$被称为**first layer**(神经网络的第一层)。那么chain的长度被称为**神经网络的深度(depth)**。最后一层被称为**output layer(输出层)**。 训练神经网络的过程中,我们会想让$f(\boldsymbol{x})$与$f^*(\boldsymbol{x})$相匹配。训练数据中包含噪声。我们需要评估在不同训练样本上的$f^*$。每个数据点$\boldsymbol{x}$都有一个label $y\approx f^*(\boldsymbol{x})$。训练数据点直接指定了 output layer在每个数据点$\boldsymbol{x}$该做的事情;必须产生一个接近$y$的值。除input/output layer外的其他层不会直接被training data所指定。input data不能明确告诉除input/output layer外的其他层具体该如何behave,而是要决定如何使用这些层来得到$f^*$最佳的approximation(估计)。这些层也被叫做hidden layer。 每一个hidden layer都是一个vector-valued的。这些hidden layer的维决定了模型的宽度width。每个vector中的元素可以被解释成神经网络中的神经元。 > [name=Yinghai]我们也可以这样理解:每个层layer都包含了许多units,<span style="color:blue">units之间是并行的,每个unit代表了一个vector-to-scalar function。每个unit会从其他units上接受一个input,然后计算该unit自己的activation value。</span>方程$f^{(i)}(\boldsymbol{x})$是用来计算 > 然而,现代神经网络研究是在许多数学和工程学科的指导下进行的,神经网络的目标并不是完美地模拟大脑。最好的办法是将前馈网络视为函数近似机,其目的是实现统计泛化,偶尔从我们对大脑的了解中汲取一些灵感,而不是将其视为大脑功能的模型。理解前馈网络的一种方法是从线性模型开始,并考虑如何克服其局限性。线性模型,如逻辑 回归和线性回归等线性模型之所以吸引人,是因为这些模型可以通过闭合形式 线性模型之所以吸引人,是因为它们可以通过闭合形式或凸优化的方法来可靠地进行计算。线性模型也有 线性模型也有一个明显的缺陷,即模型容量仅限于线性函数,因此 模型无法理解任何两个输入变量之间的相互作用。 为了让linear model能够描述对$\boldsymbol{x}$的nonlinear functions,我们可以通过将linear model的input $\boldsymbol{x}$转换成$\phi(\boldsymbol{x})$的方法来实现,其中,$\phi(\boldsymbol{x})$是nonlinear transformation(非线性变换)。同理,我们也可以应用kernel trick来得到nonlinear learning algorithm。我们可以将$\phi$视作一组用来描述$\boldsymbol{x}$的features,或者看作$\boldsymbol{x}$的一种新的表述。那么问题就变成了<span style="color:blue">如何选择映射$\phi$?</span> 1. 其中一种是使用普通的$\phi$映射,例如一个infinite-dimensional $\phi$,它是由RBF kernel来隐性描述的。如果$\phi(\boldsymbol{x})$的维度足够高,且我们总有足够的能力fit training set,但泛化到testing set上的表现很差。非常通用的特征映射(feature mapping)通常只基于局部平滑性原则,并不包含足够的先验信息来解决高级问题。 2. 另一种方法是人工设计$\phi$。在深度学习出现之前 这是主流方法。这种方法需要数十年的人工 每项独立任务都需要数十年的人工努力。领域(如语音识别或计算机视觉)的从业人员,而且在不同领域之间几乎没有转移。 3. 深度学习的策略是学习$\phi$。在这种方法中,我们有一个模型$y=f(\boldsymbol{x};\boldsymbol{\theta},\boldsymbol{w})=\phi(\boldsymbol{x};\boldsymbol{\theta})$ 。现在,我们有了参数 $\theta$,用来从一大类函数中学习 $\phi$;还有参数 $\boldsymbol{w}$,用来从$\phi(\boldsymbol{x})$ 映射到所需的输出。这是一个深度前馈网络的例子,$\phi$ 代表一个隐藏层。这种方法是三种方法中唯一一种放弃训练问题凸性的方法,但它利大于弊。在这种方法中,我们将表示参数化为$\phi(\boldsymbol{x};\boldsymbol{\theta})$,并使用优化算法找出对应于良好表示的$\boldsymbol{\theta}$。如果我们愿意,这种方法可以通过使用一个非常宽泛的$\phi(\boldsymbol{x};\boldsymbol{\theta})$族来获得第一种方法的优点。这种方法也能体现第二种方法的优点。人类实践者可以通过以下方式对自己的知识进行编码,以帮助泛化 设计他们期望表现良好的 $\phi(\boldsymbol{x};\boldsymbol{\theta})$族。这样做的好处是 的优势在于,人类设计者只需要找到正确的通用函数族, 而不是精确地找到正确的函数。 > [name=Yinghai] 理解下第三种方法 本章首先以一个简单的前馈网络为例。接下来 我们将讨论部署前馈网络所需的各项设计决策。首先,训练前馈网络需要做出许多与线性模型相同的设计决策。选择优化器、代价函数和输出形式。函数以及输出单元的形式。我们将回顾这些基于梯度学习的基础知识 学习的基本原理,然后讨论前馈网络特有的一些设计决策。前馈网络所特有的一些设计决策。前馈网络引入了 这就要求我们选择用于计算隐藏层的激活函数。用于计算隐藏层值的激活函数。我们还必须设计网络的架构 我们还必须设计网络的架构,包括网络应包含多少层、这些层应如何相互连接 层应如何相互连接,以及每层应包含多少个单元。每一层有多少单元。深度神经网络的学习需要计算复杂函数的梯度。复杂函数的梯度。我们介绍了反向传播算法及其 我们介绍了反向传播算法及其现代广义算法,它们可用于高效计算这些梯度。 ## 6.1. Example: Learning XOR XOR function (exclusive or)问题 我们定义该问题的MSE loss function为: $J(\boldsymbol{\theta})=\frac{1}{4}\Sigma_{\boldsymbol{x}\in\mathbb{X}}(f^*(\boldsymbol{x})-f(\boldsymbol{x};\boldsymbol{\theta}))^2 \space\space\space\space (6.1)$ 现在我们要选择模型的形式, $f(\boldsymbol{x};\boldsymbol{\theta})$。假设我们选择一个linear model,$\boldsymbol{\theta}$包含$\boldsymbol{w}$和$b$,那么我们的模型可以写成:$f(\boldsymbol{x};\boldsymbol{w},b)=\boldsymbol{x}^T\boldsymbol{w}+b \space\space\space\space (6.2)$ 如果对$J(\boldsymbol{\theta})$最小化 w.r.t. $\boldsymbol{w}$和$b$,那么我们可以使用normal equations。求解后得到$\boldsymbol{w}=\boldsymbol{0}$和$b=\frac{1}{2}$。也就意味着我们的linear model的output在任何时候都是0.5。 ![](https://hackmd.io/_uploads/Bkzk6r_c2.png) ![](https://hackmd.io/_uploads/S1GgpHO53.png) 该问题的解决方法是引入了一个含有2个hidden units的hidden layer。那么该前馈神经网络有一个vector of hidden units $\boldsymbol{h}$,它是由$f^{(1)}(\boldsymbol{x};\boldsymbol{W}, \boldsymbol{c})$方程计算得到。这些hidden units的值将会作为下一个layer的input。输出层仍然是一个linear regression model,但现在它是对$\boldsymbol{h}$而不是对$\boldsymbol{x}$的线性回归模型。神经网络现在包含了两个函数chain在一起。$\boldsymbol{h}=f^{(1)}(\boldsymbol{x};\boldsymbol{W},\boldsymbol{c})$且$y=f^{(2)}(\boldsymbol{h};\boldsymbol{w},b)$。于是我们有该模型:$f(\boldsymbol{x};\boldsymbol{W},\boldsymbol{c},\boldsymbol{w},b)=f^{(2)}(f^{(1)}(\boldsymbol{x}))$ $f^{(1)}$应该计算什么?——linear model目前尚能满足我们的需求,且它看似很诱人。但如果$f^{(1)}$是linear的,那么前馈网络整体上仍将是其输入的线性函数。如果我们忽略掉截距项$b$,假设$f^{(1)}(\boldsymbol{x})=\boldsymbol{W}^T\boldsymbol{x}$且$f^{(2)}(\boldsymbol{h})=\boldsymbol{h}^T\boldsymbol{w}$。那么$f(\boldsymbol{x})=\boldsymbol{w}^T\boldsymbol{W}^T\boldsymbol{x}$。我们将该方程表示为$f(\boldsymbol{x})=\boldsymbol{x}^T\boldsymbol{w}^{'}$, 其中$\boldsymbol{w}^{'}=\boldsymbol{W}\boldsymbol{w}$ <span style="color:blue; font-weight:bold;"> 显然,我们必须用一个nonlinear function来描述这些features。大多数神经网络使用的是一个 由可被学习的一组参数控制的 <a href="https://zh.wikipedia.org/wiki/%E4%BB%BF%E5%B0%84%E5%8F%98%E6%8D%A2">affine transformation</a>(仿射变换),加上一个固定的nonlinear function(该函数也就是所谓的activation function)得到的。</span> 通过使用该策略,我们定义$\boldsymbol{h}=g(\boldsymbol{W}^T\boldsymbol{x}+\boldsymbol{c}$,其中$\boldsymbol{W}$提供了线性变换的权重(weights of a linear transformation);$\boldsymbol{c}$代表biases。 之前,我们是这样描述linear regression的: 我们使用了一个vector of weights和一个scalar bias参数来描述一个从input vector到一个output scalar的仿射变换(affine transformation)。现在我们要描述的是从一个vector $\boldsymbol{x}$到另一个 vector $\boldsymbol{h}$的仿射变换。因此我们需要整个vector of bias参数。激活函数$g$通常要element-wise地应用到每个元素上,即:$h_i=g(\boldsymbol{x}^T\boldsymbol{W}_{:,i}+c_i)$。现在流行的神经网络使用的是[ReLU](https://www.cs.toronto.edu/~fritz/absps/reluICML.pdf) (Rectified Linear Unit),它是由该方程定义的:$g(z)=\max\{0,z\}$,见图6.3. ![](https://hackmd.io/_uploads/SkYm1IO5h.png) 那我们就有了如下的完整的神经网络的数学式: $$ f(\boldsymbol{x};\boldsymbol{W},\boldsymbol{c},\boldsymbol{w},b)=\boldsymbol{w}^T \max \{0, \boldsymbol{W}^T\boldsymbol{x}+\boldsymbol{c}+b\} \space\space\space\space (6.3) $$ 那再次尝试求解XOR问题,让 \begin{equation} \boldsymbol{W}= \left[ \begin{array}{cccc} 1 & 1 \\ 1 & 1 \\ \end{array} \right], \space\space\space\space (6.4) \end{equation} \begin{equation} \boldsymbol{c}= \left[ \begin{array}{cccc} 0 \\ -1 \\ \end{array} \right], \space\space\space\space (6.5) \end{equation} \begin{equation} \boldsymbol{w}= \left[ \begin{array}{cccc} 1 \\ -2 \\ \end{array} \right], \space\space\space\space (6.6) \end{equation} , $b=0$. 接着,让$\boldsymbol{X}$作为一个包含所有4个XOR问题的input数据的矩阵,即: \begin{equation} \boldsymbol{X}= \left[ \begin{array}{cccc} 0 & 0 \\ 0 & 1 \\ 1 & 0 \\ 1 & 1 \\ \end{array} \right], \space\space\space\space (6.7) \end{equation} 在神经网络中,第一步就是右乘一个权重矩阵$\boldsymbol{W}$: \begin{equation} \boldsymbol{XW}= \left[ \begin{array}{cccc} 0 & 0 \\ 1 & 1 \\ 1 & 1 \\ 2 & 2 \\ \end{array} \right], \space\space\space\space (6.8) \end{equation} 接着我们加上bias vector $\boldsymbol{c}$得到: \begin{equation} \left[ \begin{array}{cccc} 0 & -1 \\ 1 & 0 \\ 1 & 0 \\ 2 & 1 \\ \end{array} \right], \space\space\space\space (6.9) \end{equation} 在该空间中,所有数据点都分布在一条斜率为1的直线上。随着我们在该直线上移动,output需要从0开始,然后逐渐增加到1。然后降回0。一个linear model显然不能实现该变化。所以为了计算$\boldsymbol{h}$的值,我们只要简单的使用ReLU: \begin{equation} \boldsymbol{X}= \left[ \begin{array}{cccc} 0 & 0 \\ 1 & 0 \\ 1 & 0 \\ 2 & 1 \\ \end{array} \right], \space\space\space\space (6.10) \end{equation} 即可。 该ReLU变换将输入数据点之间的关系进行了改变。他们不再分布在一条直线上。根据图6.1,它们分布在一个可由一个linear model解决的空间上。 最终,我们通过乘上权重向量$w$完成对上面XOR问题的求解: \begin{equation} \boldsymbol{X}= \left[ \begin{array}{cccc} 0 \\ 1 \\ 1 \\ 0 \\ \end{array} \right], \space\space\space\space (6.11) \end{equation} 该神经网络到了对于每个输入数据的正确解。 在这个例子中,我们只是简单地指定了解决方案,然后证明它的误差为零。在实际情况中,可能会有数十亿个模型参数和数十亿个训练实例,因此我们不能像这里一样简单地猜测解决方案。相反,基于梯度的优化算法可以找到误差极小的参数。我们描述的 XOR 问题解决方案处于损失函数的全局最小值,因此梯度下降可以收敛到这一点。梯度下降法还能找到 XOR 问题的其他等效解。梯度下降法的收敛点取决于参数的初始值。在实践中,梯度下降法通常不会找到像我们在这里提出的这样简洁、易于理解的整数值解决方案 ## 6.2 Gradient-Based Learning ### 6.2.1. Cost/Loss Functions 设计深度学习算法的时候,很重要的一部分就是对于cost function的选择。cost function或多或少是与其他linear models的loss function类似。大多数情况下,parametric model会定义一个distribution $p(\boldsymbol{y}|\boldsymbol{x};\boldsymbol{\theta})$,且我们只要简单地使用MLE (maximum likelihood estimation)即可。这意味着在训练数据和模型预测之间 是可以使用cross-entropy的。 有时候我们会采取更简单的做法,即,不是预测整个$\boldsymbol{y}$的概率分布,而是预测一些条件概率(给定$\boldsymbol{x}$下,$\boldsymbol{y}$的条件概率)。这种更加specific的loss function让我们能够训练出这些estimates的predictor。 <span style="color:blue">用于训练一整个神经网络的total cost function通常会包含一个primary cost function外加一个正则项(regularization term)</span>。如此,我们仍可以对深度神经网络使用weight decay的方法,且该方法使用的也是最流行的正则化方法。第7章中会提到更高级的正则化方法。 #### 6.2.1.1. Learning Conditional Distributions with Maximum Likelihood <span style="color:blue">目前大多数神经网络都是使用MLE来训练的。这意味着很多cost function都只是一个negative log-likelihood (负最大似然), 也可以表述为cross-entropy(交叉熵)</span>。数学上写做: $J(\boldsymbol{\theta})=-\mathbb{E}_{\textbf{x,y}\sim\hat{p}_{\text{data}}}\log p_{\text{model}}(\boldsymbol{y}|\boldsymbol{x}) \space\space\space\space (6.12)$ > [name=Yinghai] 这里的 $\mathbb{E}$ 表示期望值运算符 特定形式的cost function视模型的$\log p_{\text{model}}$而定。(6.12)的延展通常会引入一些不受模型参数干扰的项,且可能会被忽略。例如,我们5.5.1.节中提到的, 如果$p_{\text{model}}(\boldsymbol{y|x})=\mathcal{N}(\boldsymbol{y};f(\boldsymbol{x}; \boldsymbol{\theta}),\boldsymbol{I})$,那我们有MSE如下: $J(\theta)=\frac{1}{2}\mathbb{E}_{\textbf{x,y}\sim \hat{p}_{\text{data}}}||\boldsymbol{y}-f(\boldsymbol{x};\boldsymbol{\theta})||+\text{const} \space\space\space\space (6.13)$ 忽略掉服从正态分布的常数项。之前我们看到**MLE在output distribution**上与**最小化linear model的MSE** 之间的等价性,但实际上,这种等价性在 使用$f(\boldsymbol{x};\boldsymbol{\theta})$预测高斯分布的均值时 也成立。 使用MLE推导cost function的其中一个好处是,它排除了为每个模型设计cost function的负担。指定一个模型$p(\boldsymbol{y}|\boldsymbol{x})$可以自动决定cost function $\log p(\boldsymbol{y|x})$ <span style="color:blue">设计神经网络时一个常见的主题就是cost function的gradient必须足够large且predictable</span>,这样才能为学习算法提供较好的指导。那些较饱和的函数(function that saturate and become very flat)会损坏这一目标(即不够large和predictable),因为它们会让gradient变得非常小。在许多情况下 出现这种情况的原因是,用于产生hidden unit或output units的激活函数饱和。负对数似然可以帮助许多模型避免这一问题。对于许多模型来说,负对数似然有助于避免这一问题。许多output units涉及一个指数函数, 当其参数非常负时就会饱和。negative log-likelihood cost function中的对数函数可以消除某些输出单元的 指数 函数。我们将 我们将在第 6.2.2 节讨论cost function与output units选择之间的交互作用。 用于最大似然估计的交叉熵成本有一个不寻常的特性,那就是当它应用于实际中常用的模型时,通常没有最小值。对于离散输出变量,大多数模型的参数设置方式都不能代表 0 或 1 的概率,但可以任意地接近于 0 或 1 的概率。logistic回归就是这种模型的一个例子。对于实值输出变量,如果模型可以控制输出分布的密度(例如,通过学习高斯输出分布的方差参数),那么就有可能为正确的训练集输出分配极高的密度,从而使交叉熵接近负值。本章介绍的正则化技术提供了几种不同的方法来修改学习问题,从而使模型不能获得无限的奖励。 #### 6.2.1.2. Learning Conditional Statistics 除了学习上述完整的条件概率分布$p(\boldsymbol{y|x};\boldsymbol{\theta})$外,我们还通常会想要学习给定$\boldsymbol{x}$时$\boldsymbol{y}$的条件统计。例如,我们有一个predictor $f(\boldsymbol{x};\boldsymbol{\theta})$,相用它来预测$\boldsymbol{y}$的均值mean。如果我们使用一个功能强大的神经网络,我们就可以认为神经网络能够代表各种函数中的任何函数 $f$,而这类函数只受连续性和有界性等特征的限制,而不是受特定参数形式的限制。从这个角度来看,我们可以把成本函数看作是一个函数,而不仅仅是一个函数。函数是从函数到实数的映射。因此,我们可以把学习看作是选择一个函数,而不仅仅是选择一组参数。我们可以设计成本函数,使其最小值出现在我们想要的某个特定函数上。例如,我们可以设计成本函数,使其最小值出现在将 $\boldsymbol{x}$ 映射为给定 $\boldsymbol{x}$ 的 $\boldsymbol{y}$ 的期望值的函数上。要解决与函数相关的优化问题,需要使用一种称为变分微积分的数学工具,详见第 19.4.2 节。要理解本章的内容,并不需要理解变分法。目前,我们只需了解变分法可用于推导以下两个结果。我们利用微积分变化推导出的第一个结果是,求解优化问题: $$ f^*=\arg\min\mathbb{E}_{\textbf{x,y}\sim p_{\text{data}}}||\boldsymbol{y}-f(\boldsymbol{x})||^2 \space\space\space\space (6.14) $$ 求 $$ f^*(\boldsymbol{x})=\mathbb{E}_{\textbf{y}\sim p_{\text{data}}}(\boldsymbol{y|x})[\boldsymbol{y}]\space\space\space\space (6.15) $$ 使得该方程处于我们optimize的class之内。换句话说,如果我们能够在有限多的样本上训练,最小化mean squared error cost function会给我们一个能够预测每个$\boldsymbol{x}$所对应的$\boldsymbol{y}$的均值。 不同的cost function会给出不同的统计量。另一个使用方差所计算得到的结果是: $$ f^*=\arg\min_f\mathbb{E}_{\textbf{x,y}\sim p_{\text{data}}}||\boldsymbol{y}-f(\boldsymbol{x})||_1 \space\space\space\space (6.16) $$ 产生一个函数,预测每个 $\boldsymbol{x}$ 的 $\boldsymbol{y}$ 的中值,只要这个函数可以被我们优化的函数族所描述。函数可以用我们优化的函数族来描述。这个成本 函数通常称为 .平均绝对误差(MAE) 不幸的是,MSE和MAE在用于基于梯度的优化时,往往会导致较差的结果。有些输出单元 与这些cost function结合使用时,会产生非常小的梯度。这也是交叉熵成本函数比MSE或MAE更受欢迎的原因之一,即使在不需要估计整个分布$p(\boldsymbol{y|x})$的情况下也是如此。 ### 6.2.2. Output Units cost function的选择与output unit的选择紧密结合。大多数时候,我们只想在data distribution和model distribution之间使用cross-entropy。选择如何表述output于是决定着cross-entropy函数的形式。 任何可以用作output的neural network unit也都可以用作hidden unit。 现在假设前馈神经网络提供了一组hidden features $\boldsymbol{h}=f(\boldsymbol{x};\boldsymbol{\theta})$。output layer就是用来提供从该features到整个任务task 之间额外的变换。 #### 6.2.2.1. Linear Units for Gaussian Output Distributions 一种常见的output unit就是一种基于没有nonlinearity的affine transformation(仿射变换)。也常被称为linear units(线性单元) 给定features $\boldsymbol{h}$,一个linear output units层会产生一个向量$\hat{\boldsymbol{y}}=\boldsymbol{W}^T\boldsymbol{h}+\boldsymbol{b}$ Linear output layers通常被用来产生一个conditional Gaussian distribution的mean: $p(\boldsymbol{y|x})=\mathcal{N}(\boldsymbol{y};\boldsymbol{\hat{y}},\boldsymbol{I}) \space\space\space\space (6.17)$ 最大化log-likelihood于是就与最小化MSE等价了 最大似然的框架使得学习Gaussian covariance变得直观,或者说使得covariance of the Gaussian变成了一个函数。然而covarience对于所有inputs来说都必须是正定矩阵(positive definite matrix)。<span style="color:blue">线性输出层很难满足这些约束条件,因此通常使用其他输出单元来参数化协方差。由于线性单元不会饱和,因此它们对基于梯度的优化算法几乎没有影响,并可用于各种优化算法。</span> #### 6.2.2.2. Sigmoid Units for Bernoulli Output Distributions 许多任务需要预测binary variable $y$的值。很多二分类问题都可以被转化为这种形式。最大似然的方法可以用来定义一个给定$\boldsymbol{x}$下,$y$上的Bernoulli Distribution。Bernoulli分布是通过一个数字来定义的。神经网络只需要预测$P(y=1|\boldsymbol{x})$。为了让这个数 是一个有效的概率,它必须处于区间[0, 1]之间。 满足了这个限定条件之后,假设我们要使用一个 linear unit,且要将其阈值控制在[0, 1]之间,那我们有: $$ P(y=1|\boldsymbol{x})=\max \{0, \min\{1, \boldsymbol{w}^T\boldsymbol{h} + b\}\} \space\space\space\space (6.18) $$ 这实际上定义了一个有效的条件概率,但我们却无法使用梯度下降对其进行有效的训练。任何时候,只要$\boldsymbol{w}^T\boldsymbol{h}+b$位于unit interval之外,那么模型output对其参数的gradient就将是$\boldsymbol{0}$。梯度为$\boldsymbol{0}$是有问题的,因为学习算法将不再能够根据相应的参数进行性能上的提升。 想法,更好的方法是确保,无论何时,只要模型有wrong answer,我们就能有一个strong gradient。该方法是基于sigmoid output units,也就是将sigmoid函数与最大似然相结合。通常sigmoid函数定义为: $\hat{y}=\sigma(\boldsymbol{w}^T\boldsymbol{h}+b)\space\space\space\space (6.19)$ 其中,$\sigma$是logistic sigmoid function 我们可以将sigmoid output unit 想象成有两个components。 1. 它使用一个linear layer来计算$z=\boldsymbol{w}^T\boldsymbol{h}+b$。 2. 接着,它使用sigmoid activation function将$z$转换为一个概率值 我们暂时忽略掉$\boldsymbol{x}$的dependence,只讨论如何用$z$定义一个$y$上的概率分布。sigmoid可以通过构建一个unnormalized probability distribution $\widetilde{P}(y)$,其和不为1。我们将其除以一个合适的常量来获得一个有效的概率分布。如果我们假设unnormalized log probabilities在$y$和$z$上是linear的,那我们可以通过指数运算获得unnormalized probabilities。接着我们normalize,就会发现一个由sigmoidal transformation of $z$控制的Bernoulli distribution: $$ \log\widetilde{P}(y) = yz \space\space\space\space (6.20) \\ \widetilde{P}(y) = \exp(yz) \space\space\space\space (6.21) \\ P(y) = \frac{\exp(yz)}{\Sigma_{y^{'}=0}^1\exp(y^{'}z)} \space\space\space\space (6.22) \\ P(y) = \sigma((2y-1)z) \space\space\space\space (6.23) $$ 基于exponentiation和normalization的概率分布在统计模型文献中非常常见。$z$变量所定义的这种 binary variable上的分布被称作 **logit** 。 这种在log-space上预测概率的方法会让人自然地联想到使用maximum likelihood learning。因为最大似然所使用的cost function就是$-\log(P(y|\boldsymbol{x})$,这里的log可以消掉sigmoid中的指数项。如不这样做,会导致sigmoid函数的饱和。一个由sigmoid参数化的Bernoulli分布的 Maximum likelihood learning的Loss function可以写成: $$ J(\boldsymbol{\theta})=-\log(P(y|\boldsymbol{x})))\space\space\space\space (6.24)\\ =-\log\sigma((2y-1)z)\space\space\space\space (6.25) \\ =\zeta((1-2y)z)\space\space\space\space (6.26) \\ $$ 该推导过程使用了3.10节中的一些性质。通过softplus function将loss重写,我们能看到该函数只有在$(1-2y)z$为very negative的时候才会saturate。因此饱和问题仅当模型有正确的答案的时候才会发生,此时$y=1$且$z$为very negative。或者$y=0$且$z$为very negative。当$z$有错误的正负号,那么softplus函数的argument $(1-2y)z$就可以简化为$|z|$。 随着$|z|$逐渐变大,$z$开始有错误的正负号。softplus函数逐渐趋向于简单地返回其参数$|z|$。随着$|z|$变大,且符号错误,softplus函数会趋向于返回参数$z$。相对于 $z$ 的导数渐近于 $\text{sign}(z)$,因此,在极不正确的 $z$ 的极限情况下,softplus函数根本不会缩小梯度。这一特性 是非常有用的,因为它意味着基于梯度的学习可以快速 纠正错误的 $z$ 当我们使用其他损失函数(如均方误差)时,损失会在 $\sigma(z)$ 时达到饱和。$\sigma(z)$随时饱和。当 $z$ 变为非常负值时,sigmoid 激活函数会饱和到 0 当 $z$ 变为负值时饱和为 0,当 $z$ 变为正值时饱和为 1。每当这种情况发生时,梯度就会收缩得太小,从而对学习无益、 无论模型得到的是正确答案还是错误答案。因此 最大似然法几乎总是训练 sigmoid 输出单元。 从分析角度看,sigmoid 的对数总是有限的,因为 sigmoid 返回的值仅限于开放区间(0,1),而不是使用有效概率的整个封闭区间 [0,1]。在软件实现中,为了避免数值问题,最好将负对数似然写成 $z$ 的函数,而不是 $\hat{y}=\sigma(z)$ 的函数。如果 sigmoid 函数下漂为零,那么取 $\hat{y}$ 的对数就会得到负的概率。 #### 6.2.2.3 多偶数输出分布的 Softmax 单元 任何时候我们都可以使用 Softmax 函数来表示一个离散变量的概率分布。的概率分布时,我们都可以使用 Softmax 函数。这可以看作是 该函数用于表示二进制变量的概率分布。该函数用于表示二进制变量的概率分布。 软最大函数最常用于分类器的输出,以表示 n 个不同类别的概率分布。更罕见的是,软最大函数 如果我们希望模型在 n 个不同类别中选择一个,则可以在模型内部使用软最大函数。n 个不同的内部变量选项之一。 在二进制变量的情况下,我们希望产生一个单一的数字: $\hat{y} = P(y=1|\boldsymbol{x}) \space\space\space\space (6.27)$ 且,我们希望这个数字在0到1之间(即,是一个概率),且我们想要这个概率值的log在gradient-based的log-likelihood优化算法上有较好的表现,我们会选择预测$z=\text{log}\tilde{P}(y=1|x)$。这样进行了指数运算和normalization后会给我们一个由sigmoid函数控制的伯努利分布。 为了将离散的案例泛化成有$n$个值的情况,我们需要生成一个向量$\hat{\boldsymbol{y}}$,其中$\hat{y}_i=P(y=i|\boldsymbol{x})$。我们不仅需要每个$\hat{y}_i$ 的元素都在0到1之间,而且需要整个向量的所有元素之和为1(因为是一个概率)。同样的方法对于Bernoulli泛化成Multinoulli分布也适用。首先需要一个线性层来预测unnormalized log probabilities:$\boldsymbol{z}=\boldsymbol{W}^T\boldsymbol{h}+\boldsymbol{b}\space\space\space\space (6.28)$ 其中$z_i=\text{log}\tilde{P}(y=i|\boldsymbol{x})$。softmax函数之后可以将$\boldsymbol{z}$给exponentiate和normalize从而获得所需要的$\hat{\boldsymbol{y}}$。更正是的softmax函数如下: $$ \text{softmax}(\boldsymbol{z})_i=\frac{\text{exp}(z_i)}{\Sigma_j\text{exp}(z_j)}\space\space\space\space(6.29) $$ 有了logistic sigmoid,对于自然数指数函数的使用通常会起到很好的效果,尤其是在使用max log-likelihood训练softmax输出target value $y$的时候。这种情况下,我们会希望最大化对数$\text{log}P(\mathbb{y}=i; \boldsymbol{z})=\text{log}\space\text{softmax}(\boldsymbol{z})_i$。我们自然而然地会想到要使用自然对数,因为log-likelihood中的log可以抵消掉$\text{softmax}$中的$\text{exp}$项。 $$ \text{log}\space\text{softmax}(\boldsymbol{z})_i=z_i-\text{log}\Sigma_j\text{exp}(z_j)\space\space\space\space(6.30) $$ (6.30)的首项说明input $z_i$总是会对cost function有影响,因为该项无法饱和,所以我们知道“学习”过程得以进行,即使是$z_i$对于第二项的贡献变得非常小。当最大化log-likelihood的时候,第一项会鼓励$z_i$变高,而第二项会鼓励所有的$\boldsymbol{z}$都变低。为了得到一些对于第二项$\text{log}\Sigma_j\text{exp}(z_j)$的直觉,我们可以观察到该项实际上可以通过$\text{max}_jz_j$来估算。<span style="color:blue">这种直觉实际上来自于“负数log-likelihood cost function总是能够很大程度上‘惩罚’最活跃的错误预测”这一事实。</span>如果正确的预测已经对softmax有了最大的input,那么$-z_i$项与$\text{log}\Sigma_j\text{exp}(z_j)\approx \text{max}_jz_j=z_i$项最终基本上可以相互抵消忽略不计的。 到目前为止,我们只讨论了一个例子。总的来说,非规范化最大似然法将驱动模型学习参数,从而驱动 softmax 预测在训练集中观察到的每种结果的计数分数。 $$ \text{softmax}(\boldsymbol{z}(\boldsymbol{x};\boldsymbol{\theta}))_i \approx\frac{\Sigma_{j=1}^{m}\boldsymbol{1}_{y^{(j)}=i,\boldsymbol{x}^{(j)}=\boldsymbol{x}}}{\Sigma_{j=1}^{m}\boldsymbol{1}_{\boldsymbol{x}^{(j)}=\boldsymbol{x}}} \space\space\space\space(6.31) $$ 由于最大似然是 --- 1 1 1 11 1 1 --- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully