机器学习和深度学习

文章目录

机器学习

记号说明:默认 x , w , h , y \mathbf{x},\mathbf{w},\mathbf{h},\mathbf{y} x,w,h,y 表示向量, b , γ , β , α , λ b,\gamma,\beta,\alpha,\lambda b,γ,β,α,λ 表示标量;若写到矩阵,则显式给出维度。

逻辑回归

逻辑回归使用 sigmoid 将线性结果映射到 ( 0 , 1 ) (0,1) (0,1)。

σ ( w ⊤ x + b ) = 1 1 + e − ( w ⊤ x + b ) \begin{align} \sigma(\mathbf{w}^{\top}\mathbf{x}+b)=\frac{1}{1+e^{-(\mathbf{w}^{\top}\mathbf{x}+b)}} \end{align} σ(w⊤x+b)=1+e−(w⊤x+b)1

其中 x ∈ R D × 1 \mathbf{x}\in\mathbb{R}^{D\times1} x∈RD×1 是输入向量, w ∈ R D × 1 \mathbf{w}\in\mathbb{R}^{D\times1} w∈RD×1 是权重向量, b ∈ R b\in\mathbb{R} b∈R 是偏置, σ ( ⋅ ) \sigma(\cdot) σ(⋅) 是 sigmoid 函数。

Softmax 回归

给定 z ∈ R D × 1 \mathbf{z}\in\mathbb{R}^{D\times1} z∈RD×1,softmax 将其映射为类别概率。

s o f t m a x ( z ) i = e z i ∑ j = 1 D e z j \begin{align} \mathrm{softmax}(\mathbf{z})i=\frac{e^{z_i}}{\sum{j=1}^{D}e^{z_j}} \end{align} softmax(z)i=∑j=1Dezjezi

其中 z i z_i zi 是向量 z \mathbf{z} z 的第 i i i 个分量, s o f t m a x ( z ) i \mathrm{softmax}(\mathbf{z})_i softmax(z)i 是第 i i i 类对应的归一化输出。

感知机

感知机输出类别符号,形式为 s g n ( w ⊤ x + b ) \mathrm{sgn}(\mathbf{w}^{\top}\mathbf{x}+b) sgn(w⊤x+b)。

s g n ( w ⊤ x + b ) = { + 1 , w ⊤ x + b > 0 0 , w ⊤ x + b = 0 − 1 , w ⊤ x + b < 0 \begin{align} \mathrm{sgn}(\mathbf{w}^{\top}\mathbf{x}+b)= \begin{cases} +1,&\mathbf{w}^{\top}\mathbf{x}+b>0\\ 0,&\mathbf{w}^{\top}\mathbf{x}+b=0\\ -1,&\mathbf{w}^{\top}\mathbf{x}+b<0 \end{cases} \end{align} sgn(w⊤x+b)=⎩ ⎨ ⎧+1,0,−1,w⊤x+b>0w⊤x+b=0w⊤x+b<0

其中 x ∈ R D × 1 \mathbf{x}\in\mathbb{R}^{D\times1} x∈RD×1 是输入, w ∈ R D × 1 \mathbf{w}\in\mathbb{R}^{D\times1} w∈RD×1 是权重, b ∈ R b\in\mathbb{R} b∈R 是偏置。

SVM

超平面与点到超平面的距离

超平面由 w ⊤ x + b = 0 \mathbf{w}^{\top}\mathbf{x}+b=0 w⊤x+b=0 定义。点 x 0 \mathbf{x}_0 x0 到超平面的距离为其在法向量方向上的投影长度。

d = ∣ w ⊤ x 0 + b ∣ ∥ w ∥ 2 \begin{align} d=\frac{|\mathbf{w}^{\top}\mathbf{x}_0+b|}{\|\mathbf{w}\|_2} \end{align} d=∥w∥2∣w⊤x0+b∣

其中 x 0 ∈ R D × 1 \mathbf{x}_0\in\mathbb{R}^{D\times1} x0∈RD×1 是样本点, w ∈ R D × 1 \mathbf{w}\in\mathbb{R}^{D\times1} w∈RD×1 是超平面法向量, b ∈ R b\in\mathbb{R} b∈R 是偏置, d d d 是点到超平面的欧氏距离。

当 D = 2 D=2 D=2,直线 A x + B y + C = 0 Ax+By+C=0 Ax+By+C=0 的点到直线距离写作:

d = ∣ A x 0 + B y 0 + C ∣ A 2 + B 2 \begin{align} d=\frac{|Ax_0+By_0+C|}{\sqrt{A^2+B^2}} \end{align} d=A2+B2 ∣Ax0+By0+C∣

其中 ( x 0 , y 0 ) (x_0,y_0) (x0,y0) 是平面中的点, A , B , C A,B,C A,B,C 是直线参数。

线性可分:数据集可以被线性模型二分类

给定数据集

D = { ( x ( n ) , y ( n ) ) } n = 1 N \begin{align} \mathcal{D}=\{(\mathbf{x}^{(n)},y^{(n)})\}_{n=1}^{N} \end{align} D={(x(n),y(n))}n=1N

其中 x ( n ) ∈ R D × 1 \mathbf{x}^{(n)}\in\mathbb{R}^{D\times1} x(n)∈RD×1 是第 n n n 个样本, y ( n ) ∈ { − 1 , + 1 } y^{(n)}\in\{-1,+1\} y(n)∈{−1,+1} 是对应标签, N N N 是样本数。

若存在权重 w ∗ \mathbf{w}^{*} w∗ 与偏置 b ∗ b^{*} b∗,使得

y ( n ) ( ( w ∗ ) ⊤ x ( n ) + b ∗ ) > 0 , ∀ n \begin{align} y^{(n)}\left((\mathbf{w}^{*})^{\top}\mathbf{x}^{(n)}+b^{*}\right)>0,\quad \forall n \end{align} y(n)((w∗)⊤x(n)+b∗)>0,∀n

则说明预测与真实值符号一致,数据线性可分。

线性可分的多分类:数据集可以被线性模型多分类

对多分类数据集 D = { ( x ( n ) , y ( n ) ) } n = 1 N \mathcal{D}=\{(\mathbf{x}^{(n)},y^{(n)})\}_{n=1}^{N} D={(x(n),y(n))}n=1N,若采用 one-vs-rest 形式,则对真实类别 c c c 与任意非真实类别 c ^ ≠ c \hat c\neq c c^=c,要求

f c ( x ( n ) ; w ∗ ) > f c ^ ( x ( n ) ; w ∗ ) \begin{align} f_c(\mathbf{x}^{(n)};\mathbf{w}^{*})>f_{\hat c}(\mathbf{x}^{(n)};\mathbf{w}^{*}) \end{align} fc(x(n);w∗)>fc^(x(n);w∗)

其中 f c ( x ; w ∗ ) f_c(\mathbf{x};\mathbf{w}^{*}) fc(x;w∗) 是类别 c c c 的打分函数, c ^ \hat c c^ 表示非 GT 类别, c c c 表示真实类别。

最大间隔原理

样本到分类超平面的间隔定义为

d i = ∣ f ( x i ) ∣ ∥ w ∥ 2 \begin{align} d_i=\frac{|f(\mathbf{x}_i)|}{\|\mathbf{w}\|_2} \end{align} di=∥w∥2∣f(xi)∣

其中 x i \mathbf{x}_i xi 是第 i i i 个样本, f ( x i ) f(\mathbf{x}_i) f(xi) 是该样本的分类函数值, ∥ w ∥ 2 \|\mathbf{w}\|_2 ∥w∥2 是法向量范数。

最小间隔记为

γ = min ⁡ i d i \begin{align} \gamma=\min_i d_i \end{align} γ=imindi

其中 γ \gamma γ 是所有样本中最小的几何间隔。

优化目标写为

max ⁡ w , b γ s.t. y ( n ) ( w ⊤ x ( n ) + b ) ∥ w ∥ 2 ≥ γ , ∀ n \begin{align} \max_{\mathbf{w},b}\ \gamma\quad \text{s.t.}\quad \frac{y^{(n)}(\mathbf{w}^{\top}\mathbf{x}^{(n)}+b)}{\|\mathbf{w}\|_2}\ge \gamma,\ \forall n \end{align} w,bmax γs.t.∥w∥2y(n)(w⊤x(n)+b)≥γ, ∀n

其中 w ∈ R D × 1 \mathbf{w}\in\mathbb{R}^{D\times1} w∈RD×1, b ∈ R b\in\mathbb{R} b∈R, γ \gamma γ 是待最大化的间隔。

若做尺度归一化并令 ∥ w ∥ 2 γ = 1 \|\mathbf{w}\|_2\gamma=1 ∥w∥2γ=1,则可等价写为

max ⁡ w , b 2 ∥ w ∥ 2 s.t. y ( n ) ( w ⊤ x ( n ) + b ) ≥ 1 , ∀ n \begin{align} \max_{\mathbf{w},b}\ \frac{2}{\|\mathbf{w}\|_2}\quad \text{s.t.}\quad y^{(n)}(\mathbf{w}^{\top}\mathbf{x}^{(n)}+b)\ge 1,\ \forall n \end{align} w,bmax ∥w∥22s.t.y(n)(w⊤x(n)+b)≥1, ∀n

其中两条支撑超平面为 w ⊤ x + b = 1 \mathbf{w}^{\top}\mathbf{x}+b=1 w⊤x+b=1 与 w ⊤ x + b = − 1 \mathbf{w}^{\top}\mathbf{x}+b=-1 w⊤x+b=−1,中间的分类超平面为 w ⊤ x + b = 0 \mathbf{w}^{\top}\mathbf{x}+b=0 w⊤x+b=0,间隔宽度为 2 ∥ w ∥ 2 \frac{2}{\|\mathbf{w}\|_2} ∥w∥22。

重点 :支撑向量位于 w ⊤ x + b = ± 1 \mathbf{w}^{\top}\mathbf{x}+b=\pm1 w⊤x+b=±1 上,它们决定最大间隔。

核函数

动机

在线性情形中,我们希望构造参数 w ∗ , b ∗ \mathbf{w}^{*},b^{*} w∗,b∗,使

y ( n ) ( ( w ∗ ) ⊤ x ( n ) + b ∗ ) > 0 , ∀ n \begin{align} y^{(n)}\left((\mathbf{w}^{*})^{\top}\mathbf{x}^{(n)}+b^{*}\right)>0,\quad \forall n \end{align} y(n)((w∗)⊤x(n)+b∗)>0,∀n

其中 x ( n ) ∈ R D × 1 \mathbf{x}^{(n)}\in\mathbb{R}^{D\times1} x(n)∈RD×1。

核映射

考虑映射 ϕ : R D → R M \phi:\mathbb{R}^{D}\rightarrow\mathbb{R}^{M} ϕ:RD→RM,其中 M ≥ 2 M\ge2 M≥2,并令

ϕ ( x ) : R D → R M \begin{align} \phi(\mathbf{x}):\mathbb{R}^{D}\rightarrow\mathbb{R}^{M} \end{align} ϕ(x):RD→RM

其中 ϕ ( x ) ∈ R M × 1 \phi(\mathbf{x})\in\mathbb{R}^{M\times1} ϕ(x)∈RM×1 是高维特征表示。

通过高维映射,希望满足

y ( n ) ( ( w ∗ ) ⊤ ϕ ( x ( n ) ) + b ∗ ) > 0 , ∀ n \begin{align} y^{(n)}\left((\mathbf{w}^{*})^{\top}\phi(\mathbf{x}^{(n)})+b^{*}\right)>0,\quad \forall n \end{align} y(n)((w∗)⊤ϕ(x(n))+b∗)>0,∀n

其中 w ∗ ∈ R M × 1 \mathbf{w}^{*}\in\mathbb{R}^{M\times1} w∗∈RM×1, b ∗ ∈ R b^{*}\in\mathbb{R} b∗∈R。

投影至低维空间 是笔记中的强调点,这里更准确地说,是通过特征映射将数据送入新的特征空间后再线性分割。

注:一般不需要显式写出 ϕ ( x ) \phi(\mathbf{x}) ϕ(x) 的具体形式,常通过"核技巧"来构造。

软间隔与硬间隔

笔记仅做标题提示:软间隔允许少量样本越过间隔或被误分;硬间隔要求所有样本严格满足约束。

前馈神经网络

前馈神经网络(FFN)的单层写法为

z ( l ) = W ( l ) a ( l − 1 ) + b ( l ) \begin{align} \mathbf{z}^{(l)}=\mathbf{W}^{(l)}\mathbf{a}^{(l-1)}+\mathbf{b}^{(l)} \end{align} z(l)=W(l)a(l−1)+b(l)

其中 a ( l − 1 ) ∈ R d l − 1 × 1 \mathbf{a}^{(l-1)}\in\mathbb{R}^{d_{l-1}\times1} a(l−1)∈Rdl−1×1 是上一层激活, W ( l ) ∈ R d l × d l − 1 \mathbf{W}^{(l)}\in\mathbb{R}^{d_l\times d_{l-1}} W(l)∈Rdl×dl−1 是第 l l l 层权重矩阵, b ( l ) ∈ R d l × 1 \mathbf{b}^{(l)}\in\mathbb{R}^{d_l\times1} b(l)∈Rdl×1 是偏置, z ( l ) ∈ R d l × 1 \mathbf{z}^{(l)}\in\mathbb{R}^{d_l\times1} z(l)∈Rdl×1 是线性输出。

对应激活为

a ( l ) = σ ( z ( l ) ) \begin{align} \mathbf{a}^{(l)}=\sigma(\mathbf{z}^{(l)}) \end{align} a(l)=σ(z(l))

其中 σ ( ⋅ ) \sigma(\cdot) σ(⋅) 表示逐元素激活函数。

通用近似定理:使用非线性激活函数的 ANN 可以逼近任意连续函数。反过来,若不使用非线性激活,则多层 ANN 等价于单层 ANN。

激活函数

Logistic

σ ( x ) = 1 1 + e − x \begin{align} \sigma(x)=\frac{1}{1+e^{-x}} \end{align} σ(x)=1+e−x1

其中 x ∈ R x\in\mathbb{R} x∈R 是标量输入, σ ( x ) ∈ ( 0 , 1 ) \sigma(x)\in(0,1) σ(x)∈(0,1)。

tanh

σ ( x ) = e x − e − x e x + e − x \begin{align} \sigma(x)=\frac{e^x-e^{-x}}{e^x+e^{-x}} \end{align} σ(x)=ex+e−xex−e−x

其中 x ∈ R x\in\mathbb{R} x∈R 是输入, σ ( x ) ∈ ( − 1 , 1 ) \sigma(x)\in(-1,1) σ(x)∈(−1,1)。

ReLU

ReLU 是线性修正单元。

σ ( x ) = max ⁡ ( 0 , x ) \begin{align} \sigma(x)=\max(0,x) \end{align} σ(x)=max(0,x)

其中 x ∈ R x\in\mathbb{R} x∈R 是输入, σ ( x ) ≥ 0 \sigma(x)\ge0 σ(x)≥0。

死亡RELU: ∂ σ ∂ f = 0 \frac{\partial \sigma}{\partial f}=0 ∂f∂σ=0,梯度无法传导

若对某一输入始终有 f ( z ; w ∗ , b ∗ ) < 0 f(\mathbf{z};\mathbf{w}^{*},b^{*})<0 f(z;w∗,b∗)<0,则

σ ( f ( z ; w ∗ , b ∗ ) ) = 0 \begin{align} \sigma(f(\mathbf{z};\mathbf{w}^{*},b^{*}))=0 \end{align} σ(f(z;w∗,b∗))=0

其中 z \mathbf{z} z 是输入特征, f ( z ; w ∗ , b ∗ ) f(\mathbf{z};\mathbf{w}^{*},b^{*}) f(z;w∗,b∗) 是线性部分输出。

链式求导时

∂ L ∂ w = ∂ L ∂ σ ( ⋅ ) ⋅ ∂ σ ( ⋅ ) ∂ f ⋅ ∂ f ∂ w \begin{align} \frac{\partial \mathcal{L}}{\partial \mathbf{w}}=\frac{\partial \mathcal{L}}{\partial \sigma(\cdot)}\cdot\frac{\partial \sigma(\cdot)}{\partial f}\cdot\frac{\partial f}{\partial \mathbf{w}} \end{align} ∂w∂L=∂σ(⋅)∂L⋅∂f∂σ(⋅)⋅∂w∂f

其中 L \mathcal{L} L 是损失函数, f f f 是线性输出, ∂ σ ( ⋅ ) ∂ f \frac{\partial \sigma(\cdot)}{\partial f} ∂f∂σ(⋅) 是激活对线性项的导数。

当 σ ( ⋅ ) < 0 \sigma(\cdot)<0 σ(⋅)<0 对应的 ReLU 截断区间起作用时,笔记强调 ∂ σ ∂ f = 0 \frac{\partial \sigma}{\partial f}=0 ∂f∂σ=0,于是参数无法更新,该单元可能"永远死记"。

Leaky ReLU

Leaky ReLU 写为

σ ( x ) = { x , x ≥ 0 β x , x < 0 \begin{align} \sigma(x)= \begin{cases} x,&x\ge0\\ \beta x,&x<0 \end{cases} \end{align} σ(x)={x,βx,x≥0x<0

其中 β \beta β 是负半轴斜率,笔记中特别注明一般取 β = 0.1 \beta=0.1 β=0.1。

它用于缓解负区间导数为零的问题,即在 x < 0 x<0 x<0 时仍有 ∂ σ ( x ) ∂ x ≠ 0 \frac{\partial \sigma(x)}{\partial x}\neq0 ∂x∂σ(x)=0。

Swish

Swish 是带门控的激活函数。

S w i s h ( x ) = x ⋅ σ ( β x ) \begin{align} \mathrm{Swish}(x)=x\cdot\sigma(\beta x) \end{align} Swish(x)=x⋅σ(βx)

其中 x ∈ R x\in\mathbb{R} x∈R 是输入, β \beta β 是可调参数, σ ( β x ) ∈ ( 0 , 1 ) \sigma(\beta x)\in(0,1) σ(βx)∈(0,1) 可视作门控。

SiLU

SiLU(sigmoid linear unit)在笔记中写为

S i L U ( x ) = x ⋅ σ ( x ) \begin{align} \mathrm{SiLU}(x)=x\cdot\sigma(x) \end{align} SiLU(x)=x⋅σ(x)

其中它可视作 β = 1 \beta=1 β=1 时的 Swish。

GELU

笔记中 GELU 写为

G E L U ( x ) = x ⋅ Φ ( x ) \begin{align} \mathrm{GELU}(x)=x\cdot\Phi(x) \end{align} GELU(x)=x⋅Φ(x)

其中 Φ ( x ) \Phi(x) Φ(x) 是高斯分布 N ( 0 , 1 ) \mathcal{N}(0,1) N(0,1) 的累积分布函数, x x x 是输入标量。

FFN 的改进:门控线性单元

GLU

FFN 的改进方向之一是门控线性单元 GLU。笔记中给出的中间变量为

h = ( W 1 x ) ⊙ σ ( V x ) \begin{align} \mathbf{h}=(\mathbf{W}_1\mathbf{x})\odot \sigma(\mathbf{V}\mathbf{x}) \end{align} h=(W1x)⊙σ(Vx)

其中 x ∈ R D 1 × 1 \mathbf{x}\in\mathbb{R}^{D_1\times1} x∈RD1×1 是输入, W 1 ∈ R D 2 × D 1 \mathbf{W}_1\in\mathbb{R}^{D_2\times D_1} W1∈RD2×D1 与 V ∈ R D 2 × D 1 \mathbf{V}\in\mathbb{R}^{D_2\times D_1} V∈RD2×D1 是线性映射矩阵, ⊙ \odot ⊙ 是逐元素乘法, σ ( V x ) \sigma(\mathbf{V}\mathbf{x}) σ(Vx) 是门控激活。

输出层为

o = W 2 h + b 3 \begin{align} \mathbf{o}=\mathbf{W}_2\mathbf{h}+\mathbf{b}_3 \end{align} o=W2h+b3

其中 W 2 ∈ R D 3 × D 2 \mathbf{W}_2\in\mathbb{R}^{D_3\times D_2} W2∈RD3×D2, h ∈ R D 2 × 1 \mathbf{h}\in\mathbb{R}^{D_2\times1} h∈RD2×1, o ∈ R D 3 × 1 \mathbf{o}\in\mathbb{R}^{D_3\times1} o∈RD3×1, b 3 ∈ R D 3 × 1 \mathbf{b}_3\in\mathbb{R}^{D_3\times1} b3∈RD3×1。

对比普通 FFN,笔记中写出:

W 1 ∈ R D 2 × D 1 , W 2 ∈ R D 3 × D 2 \begin{align} \mathbf{W}_1\in\mathbb{R}^{D_2\times D_1},\quad \mathbf{W}_2\in\mathbb{R}^{D_3\times D_2} \end{align} W1∈RD2×D1,W2∈RD3×D2

其中通常取 D 2 > D 1 D_2>D_1 D2>D1。

对 GLU,笔记额外强调:

V , W 1 ∈ R D 2 × D 1 , W 2 ∈ R D 3 × D 2 \begin{align} \mathbf{V},\mathbf{W}_1\in\mathbb{R}^{D_2\times D_1},\quad \mathbf{W}_2\in\mathbb{R}^{D_3\times D_2} \end{align} V,W1∈RD2×D1,W2∈RD3×D2

其中 V \mathbf{V} V 与 W 1 \mathbf{W}_1 W1 分别提供门控分支与主分支。

Swish GLU

笔记把 FFN 的门控激活替换成 Swish,写作

S w i s h G L U ( x ) = W 2 ( ( W 1 x + b 1 ) ⊙ S w i s h ( V x + b 2 ) ) + b 3 \begin{align} \mathrm{SwishGLU}(\mathbf{x})=\mathbf{W}_2\big((\mathbf{W}_1\mathbf{x}+\mathbf{b}_1)\odot \mathrm{Swish}(\mathbf{V}\mathbf{x}+\mathbf{b}_2)\big)+\mathbf{b}_3 \end{align} SwishGLU(x)=W2((W1x+b1)⊙Swish(Vx+b2))+b3

其中 x ∈ R D 1 × 1 \mathbf{x}\in\mathbb{R}^{D_1\times1} x∈RD1×1, W 1 , V ∈ R D 2 × D 1 \mathbf{W}_1,\mathbf{V}\in\mathbb{R}^{D_2\times D_1} W1,V∈RD2×D1, W 2 ∈ R D 3 × D 2 \mathbf{W}_2\in\mathbb{R}^{D_3\times D_2} W2∈RD3×D2, b 1 , b 2 ∈ R D 2 × 1 \mathbf{b}_1,\mathbf{b}_2\in\mathbb{R}^{D_2\times1} b1,b2∈RD2×1, b 3 ∈ R D 3 × 1 \mathbf{b}_3\in\mathbb{R}^{D_3\times1} b3∈RD3×1。

GEGLU

对应地,GEGLU 写为

G E G L U ( x ) = W 2 ( ( W 1 x + b 1 ) ⊙ G E L U ( V x + b 2 ) ) + b 3 \begin{align} \mathrm{GEGLU}(\mathbf{x})=\mathbf{W}_2\big((\mathbf{W}_1\mathbf{x}+\mathbf{b}_1)\odot \mathrm{GELU}(\mathbf{V}\mathbf{x}+\mathbf{b}_2)\big)+\mathbf{b}_3 \end{align} GEGLU(x)=W2((W1x+b1)⊙GELU(Vx+b2))+b3

其中各符号含义与 Swish GLU 相同,只是门控函数换为 GELU。

Maxout


卷积神经网络

归纳偏置:局部性+ 平移不变性

卷积定义

给定序列 x \mathbf{x} x 与滤波器 w \mathbf{w} w,输出写作 y = w ∗ x y=\mathbf{w}\ast\mathbf{x} y=w∗x。

若把输入记作矩阵 X ∈ R m × n \mathbf{X}\in\mathbb{R}^{m\times n} X∈Rm×n,卷积核记作 W ∈ R u × v \mathbf{W}\in\mathbb{R}^{u\times v} W∈Ru×v,则输出 Y ∈ R m ′ × n ′ \mathbf{Y}\in\mathbb{R}^{m'\times n'} Y∈Rm′×n′。

卷积超参数包含步长 stride S h , S w S_h,S_w Sh,Sw,填充 padding P h , P w P_h,P_w Ph,Pw,卷积核大小 u × v u\times v u×v。若 X ∈ R M × N \mathbf{X}\in\mathbb{R}^{M\times N} X∈RM×N,则输出尺寸为

M ′ = ⌊ M − u + 2 P h S h ⌋ + 1 \begin{align} M'=\left\lfloor\frac{M-u+2P_h}{S_h}\right\rfloor+1 \end{align} M′=⌊ShM−u+2Ph⌋+1

其中 M M M 是输入高, u u u 是卷积核高, P h P_h Ph 是纵向 padding, S h S_h Sh 是纵向步长, M ′ M' M′ 是输出高。

N ′ = ⌊ N − v + 2 P w S w ⌋ + 1 \begin{align} N'=\left\lfloor\frac{N-v+2P_w}{S_w}\right\rfloor+1 \end{align} N′=⌊SwN−v+2Pw⌋+1

其中 N N N 是输入宽, v v v 是卷积核宽, P w P_w Pw 是横向 padding, S w S_w Sw 是横向步长, N ′ N' N′ 是输出宽。

窄卷积: S = 1 , P = 0 S=1,P=0 S=1,P=0。宽卷积: S = 1 , P = K − 1 S=1,P=K-1 S=1,P=K−1。等宽卷积满足 K = 2 P + 1 K=2P+1 K=2P+1,此时输入与输出尺寸不变。

CNN 结构

卷积层提取局部特征,池化层汇聚不变信息。笔记中的结构图为:

输入 -> 卷积 -> 池化 -> 规化/汇聚 -> 输出

其中卷积负责特征图的升降维,池化负责压缩信息,末端常带残差连接。

池化

局部感知中写了两类典型池化:

  • K × K × K\times K\times K×K× mean:对局部区域做平均池化。
  • global mean:全局平均池化。

卷积种类

转置卷积

转置卷积被标注为"逆操作"。笔记中写为

Y = W X = C X \begin{align} \mathbf{Y}=\mathbf{W}\mathbf{X}=\mathbf{C}\mathbf{X} \end{align} Y=WX=CX

其中 X ∈ R m × 1 \mathbf{X}\in\mathbb{R}^{m\times1} X∈Rm×1 是输入展平向量, Y ∈ R k × 1 \mathbf{Y}\in\mathbb{R}^{k\times1} Y∈Rk×1 是输出, C \mathbf{C} C 表示由卷积核诱导出的稀疏矩阵。

与之对应,转置卷积可写作

X = C ⊤ Y \begin{align} \mathbf{X}=\mathbf{C}^{\top}\mathbf{Y} \end{align} X=C⊤Y

其中 C ⊤ \mathbf{C}^{\top} C⊤ 把低分辨率特征重新映射回高分辨率空间。

空洞卷积

空洞卷积通过在卷积核内部插入空洞来扩大感受野。

深度可分离卷积

按照空间 + 通道 两步拆分。

先做 depthwise 卷积。若输入 X ∈ R M × N × C \mathbf{X}\in\mathbb{R}^{M\times N\times C} X∈RM×N×C,则对每个通道 c ∈ { 1 , ... , C } c\in\{1,\dots,C\} c∈{1,...,C},使用核 W ∈ R K × K × C \mathbf{W}\in\mathbb{R}^{K\times K\times C} W∈RK×K×C 分别卷积,得到

X ′ ∈ R M ′ × N ′ × C \begin{align} \mathbf{X}'\in\mathbb{R}^{M'\times N'\times C} \end{align} X′∈RM′×N′×C

其中 C C C 是通道数, K × K K\times K K×K 是空间卷积核, X ′ \mathbf{X}' X′ 是逐通道卷积后的结果。

再做 pointwise 映射。使用

W ′ ∈ R 1 × 1 × D \begin{align} \mathbf{W}'\in\mathbb{R}^{1\times1\times D} \end{align} W′∈R1×1×D

对通道进行线性组合,得到

Y ∈ R M ′ × N ′ × D \begin{align} \mathbf{Y}\in\mathbb{R}^{M'\times N'\times D} \end{align} Y∈RM′×N′×D

其中 D D D 是输出通道数。笔记最后强调:分离的是空间域与通道域。

循环神经网络

循环神经网络用于处理序列。

递推关系写为

h t = σ ( U h t − 1 + W x t ) \begin{align} \mathbf{h}t=\sigma(\mathbf{U}\mathbf{h}{t-1}+\mathbf{W}\mathbf{x}_t) \end{align} ht=σ(Uht−1+Wxt)

其中 x t ∈ R D x × 1 \mathbf{x}t\in\mathbb{R}^{D_x\times1} xt∈RDx×1 是时刻 t t t 的输入, h t − 1 ∈ R D h × 1 \mathbf{h}{t-1}\in\mathbb{R}^{D_h\times1} ht−1∈RDh×1 是上一时刻隐藏状态, U ∈ R D h × D h \mathbf{U}\in\mathbb{R}^{D_h\times D_h} U∈RDh×Dh 是循环权重, W ∈ R D h × D x \mathbf{W}\in\mathbb{R}^{D_h\times D_x} W∈RDh×Dx 是输入权重, σ ( ⋅ ) \sigma(\cdot) σ(⋅) 是激活函数。

输出为

y t = V h t \begin{align} \mathbf{y}_t=\mathbf{V}\mathbf{h}_t \end{align} yt=Vht

其中 V ∈ R D y × D h \mathbf{V}\in\mathbb{R}^{D_y\times D_h} V∈RDy×Dh, y t ∈ R D y × 1 \mathbf{y}_t\in\mathbb{R}^{D_y\times1} yt∈RDy×1。

理论上,给定序列 x 1 , x 2 , ... , x N \mathbf{x}_1,\mathbf{x}_2,\dots,\mathbf{x}_N x1,x2,...,xN,可以递推出 h 1 , h 2 , ... , h N \mathbf{h}_1,\mathbf{h}_2,\dots,\mathbf{h}_N h1,h2,...,hN,并进一步得到 y 1 , y 2 , ... , y N \mathbf{y}_1,\mathbf{y}_2,\dots,\mathbf{y}_N y1,y2,...,yN。

应用

  • 序列分类: x 1 : N → h 1 : N → h N → y N \mathbf{x}{1:N}\rightarrow \mathbf{h}{1:N}\rightarrow \mathbf{h}_N\rightarrow \mathbf{y}_N x1:N→h1:N→hN→yN
  • 序列标记: x 1 : N → h 1 : N → y 1 : N \mathbf{x}{1:N}\rightarrow \mathbf{h}{1:N}\rightarrow \mathbf{y}_{1:N} x1:N→h1:N→y1:N
  • 自回归: x 1 : N → h 1 : N → y N + 1 = x N + 1 \mathbf{x}{1:N}\rightarrow \mathbf{h}{1:N}\rightarrow y_{N+1}=x_{N+1} x1:N→h1:N→yN+1=xN+1,再递推得到后续预测
  • 条件生成:如条件回归、机器翻译

RNN 问题:长程依赖,以及梯度爆炸/消失。差异形式 被特别标记。

LSTM

LSTM 通过三门一候选内部状态控制记忆流动。

遗忘门:

f t = σ ( W f x t + U f h t − 1 + b f ) \begin{align} \mathbf{f}_t=\sigma(\mathbf{W}_f\mathbf{x}_t+\mathbf{U}f\mathbf{h}{t-1}+\mathbf{b}_f) \end{align} ft=σ(Wfxt+Ufht−1+bf)

其中 f t ∈ R D h × 1 \mathbf{f}_t\in\mathbb{R}^{D_h\times1} ft∈RDh×1 是遗忘门, W f ∈ R D h × D x \mathbf{W}_f\in\mathbb{R}^{D_h\times D_x} Wf∈RDh×Dx, U f ∈ R D h × D h \mathbf{U}_f\in\mathbb{R}^{D_h\times D_h} Uf∈RDh×Dh, b f ∈ R D h × 1 \mathbf{b}_f\in\mathbb{R}^{D_h\times1} bf∈RDh×1。

输入门:

i t = σ ( W i x t + U i h t − 1 + b i ) \begin{align} \mathbf{i}_t=\sigma(\mathbf{W}_i\mathbf{x}_t+\mathbf{U}i\mathbf{h}{t-1}+\mathbf{b}_i) \end{align} it=σ(Wixt+Uiht−1+bi)

其中 i t ∈ R D h × 1 \mathbf{i}_t\in\mathbb{R}^{D_h\times1} it∈RDh×1 是输入门。

输出门:

o t = σ ( W o x t + U o h t − 1 + b o ) \begin{align} \mathbf{o}_t=\sigma(\mathbf{W}_o\mathbf{x}_t+\mathbf{U}o\mathbf{h}{t-1}+\mathbf{b}_o) \end{align} ot=σ(Woxt+Uoht−1+bo)

其中 o t ∈ R D h × 1 \mathbf{o}_t\in\mathbb{R}^{D_h\times1} ot∈RDh×1 是输出门。

候选状态:

c ~ t = σ ( W c x t + U c h t − 1 + b c ) \begin{align} \tilde{\mathbf{c}}_t=\sigma(\mathbf{W}_c\mathbf{x}_t+\mathbf{U}c\mathbf{h}{t-1}+\mathbf{b}_c) \end{align} c~t=σ(Wcxt+Ucht−1+bc)

其中 c ~ t ∈ R D h × 1 \tilde{\mathbf{c}}_t\in\mathbb{R}^{D_h\times1} c~t∈RDh×1 是候选记忆。

内部状态更新:

c t = f t ⊙ c t − 1 + i t ⊙ c ~ t \begin{align} \mathbf{c}_t=\mathbf{f}t\odot \mathbf{c}{t-1}+\mathbf{i}_t\odot \tilde{\mathbf{c}}_t \end{align} ct=ft⊙ct−1+it⊙c~t

其中 c t − 1 , c t ∈ R D h × 1 \mathbf{c}_{t-1},\mathbf{c}_t\in\mathbb{R}^{D_h\times1} ct−1,ct∈RDh×1 分别是前一时刻和当前时刻的记忆状态。

隐藏状态更新:

h t = o t ⊙ tanh ⁡ ( c t ) \begin{align} \mathbf{h}_t=\mathbf{o}_t\odot\tanh(\mathbf{c}_t) \end{align} ht=ot⊙tanh(ct)

其中 h t ∈ R D h × 1 \mathbf{h}_t\in\mathbb{R}^{D_h\times1} ht∈RDh×1 是输出隐藏状态。

重点:长短期记忆通过遗忘门、输入门、输出门控制记忆流动。

GRU

笔记强调 GRU 用更简化的门控结构替代 LSTM 的部分机制,保留候选门与重置门的思路。总结写为:

  • LSTM:结构更复杂,表达细致。
  • GRU:结构更简单,训练更容易。

并注明:RNN 中隐藏状态 h t h_t ht 随时间不断更新,容易出现梯度问题。

BiLSTM

BiLSTM 即双向 LSTM,同时使用正向与反向序列信息。

若正向隐藏状态为 h → t \overrightarrow{\mathbf{h}}_t h t,反向隐藏状态为 h ← t \overleftarrow{\mathbf{h}}_t h t,则输出可由两者联合构成。笔记主要保留了结构图,没有额外展开公式。

自回归生成

训练阶段,给定输入序列与输出序列,笔记特别标出 教师强制(teacher forcing)

优点:并行、高效。

缺点:误差累积。

笔记中用条件概率形式提示暴露偏差问题,可概括为:训练时模型依赖真实历史,而测试时依赖自身历史预测,二者分布不一致。

网络优化

批大小

小批量训练是默认设置。

mini-batch 梯度下降可看成在稳定性与效率之间折中。

笔记中写到:

  • 线性缩放:batch size 增大时,训练更稳定,学习率也可相应增大。
  • 梯度累积:把一个大 batch 拆成多个 mini-batch 累加。

学习率

余弦退火

α t = cos ⁡ ( π t T ) + 1 2 α 0 \begin{align} \alpha_t=\frac{\cos\left(\pi\frac{t}{T}\right)+1}{2}\alpha_0 \end{align} αt=2cos(πTt)+1α0

其中 t t t 是当前训练步, T T T 是总调度长度, α 0 \alpha_0 α0 是初始学习率, α t \alpha_t αt 是第 t t t 步的学习率。

热身

当 t < T ′ t<T' t<T′ 时,笔记中写为线性 warmup:

α t = t T ′ α 0 , if t < T ′ \begin{align} \alpha_t=\frac{t}{T'}\alpha_0,\quad \text{if } t<T' \end{align} αt=T′tα0,if t<T′

其中 T ′ T' T′ 是 warmup 持续步数。

初始化

  • Xavier 初始化
  • Kaiming 初始化

数据预处理

设数据集为 { x ( n ) } n = 1 N \{\mathbf{x}^{(n)}\}_{n=1}^{N} {x(n)}n=1N。

Z-score

x ( i ) ′ = x ( i ) − μ σ \begin{align} x^{(i)\prime}=\frac{x^{(i)}-\mu}{\sigma} \end{align} x(i)′=σx(i)−μ

其中 x ( i ) x^{(i)} x(i) 是第 i i i 个特征, μ \mu μ 是该特征的均值, σ \sigma σ 是该特征的标准差, x ( i ) ′ x^{(i)\prime} x(i)′ 是标准化后的特征。

Min-max

x ( i ) ′ = x ( i ) − min ⁡ n x ( n ) max ⁡ n x ( n ) − min ⁡ n x ( n ) \begin{align} x^{(i)\prime}=\frac{x^{(i)}-\min_n x^{(n)}}{\max_n x^{(n)}-\min_n x^{(n)}} \end{align} x(i)′=maxnx(n)−minnx(n)x(i)−minnx(n)

其中 min ⁡ n x ( n ) \min_n x^{(n)} minnx(n) 与 max ⁡ n x ( n ) \max_n x^{(n)} maxnx(n) 分别是该特征在样本维度上的最小值和最大值,笔记旁注说明它常把数据缩放到某一固定分布区间。

白化

白化(whitening)用于去除冗余数据、去除特征相关性。

例:PCA 可用于保留高方差方向,同时去除冗余并做降维。

归一化

归一化的动机包括:

  • 内部协变量偏移
  • 优化曲面更平滑
  • 缓解梯度爆炸
  • 分布变化过大时利于收敛

给定张量 z ∈ R B × L × D \mathbf{z}\in\mathbb{R}^{B\times L\times D} z∈RB×L×D:

BatchNorm

B N ( γ , β ) = γ ⊙ x − μ σ + β \begin{align} \mathrm{BN}(\gamma,\beta)=\gamma\odot\frac{\mathbf{x}-\mu}{\sigma}+\beta \end{align} BN(γ,β)=γ⊙σx−μ+β

其中 x \mathbf{x} x 是输入张量, μ , σ \mu,\sigma μ,σ 在 batch 维度上统计, γ , β \gamma,\beta γ,β 是可学习的缩放与平移参数。

LayerNorm

L N ( γ , β ) = γ ⊙ x − μ σ + β \begin{align} \mathrm{LN}(\gamma,\beta)=\gamma\odot\frac{\mathbf{x}-\mu}{\sigma}+\beta \end{align} LN(γ,β)=γ⊙σx−μ+β

其中 μ , σ \mu,\sigma μ,σ 在特征维度上统计,笔记中特别写到"特征 ( D ) (D) (D) 维度"。

RMSNorm

R M S N o r m ( γ ) = γ ⊙ x ∥ x ∥ 2 \begin{align} \mathrm{RMSNorm}(\gamma)=\gamma\odot\frac{\mathbf{x}}{\|\mathbf{x}\|_2} \end{align} RMSNorm(γ)=γ⊙∥x∥2x

其中 ∥ x ∥ 2 \|\mathbf{x}\|_2 ∥x∥2 表示按特征维计算的二范数, γ \gamma γ 是缩放参数。笔记总结其优点为:速度较快、效率较高。

技巧 :激活层后可做 pre-norm;在注意力(Attn)和 FFN 前也可做 pre-norm

正则化

L p L_p Lp 正则化

正则项写为 λ ∥ θ ∥ p \lambda\|\theta\|_p λ∥θ∥p,用于约束参数规模。

权重衰减

θ t = ( 1 − β ) θ t − 1 − α t g t \begin{align} \theta_t=(1-\beta)\theta_{t-1}-\alpha_t g_t \end{align} θt=(1−β)θt−1−αtgt

其中 θ t \theta_t θt 是当前参数, θ t − 1 \theta_{t-1} θt−1 是上一步参数, g t g_t gt 是当前梯度, α t \alpha_t αt 是学习率, β \beta β 是衰减系数。笔记中注明通常 α , β ≪ 1 \alpha,\beta\ll1 α,β≪1,例如 β \beta β 可取约 10 − 3 10^{-3} 10−3 量级。

数据增强

普通增强方式包括:旋转、平移、缩放、噪音、抖动、色彩变化。

mixup

x ^ = λ x i + ( 1 − λ ) x j \begin{align} \hat{\mathbf{x}}=\lambda \mathbf{x}_i+(1-\lambda)\mathbf{x}_j \end{align} x^=λxi+(1−λ)xj

其中 x i , x j \mathbf{x}_i,\mathbf{x}_j xi,xj 是两个样本输入, λ ∈ 0 , 1 \lambda\in0,1 λ∈0,1 是混合系数, x ^ \hat{\mathbf{x}} x^ 是混合后的输入。

y ^ = λ y i + ( 1 − λ ) y j \begin{align} \hat{\mathbf{y}}=\lambda \mathbf{y}_i+(1-\lambda)\mathbf{y}_j \end{align} y^=λyi+(1−λ)yj

其中 y i , y j \mathbf{y}_i,\mathbf{y}_j yi,yj 是对应标签向量, y ^ \hat{\mathbf{y}} y^ 是混合后的软标签。笔记中强调它会"混合,平滑边界"。

CutMix

CutMix 被记为"局部替换",即把一个样本的局部区域替换为另一样本的局部区域,并同步混合标签。

概率校正

标签平滑

硬标签写为 one-hot,例如 0 , 0 , ... , 1 , ... , 0 0,0,\\dots,1,\\dots,0 0,0,...,1,...,0。软标签写为

y soft = α , ε K − 1 , ε K − 1 , ... , ε K − 1 , 1 − ε \begin{align} \mathbf{y}_{\text{soft}}=\left\\alpha,\\frac{\\varepsilon}{K-1},\\frac{\\varepsilon}{K-1},\\dots,\\frac{\\varepsilon}{K-1},1-\\varepsilon\\right \end{align} ysoft=α,K−1ε,K−1ε,...,K−1ε,1−ε

其中 K K K 是类别数, ε \varepsilon ε 是平滑系数, α \alpha α 表示非目标类中被分配到的平滑质量, 1 − ε 1-\varepsilon 1−ε 是目标类概率。

笔记中的解释是:这样可以避免过拟合,使输出不再是 0,同时不过度置信。

对比:传统硬标签教网络"死题";平滑标签更接近"考生"式的软性认知。

相关推荐
许泽宇的技术分享1 小时前
我拆了一个 Agent 仓库,发现真正难的从来不是让 AI 会写代码
人工智能
webor20061 小时前
<六>ChatGPT到底叫什么?——语言模型
人工智能·ai·语言模型·chatgpt·claude
dunge20261 小时前
ChatGPT Plus / Pro + Codex 技术深度解析:从 API 集成到性能优化与实战案例
人工智能·chatgpt
ʜᴇɴʀʏ1 小时前
ICCV 2025 | STEP-DETR:基于超级教师与伪标签引导文本查询的半监督目标检测
人工智能·目标检测·计算机视觉·transformer
必须会一定会1 小时前
用纯 HTML/JS 做一个 AI 需求澄清器:把模糊想法转换成可执行任务书
开发语言·前端·javascript·人工智能·html·ai编程
HyperAI超神经1 小时前
基于 Strassen 与 LCMA 低复杂度矩阵乘,腾讯 FalconGEMM 探索超越硬件峰值的矩阵乘优化
人工智能·线性代数·矩阵·智能体·推理·ai编译器
DeepIntelli1 小时前
品牌百科词条建设:从词条命名到提交的完整技术指南
人工智能
TAN-90°-1 小时前
Deep Learning for Computer Vision——Image Classification with Linear Classifiers
python·深度学习·算法·计算机视觉·线性回归
MindUp1 小时前
企业网盘选型的技术评估维度与主流产品架构简析
人工智能·安全·架构