Gaussian Process Regression
RL实验跑多了,理论就基本忘光光了,只剩下intuitive的视角,本想着用贝叶斯优化来调超参亦或者指导RL中的探索,但第一个门槛是对高斯过程的直觉理解一点都不深入。看着网上许多高斯过程回归的竟然看不太懂,然后还是自己写写吧。主要浓缩自2006年的参考书目Gaussian Process for Machine Learning的Chapter 2以及一篇英文blog visual gaussian process
概述
首先一个监督问题有三个对象: 数据 X X X、模型(结构与参数 W W W)、标签 Y Y Y,一般有数据集 { X , Y } \{X,Y\} {X,Y},然后我们需要选择模型结构(线性linear、平滑smoothing、凸凹等特性)以及学习算法(MLE、MAP)来得到参数 W W W.
因此解决监督问题一般有两个方向:
- 限制模型结构的选择(比如选线性、平滑性好的模型结构)
- 对所有可能的模型结构设定一个prior(希望能从data中学习适应自身结构的模型结构,如适合的给high probability)
对于第一个方向,诸如L1或L2等regularization的方式都属于此类,但因为constraint的存在,容易出现overfitting的现象,需要richness of function class来寻找更适合Data自身结构的模型
对于第二个方向,所有可能的模型结构(function class)是无穷多个的,阿这?咋搞?这时候就搬来了Gaussian Process。这也就是说高斯过程是由定义在连续域上的服从高斯分布的无穷维随机变量组成的。
多元高斯分布的定义
一个n维随机向量 X \mathbf{X} X服从多元高斯分布,均值向量 μ \mu μ决定了分布的中心,协方差矩阵 Σ \Sigma Σ决定了分布的形状且反映了各维度变量之间的关系:
X = [ X 1 X 2 ⋮ X n ] ∼ N ( μ , Σ ) w h e r e X i ∼ N ( μ i , Σ i ) Σ = C o v ( X i , X j ) = E [ ( X i − μ i ) ( X j − μ j ) ] \mathbf{X} =\left[ \begin{matrix} X_1\\ X_2\\ \vdots\\ X_n \end{matrix}\right] \sim \mathcal N(\mu, \Sigma)\\ where \quad X_i\sim \mathcal N(\mu_i,\Sigma_i)\\ \Sigma =Cov(X_i,X_j)=E[(X_i-\mu_i)(X_j-\mu_j)] X=⎣⎢⎢⎢⎡X1X2⋮Xn⎦⎥⎥⎥⎤∼N(μ,Σ)whereXi∼N(μi,Σi)Σ=Cov(Xi,Xj)=E[(Xi−μi)(Xj−μj)]
多元高斯分布具有下述的性质:
边际分布: X ∼ N ( μ X , Σ X ) , Y ∼ N ( μ Y , Σ Y ) X\sim \mathcal N(\mu_X,\Sigma_X), Y\sim\mathcal N(\mu_Y,\Sigma_Y) X∼N(μX,ΣX),Y∼N(μY,ΣY)
联合分布为:
P X , Y = P ( x , y ) = [ X Y ] ∼ N ( [ μ X μ Y ] , [ Σ X X Σ X Y Σ Y X Σ Y Y ] ) P_{X,Y}=P(x,y)=\left [\begin{matrix} X\\ Y \end{matrix}\right]\sim \mathcal N\left( \left[\begin{matrix}\mu_X\\\mu_Y\end{matrix}\right],\left[\begin{matrix}\Sigma_{XX}& \Sigma_{XY}\\ \Sigma_{YX} & \Sigma_{YY}\end{matrix}\right] \right) PX,Y=P(x,y)=[XY]∼N([μXμY],[ΣXXΣYXΣXYΣYY])
条件分布:
X ∣ Y ∼ N ( μ X + Σ X Y Σ Y Y − 1 ( Y − μ Y ) , Σ X X − Σ X Y Σ Y Y − 1 Σ Y X ) Y ∣ X ∼ N ( μ Y + Σ Y X Σ X X − 1 ( X − μ X ) , Σ Y Y − Σ Y X Σ X X − 1 Σ X Y ) \begin{array}{l} X \mid Y \sim \mathcal{N}\left(\mu_{X}+\Sigma_{X Y} \Sigma_{Y Y}^{-1}\left(Y-\mu_{Y}\right), \Sigma_{X X}-\Sigma_{X Y} \Sigma_{Y Y}^{-1} \Sigma_{Y X}\right) \\ Y \mid X \sim \mathcal{N}\left(\mu_{Y}+\Sigma_{Y X} \Sigma_{X X}^{-1}\left(X-\mu_{X}\right), \Sigma_{Y Y}-\Sigma_{Y X} \Sigma_{X X}^{-1} \Sigma_{X Y}\right) \end{array} X∣Y∼N(μX+ΣXYΣYY−1(Y−μY),ΣXX−ΣXYΣYY−1ΣYX)Y∣X∼N(μY+ΣYXΣXX−1(X−μX),ΣYY−ΣYXΣXX−1ΣXY)
需要明确的是,一般来说这里的随机变量是定义在维度dimension上的,即一般是属性attribute(如影响商品价格的attribute有质量 X 1 X_1 X1,供求比 X 2 X_2 X2,成本 X 3 X_3 X3等),所以价格服从有限多元的联合高斯分布。
高斯过程
定义如下:
一个高斯过程是无限多个服从高斯分布随机变量的集合,从该集合中随意选取的有限个随机变量服从自身的联合高斯分布。
假设取三个随机变量
i
,
j
,
k
i,j,k
i,j,k即
X
1
,
.
.
.
X
n
,
n
→
∞
,
∀
i
,
j
,
k
∈
n
,
P
X
i
,
X
j
,
X
k
∼
N
(
μ
i
j
k
,
Σ
i
j
k
)
X_1,...X_n,n\rightarrow \infty,\forall i,j,k\in n,P_{X_i,X_j,X_k}\sim \mathcal N(\mu_{ijk},\Sigma_{ijk})
X1,...Xn,n→∞,∀i,j,k∈n,PXi,Xj,Xk∼N(μijk,Σijk),因此从数学意义上,无限个随机变量
{
X
n
}
n
=
1
n
→
∞
\{X_n\}_{n=1}^{n\rightarrow\infty}
{Xn}n=1n→∞可以看作是多元高斯分布的维度dim拉长到无限,从函数的角度用
f
(
x
)
f(x)
f(x)进行代替,描述为:
f ( x ) ∼ G P ( m ( x ) , k ( x , x ′ ) ) w h e r e m ( x ) = E [ f ( x ) ] k ( x , x ′ ) = E [ ( f ( x ) − m ( x ) ) ( f ( x ′ ) − m ( x ′ ) ) ] (*) \begin{aligned} f(\mathbf{x})&\sim \mathcal{GP}\left(m(\mathbf{x}),k(\mathbf{x},\mathbf{x}')\right)\\ where \quad m(\mathbf{x})&=\mathbb E[f(\mathbf x)]\\ k(\mathbf{x,x'})&=\mathbb E[(f(\mathbf{x})-m(\mathbf{x}))(f(\mathbf x')-m(\mathbf x'))]\tag{*} \end{aligned} f(x)wherem(x)k(x,x′)∼GP(m(x),k(x,x′))=E[f(x)]=E[(f(x)−m(x))(f(x′)−m(x′))](*)
尽管数学意义上是有限维度到无限维度上的拓展,但实际使用的时候,随机变量的含义就发生了改变。使用多元高斯分布时,一个随机变量是一个维度上的高斯分布,因此可以将一维度对应一个attribute,维度是离散的;但高斯过程是定义在连续域上的,一个随机变量是连续域上的一个值,维度是连续的。最常见的情况,随机变量的index所在的连续域是时间。
这里初看定义,还是有点懵,直觉有阻碍,下面从两个角度阐释高斯过程。
- Weight-Space View即从模型的参数 W W W的角度出发,看待高斯过程
- Function-space View即把高斯过程看作是函数的分布,从函数空间的角度进行推断
一、Weight-space View
1.1 标准贝叶斯线性模型
此处回顾一下标准的多元线性回归模型 + 贝叶斯
-
有 n n n个样本的数据集 D = { ( x i , y i ) } i = 1 n \mathcal D=\{(\mathbf{x_i},y_i)\}_{i=1}^n D={(xi,yi)}i=1n,其中 x i ∈ R D , y i ∈ R x_i \in \mathbb R^D,y_i\in \mathbb R xi∈RD,yi∈R,输入矩阵形式为 X ∈ R D × n X\in \mathbb R^{D\times n} X∈RD×n,输出targets向量为 y ∈ R n \mathbf{y}\in \mathbb R^n y∈Rn.
这里输入矩阵的表示,使得一个样本是一列. -
选择标准的线性模型(模型结构为线性,并受取决于样本的高斯噪声扰动)
f ( x ) = x T w , y = f ( x ) + ϵ , ϵ ∼ N ( 0 , σ n 2 ) f(\mathbf x)=\mathbf x^T\mathbf w, \qquad y=f(\mathbf x)+\epsilon, \quad \epsilon\sim \mathcal N(0,\sigma_n^2) f(x)=xTw,y=f(x)+ϵ,ϵ∼N(0,σn2)因此其参数对象为 w ∈ R D \mathbf{w}\in \mathbb R^D w∈RD,于是这个监督问题便被建模起来了,从 X , Y X,Y X,Y中学习线性结构模型中的参数 W W W即 p ( w ∣ X , y ) p(\mathbf w|X,\mathbf y) p(w∣X,y)
-
贝叶斯线性模型 ( 请自行对应posterior, likelihood, prior )
posterior = likelihood × prior marginal likelihood ⟺ p ( w ∣ y , X ) = p ( y ∣ X , w ) p ( w ) p ( y ∣ X ) \text{posterior}=\frac{\text{likelihood}\times \text{prior}}{\text{marginal likelihood}}\Longleftrightarrow p(\mathbf w\mid \mathbf y,X)=\frac{p(\mathbf y|X,\mathbf w)p(\mathbf w)}{p(\mathbf y\mid X)} posterior=marginal likelihoodlikelihood×prior⟺p(w∣y,X)=p(y∣X)p(y∣X,w)p(w)频率派角度认为参数w是一个数,而贝叶斯派认为参数w服从一个分布,此处为贝叶斯学派的角度。于是参数分布通过线性结构(function class)决定了模型。MAP与MLE的差别仅在于prior,此处指定参数分布的prior服从均值为0向量,协方差矩阵为 Σ p \Sigma_p Σp的多元高斯分布:
w ∼ N ( 0 , Σ p ) \mathbf w\sim \mathcal N(\mathbf 0,\Sigma_p) w∼N(0,Σp)likelihood为(假设样本i.i.d):
p ( y ∣ X , w ) = ∏ i = 1 n p ( y i ∣ x i , w ) = ∏ i = 1 n 1 2 π σ n exp ( − ( y i − x i ⊤ w ) 2 2 σ n 2 ) = 1 ( 2 π σ n 2 ) n / 2 exp ( − 1 2 σ n 2 ∣ y − X ⊤ w ∣ 2 ) = N ( X ⊤ w , σ n 2 I ) \begin{aligned} p(\mathbf{y} \mid X, \mathbf{w}) &=\prod_{i=1}^{n} p\left(y_{i} \mid \mathbf{x}_{i}, \mathbf{w}\right)=\prod_{i=1}^{n} \frac{1}{\sqrt{2 \pi} \sigma_{n}} \exp \left(-\frac{\left(y_{i}-\mathbf{x}_{i}^{\top} \mathbf{w}\right)^{2}}{2 \sigma_{n}^{2}}\right) \\ &=\frac{1}{\left(2 \pi \sigma_{n}^{2}\right)^{n / 2}} \exp \left(-\frac{1}{2 \sigma_{n}^{2}}\left|\mathbf{y}-X^{\top} \mathbf{w}\right|^{2}\right)=\mathcal{N}\left(X^{\top} \mathbf{w}, \sigma_{n}^{2} I\right) \end{aligned} p(y∣X,w)=i=1∏np(yi∣xi,w)=i=1∏n2πσn1exp(−2σn2(yi−xi⊤w)2)=(2πσn2)n/21exp(−2σn21∣∣y−X⊤w∣∣2)=N(X⊤w,σn2I)posterior为:
p ( w ∣ X , y ) ∝ exp ( − 1 2 σ n 2 ( y − X ⊤ w ) ⊤ ( y − X ⊤ w ) ) exp ( − 1 2 w ⊤ Σ p − 1 w ) ∝ exp ( − 1 2 ( w − w ‾ ) ⊤ ( 1 σ n 2 X X ⊤ + Σ p − 1 ) ( w − w ‾ ) ) w h e r e w ‾ = σ n − 2 ( σ n − 2 X X T + Σ p − 1 ) − 1 X y \begin{aligned} p(\mathbf{w} \mid X, \mathbf{y}) & \propto \exp \left(-\frac{1}{2 \sigma_{n}^{2}}\left(\mathbf{y}-X^{\top} \mathbf{w}\right)^{\top}\left(\mathbf{y}-X^{\top} \mathbf{w}\right)\right) \exp \left(-\frac{1}{2} \mathbf{w}^{\top} \Sigma_{p}^{-1} \mathbf{w}\right) \\ & \propto \exp \left(-\frac{1}{2}(\mathbf{w}-\overline{\mathbf{w}})^{\top}\left(\frac{1}{\sigma_{n}^{2}} X X^{\top}+\Sigma_{p}^{-1}\right)(\mathbf{w}-\overline{\mathbf{w}})\right) \end{aligned}\\ where \quad \overline{\mathbf{w}}=\sigma_n^{-2}(\sigma_n^{-2}XX^T+\Sigma_p^{-1})^{-1}X\mathbf y p(w∣X,y)∝exp(−2σn21(y−X⊤w)⊤(y−X⊤w))exp(−21w⊤Σp−1w)∝exp(−21(w−w)⊤(σn21XX⊤+Σp−1)(w−w))wherew=σn−2(σn−2XXT+Σp−1)−1Xy
令 A = σ n − 2 X X T + Σ p − 1 \mathbf A=\sigma_n^{-2}XX^T+\Sigma_p^{-1} A=σn−2XXT+Σp−1有:
p ( w ∣ X , y ) ∼ N ( w ‾ = 1 σ n 2 A − 1 X y , A − 1 ) p(\mathbf w|X,\mathbf y)\sim \mathcal N(\overline \mathbf w=\frac{1}{\sigma_n^2}A^{-1}X\mathbf y,A^{-1}) p(w∣X,y)∼N(w=σn21A−1Xy,A−1)因此通过数据集中的 X , y X,\mathbf y X,y,指定模型 f ( x ) f(x) f(x)的输出噪声为 ϵ ∼ N ( 0 , σ n 2 ) \epsilon\sim \mathcal N(0,\sigma_n^2) ϵ∼N(0,σn2),初始化模型参数的prior为 w ∼ N ( 0 , Σ p ) \mathbf w\sim \mathcal N(\mathbf 0,\Sigma_p) w∼N(0,Σp),便可得到参数的分布 p ( w ∣ X , y ) p(\mathbf w|X,\mathbf y) p(w∣X,y).
-
贝叶斯推断:给定测试样本 x ∗ x_{*} x∗,推断其输出 f ( x ∗ ) f(x_{*}) f(x∗),简记为 f ⋆ f_\star f⋆
p ( f ∗ ∣ x ∗ , X , y ) = ∫ p ( f ∗ ∣ x ∗ , w ) p ( w ∣ X , y ) d w = N ( 1 σ n 2 x ∗ ⊤ A − 1 X y , x ∗ ⊤ A − 1 x ∗ ) \begin{aligned} p\left(f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y}\right) &=\int p\left(f_{*} \mid \mathbf{x}_{*}, \mathbf{w}\right) p(\mathbf{w} \mid X, \mathbf{y}) d \mathbf{w} \\ &=\mathcal{N}\left(\frac{1}{\sigma_{n}^{2}} \mathbf{x}_{*}^{\top} A^{-1} X \mathbf{y}, \mathbf{x}_{*}^{\top} A^{-1} \mathbf{x}_{*}\right) \end{aligned} p(f∗∣x∗,X,y)=∫p(f∗∣x∗,w)p(w∣X,y)dw=N(σn21x∗⊤A−1Xy,x∗⊤A−1x∗)
根据下图总结一下:
- 具体化问题为一维的 x x x,两个参数 w 1 , w 2 w_1,w_2 w1,w2的线性模型 f ( x ) = w 1 + w 2 x f(x)=w_1 + w_2x f(x)=w1+w2x,noise为 ϵ ∼ N ( 0 , I ) \epsilon \sim \mathcal N(0,I) ϵ∼N(0,I)
- 确定prior为 w ∼ N ( 0 , I ) w\sim \mathcal N(\mathbf 0, \mathbf I) w∼N(0,I),对应下图(a)
- 确定数据集 X , y X,\mathbf y X,y为图(b)中的三个带星号的样本点
- 于是likelihood和posterior都有了 p ( y ∣ X , w ) = N ( X ⊤ w , σ n 2 I ) p(\mathbf{y} \mid X, \mathbf{w})=\mathcal{N}\left(X^{\top} \mathbf{w}, \sigma_{n}^{2} I\right) p(y∣X,w)=N(X⊤w,σn2I)、 p ( w ∣ X , y ) ∼ N ( w ‾ = 1 σ n 2 A − 1 X y , A − 1 ) p(\mathbf w|X,\mathbf y)\sim \mathcal N(\overline \mathbf w=\frac{1}{\sigma_n^2}A^{-1}X\mathbf y,A^{-1}) p(w∣X,y)∼N(w=σn21A−1Xy,A−1),分别对应(c)(d)
- 然后经过贝叶斯推断公式 p ( f ∗ ∣ x ∗ , X , y ) = N ( 1 σ n 2 x ∗ ⊤ A − 1 X y , x ∗ ⊤ A − 1 x ∗ ) p\left(f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y}\right)=\mathcal{N}\left(\frac{1}{\sigma_{n}^{2}} \mathbf{x}_{*}^{\top} A^{-1} X \mathbf{y}, \mathbf{x}_{*}^{\top} A^{-1} \mathbf{x}_{*}\right) p(f∗∣x∗,X,y)=N(σn21x∗⊤A−1Xy,x∗⊤A−1x∗),在(b)中画出mean plot对应实线,加出一两个标准差对应虚线。
按逻辑抽象总结一波:
- 将一定数量的样本组成 X X X,对应的label组成 y \mathbf y y
- 将它们丢进设定好prior与model noise的标准线性模型中
- 根据MAP准则得到模型参数 W W W服从的分布,即posterior
- 给一个测试样本 x ⋆ x_\star x⋆,模型通过贝叶斯推断给label服从的分布 f ⋆ f_\star f⋆
- 从label分布中取mean值,or sample给出estimate
1.2 Feature Space的贝叶斯线性模型
上述之所以为标准线性模型,是因为定义模型时采用的是直接的输出:
f
(
x
)
=
x
T
w
f(\mathbf x)=\mathbf x^Tw
f(x)=xTw
该线性指的是与参数的线性,将
D
D
D维输入向量
x
\mathbf x
x投射到高维空间
ϕ
(
x
)
\mathbf \phi(x)
ϕ(x),认为是特征空间Feature Space,在高维空间上建模与参数的线性模型,对原本输入其中第一维度
x
1
x_1
x1投射形式可以选多项式方式,扩展到
L
L
L维。
ϕ
(
x
1
)
=
(
1
,
x
,
x
2
,
.
.
.
,
x
L
−
1
)
T
\mathbf \phi(x_1)=(1,x,x^2,...,x^{L-1})^T
ϕ(x1)=(1,x,x2,...,xL−1)T
无论哪种方式, ϕ \mathbf \phi ϕ的作用就是把 D D D维输入向量 x ∈ R D \mathbf x\in \mathbb R^D x∈RD中的每一维度亦不同的方式扩展成高维 N N N维 ϕ ( x ) ∈ R N \mathbf \phi(x)\in \mathbb R^N ϕ(x)∈RN.
贝叶斯线性模型为:
f
(
x
)
=
ϕ
(
x
)
T
w
f(\mathbf x)=\phi(\mathbf x)^Tw
f(x)=ϕ(x)Tw
只需要把1.1节贝叶斯标准线性模型中的推断形式换一下即可。
原来为:
p
(
f
∗
∣
x
∗
,
X
,
y
)
=
∫
p
(
f
∗
∣
x
∗
,
w
)
p
(
w
∣
X
,
y
)
d
w
=
N
(
1
σ
n
2
x
∗
⊤
A
−
1
X
y
,
x
∗
⊤
A
−
1
x
∗
)
(1)
\begin{aligned} p\left(f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y}\right) &=\int p\left(f_{*} \mid \mathbf{x}_{*}, \mathbf{w}\right) p(\mathbf{w} \mid X, \mathbf{y}) d \mathbf{w} \\ &=\mathcal{N}\left(\frac{1}{\sigma_{n}^{2}} \mathbf{x}_{*}^{\top} A^{-1} X \mathbf{y}, \mathbf{x}_{*}^{\top} A^{-1} \mathbf{x}_{*}\right) \end{aligned}\tag{1}
p(f∗∣x∗,X,y)=∫p(f∗∣x∗,w)p(w∣X,y)dw=N(σn21x∗⊤A−1Xy,x∗⊤A−1x∗)(1)
将
X
X
X换成
Φ
(
X
)
\boldsymbol{\Phi}(X)
Φ(X)和
x
∗
\mathbf{x}_{*}
x∗换成
ϕ
(
x
∗
)
\boldsymbol{\phi}\left(\mathbf{x}_{*}\right)
ϕ(x∗),于是(1)变成:
f
∗
∣
x
∗
,
X
,
y
∼
N
(
1
σ
n
2
ϕ
(
x
∗
)
⊤
A
−
1
Φ
(
X
)
y
,
ϕ
(
x
∗
)
⊤
A
−
1
ϕ
(
x
∗
)
)
(2)
f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y} \sim \mathcal{N}\left(\frac{1}{\sigma_{n}^{2}} \boldsymbol{\phi}\left(\mathbf{x}_{*}\right)^{\top} A^{-1} \boldsymbol{\Phi}(X) \mathbf{y}, \boldsymbol{\phi}\left(\mathbf{x}_{*}\right)^{\top} A^{-1} \boldsymbol{\phi}\left(\mathbf{x}_{*}\right)\right)\tag{2}
f∗∣x∗,X,y∼N(σn21ϕ(x∗)⊤A−1Φ(X)y,ϕ(x∗)⊤A−1ϕ(x∗))(2)
其中 A = σ n − 2 Φ ( X ) Φ ( X ) T + Σ p − 1 A=\sigma_n^{-2}\boldsymbol{\Phi}(X)\boldsymbol{\Phi}(X)^T+\Sigma_p^{-1} A=σn−2Φ(X)Φ(X)T+Σp−1, Σ p − 1 \Sigma_p^{-1} Σp−1是来自prior的。
老生常谈的一点是,(2)需要对高维的矩阵 A A A是 N × N N\times N N×N求逆,需要很大的computation,需要引入kernel trick,怎么引入呢?
将
Φ
(
X
)
\boldsymbol{\Phi}(X)
Φ(X),
ϕ
(
x
∗
)
\boldsymbol{\phi}\left(\mathbf{x}_{*}\right)
ϕ(x∗)即简记为
Φ
,
ϕ
∗
\Phi,\phi_{*}
Φ,ϕ∗,于是
A
=
σ
n
−
2
Φ
Φ
⊤
+
Σ
p
−
1
A=\sigma_n^{-2}\Phi{\Phi}^{\top}+\Sigma_p^{-1}
A=σn−2ΦΦ⊤+Σp−1,定义
K
=
Φ
⊤
Σ
p
Φ
K=\Phi^{\top}\Sigma_p\Phi
K=Φ⊤ΣpΦ,(2)式恒等变换成:
f
∗
∣
x
∗
,
X
,
y
∼
N
(
ϕ
∗
⊤
Σ
p
Φ
(
K
+
σ
n
2
I
)
−
1
y
,
ϕ
∗
⊤
Σ
p
ϕ
∗
−
ϕ
∗
⊤
Σ
p
Φ
(
K
+
σ
n
2
I
)
−
1
Φ
⊤
Σ
p
ϕ
∗
)
(3)
\begin{aligned} f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y} \sim \mathcal{N}(& \boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \mathbf{y} ,\\ &\left.\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}-\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \Phi^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}\right) \end{aligned}\tag{3}
f∗∣x∗,X,y∼N(ϕ∗⊤ΣpΦ(K+σn2I)−1y,ϕ∗⊤Σpϕ∗−ϕ∗⊤ΣpΦ(K+σn2I)−1Φ⊤Σpϕ∗)(3)
此处变换正说明了该算法适用范围是样本数量比较少,但样本维度特别高的情况!
该变换成功将需要求逆的矩阵从 N × N N\times N N×N变成 n × n n\times n n×n(n为样本数量),观察(3)式,有 ϕ ∗ ⊤ Σ p Φ , ϕ ∗ ⊤ Σ p ϕ ∗ , ϕ ∗ ⊤ Σ p Φ , Φ ⊤ Σ p ϕ ∗ \boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi,\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \boldsymbol{\phi}_{*},\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi,\Phi^{\top} \Sigma_{p} \boldsymbol{\phi}_{*} ϕ∗⊤ΣpΦ,ϕ∗⊤Σpϕ∗,ϕ∗⊤ΣpΦ,Φ⊤Σpϕ∗这些仍需要计算高维inner product的项,将 n × n n\times n n×n协方差矩阵 Σ p \Sigma_p Σp进行SVD分解有 Σ p = U D U ⊤ \Sigma_p=UDU^{\top} Σp=UDU⊤, Σ p 1 / 2 = U D 1 / 2 U ⊤ \Sigma_p^{1/2}=UD^{1/2}U^{\top} Σp1/2=UD1/2U⊤,因此定义kernel为:
k ( x , x ∗ ) = Φ ⊤ Σ p ϕ ∗ = ( Σ p 1 / 2 Φ ) ⊤ ( Σ p 1 / 2 ϕ ∗ ) = ψ ( x ) ⊤ ψ ( x ∗ ) \begin{aligned} k(\mathbf x,\mathbf x_*)&=\Phi^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}\\ &=(\Sigma_p^{1/2}\Phi)^{\top}(\Sigma_p^{1/2}\boldsymbol{\phi}_{*})\\ &=\psi(\mathbf x)^{\top}\psi(\mathbf x_*) \end{aligned} k(x,x∗)=Φ⊤Σpϕ∗=(Σp1/2Φ)⊤(Σp1/2ϕ∗)=ψ(x)⊤ψ(x∗)
所以(3)中分布的参数就可以全用这个kernel进行计算了。kernel trick确实是为了简化高维计算才引入的,其也塑造了原始输入映射到高维空间的特征构造方式。所以选择了一个kernel,就意味着特征映射到高维空间的方式确定了。
二、Function-space View
2.1 高斯过程回顾GPR前情提要
高斯过程的定义:高斯过程是随机变量的集合,随机变量的任何有限数量都服从联合高斯分布
f ( x ) ∼ G P ( m ( x ) , k ( x , x ′ ) ) w h e r e m ( x ) = E [ f ( x ) ] k ( x , x ′ ) = E [ ( f ( x ) − m ( x ) ) ( f ( x ′ ) − m ( x ′ ) ) ] (*) \begin{aligned} f(\mathbf{x})&\sim \mathcal{GP}\left(m(\mathbf{x}),k(\mathbf{x},\mathbf{x}')\right)\\ where \quad m(\mathbf{x})&=\mathbb E[f(\mathbf x)]\\ k(\mathbf{x,x'})&=\mathbb E[(f(\mathbf{x})-m(\mathbf{x}))(f(\mathbf x')-m(\mathbf x'))]\tag{*} \end{aligned} f(x)wherem(x)k(x,x′)∼GP(m(x),k(x,x′))=E[f(x)]=E[(f(x)−m(x))(f(x′)−m(x′))](*)
Weight-Space View主要是想说明贝叶斯线性回归是高斯过程的特例。
将
f
(
x
)
=
ϕ
(
x
)
⊤
w
,
w
∼
N
(
0
,
Σ
p
)
f(\mathbf{x})=\boldsymbol{\phi(\mathbf{x})^{\top}}\mathbf w,\mathbf w\sim \mathcal N(\mathbf 0,\Sigma_p)
f(x)=ϕ(x)⊤w,w∼N(0,Σp)代入高斯过程的定义(*)中:
E
[
f
(
x
)
]
=
ϕ
(
x
)
⊤
E
[
w
]
=
0
E
[
f
(
x
)
f
(
x
′
)
]
=
ϕ
(
x
)
⊤
E
[
w
w
⊤
]
ϕ
(
x
′
)
=
ϕ
(
x
)
⊤
Σ
p
ϕ
(
x
′
)
.
\begin{aligned} \mathbb{E}[f(\mathbf{x})] &=\boldsymbol{\phi}(\mathbf{x})^{\top} \mathbb{E}[\mathbf{w}]=0 \\ \mathbb{E}\left[f(\mathbf{x}) f\left(\mathbf{x}^{\prime}\right)\right] &=\boldsymbol{\phi}(\mathbf{x})^{\top} \mathbb{E}\left[\mathbf{w} \mathbf{w}^{\top}\right] \boldsymbol{\phi}\left(\mathbf{x}^{\prime}\right)=\boldsymbol{\phi}(\mathbf{x})^{\top} \Sigma_{p} \boldsymbol{\phi}\left(\mathbf{x}^{\prime}\right) . \end{aligned}
E[f(x)]E[f(x)f(x′)]=ϕ(x)⊤E[w]=0=ϕ(x)⊤E[ww⊤]ϕ(x′)=ϕ(x)⊤Σpϕ(x′).
所以贝叶斯线性回归是mean function即
m
(
x
)
=
0
m(\mathbf x)=0
m(x)=0,covariance function为kernel的特例。
这从直觉上怎么理解?
- mean function为0,意味着每一个样本 x \mathbf x x通过模型 f f f的输出 f ( x ) f(\mathbf x) f(x)期望为0,在没有特别先验的情况下,均可设置所有可能的函数在某个样本的值均值为0(这里的期望的对象是所有函数,在一个样本点 x 0 x_0 x0下, f 1 ( x 0 ) , f 2 ( x 0 ) , . . . , f n ( x 0 ) f_1(x_0),f_2(x_0),...,f_n(x_0) f1(x0),f2(x0),...,fn(x0)加起来为0)
- covariance function取决于kernel,意味着模型输出 f ( x ) f(\mathbf x) f(x)与回归值 y \mathbf y y的不同之处在于该样本 x \mathbf x x与其它样本 x ′ \mathbf x' x′的距离 ϕ ( x ) ⊤ Σ p ϕ ( x ′ ) \boldsymbol{\phi}(\mathbf{x})^{\top} \Sigma_{p} \boldsymbol{\phi}\left(\mathbf{x}^{\prime}\right) ϕ(x)⊤Σpϕ(x′),距离越远,模型输出 f ( x ) f(\mathbf x) f(x)与回归值 y \mathbf y y的扰动越大。将样本的距离与输出空间的函数分布扰动建立了联系(符合离其它样本 x ′ \mathbf x' x′越远的样本 x \mathbf x x,其输出的 f ( x ) f(\mathbf x) f(x)应该更远离的直觉)
所以从函数的角度上看:
- mean function就是函数 f ( x ) f(x) f(x)分布的prior
- covariance functoon就是prior中关于函数分布的平滑特性smoothness
2.2 指定covariance function的GPR
既然mean function是函数分布的prior,在一无所知的情况下一般指定为0,于是关键是选择一个特定的covariance function,此处选择指定如下:
cov ( f ( x ) , f ( x ′ ) ) = E [ ( f ( x ) − m ( x ) ) ( f ( x ′ ) − m ( x ′ ) ) ] = k ( x , x ) = exp ( − 1 2 ∣ x − x ′ ∣ 2 ) w h e r e m ( x ) = m ( x ′ ) = 0 \begin{aligned} \operatorname{cov}\left(f\left(\mathbf{x}\right), f\left(\mathbf{x'}\right)\right)&=\mathbb E[(f(\mathbf{x})-m(\mathbf{x}))(f(\mathbf x')-m(\mathbf x'))]\\&=k\left(\mathbf{x}, \mathbf{x}\right)=\exp \left(-\frac{1}{2}\left|\mathbf{x}-\mathbf{x'}\right|^{2}\right)\\ &where\quad m(\mathbf{x})=m(\mathbf{x'})=0 \end{aligned} cov(f(x),f(x′))=E[(f(x)−m(x))(f(x′)−m(x′))]=k(x,x)=exp(−21∣x−x′∣2)wherem(x)=m(x′)=0
这covariance function是两个输入样本之间距离的函数,两个样本越近说明它们对彼此的影响越大即方差越大,越远影响越小从而方差越小,还可以引入一个length scale即
ℓ
\ell
ℓ来衡量距离的程度对输出的影响。
∣
x
−
x
′
∣
/
ℓ
\left|\mathbf{x}-\mathbf{x'}\right| / \ell
∣x−x′∣/ℓ
所以总结一波,问题如下:
(
x
∈
R
)
(x\in \mathbb R)
(x∈R)
f
(
x
)
∼
G
P
(
m
(
x
)
,
k
(
x
,
x
′
)
)
w
h
e
r
e
m
(
x
)
=
0
k
(
x
,
x
′
)
=
exp
(
−
1
2
∣
x
−
x
′
∣
2
)
\begin{aligned} f(\mathbf{x})&\sim \mathcal{GP}\left(m(\mathbf{x}),k(\mathbf{x},\mathbf{x}')\right)\\ &where \quad m(\mathbf{x}) =0\\ & k(\mathbf x,\mathbf x')=\exp\left(-\frac{1}{2}\left| x-x'\right|^2\right) \end{aligned}
f(x)∼GP(m(x),k(x,x′))wherem(x)=0k(x,x′)=exp(−21∣x−x′∣2)
下面的测试点只是为了画出函数!请重点理解!
-
在prior的情况下,从 [ − 5 , 5 ] [-5,5] [−5,5]中选择一堆测试点 x 1 , . . . , x n ∗ x_1,...,x_{n_*} x1,...,xn∗拼成矩阵 X ∗ X_* X∗,然后逐个计算 k ( x p , x q ) , ∀ p , q ∈ n ∗ k(x_p,x_q),\forall p,q\in n_* k(xp,xq),∀p,q∈n∗拼成 n ∗ × n ∗ n_*\times n_* n∗×n∗的矩阵 K ( X ∗ , X ∗ ) K(X_*,X_*) K(X∗,X∗),形成 f ( X ∗ ) ∼ N ( 0 , K ( X ∗ , X ∗ ) ) \mathbf f(X_*)\sim \mathcal N\left(\mathbf 0, K(X_*,X_*)\right) f(X∗)∼N(0,K(X∗,X∗))从中Sample出三个函数如下图(a)的红、绿、蓝(这些测试点只是为了画出函数)
-
假设有了观察得到的训练样本点 { x i , f ( x i ) } i = 1 n \{x_i,f(x_i)\}_{i=1}^n {xi,f(xi)}i=1n,拼成训练矩阵 X ∈ R 1 × n X\in \mathbb R^{1\times n} X∈R1×n,其输出拼成 f \mathbf f f,而测试矩阵 X ∗ ∈ R 1 × n ∗ X_*\in \mathbb R^{1\times n_*} X∗∈R1×n∗,输出为 f ∗ \mathbf f_* f∗,于是它们的joint prior为:
[ f f ∗ ] ∼ N ( 0 , [ K ( X , X ) K ( X , X ∗ ) K ( X ∗ , X ) K ( X ∗ , X ∗ ) ] ) w h e r e K ( X , X ∗ ) ∈ R n × n ∗ , K ( X ∗ , X ) ∈ R ∈ n ∗ × n \left[\begin{array}{l} \mathbf{f} \\ \mathbf{f}_{*} \end{array}\right] \sim \mathcal{N}\left(\mathbf{0},\left[\begin{array}{ll} K(X, X) & K\left(X, X_{*}\right) \\ K\left(X_{*}, X\right) & K\left(X_{*}, X_{*}\right) \end{array}\right]\right)\\ \text where\quad K(X,X_*)\in \mathbb R^{n\times n_*},K(X_*,X)\in \mathbb R\in ^{n_*\times n} [ff∗]∼N(0,[K(X,X)K(X∗,X)K(X,X∗)K(X∗,X∗)])whereK(X,X∗)∈Rn×n∗,K(X∗,X)∈R∈n∗×n -
1.2节中的(3)式即Bayesian Linear Regression的推断公式: f ∗ ∣ x ∗ , X , y ∼ N ( ϕ ∗ ⊤ Σ p Φ ( K + σ n 2 I ) − 1 y , ϕ ∗ ⊤ Σ p ϕ ∗ − ϕ ∗ ⊤ Σ p Φ ( K + σ n 2 I ) − 1 Φ ⊤ Σ p ϕ ∗ ) (3) \begin{aligned} f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y} \sim \mathcal{N}(& \boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \mathbf{y} ,\\ &\left.\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}-\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \Phi^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}\right) \end{aligned}\tag{3} f∗∣x∗,X,y∼N(ϕ∗⊤ΣpΦ(K+σn2I)−1y,ϕ∗⊤Σpϕ∗−ϕ∗⊤ΣpΦ(K+σn2I)−1Φ⊤Σpϕ∗)(3)
此处的 ϕ ∗ ⊤ Σ p Φ \boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi ϕ∗⊤ΣpΦ实际上就是Kernel(嗯嗯!不懂可看文末总结)
可以得到观察到训练样本后,测试的输出函数分布如图(b)所示:
f ∗ ∣ X ∗ , X , f ∼ N ( K ( X ∗ , X ) K ( X , X ) − 1 f K ( X ∗ , X ∗ ) − K ( X ∗ , X ) K ( X , X ) − 1 K ( X , X ∗ ) ) \begin{aligned} \mathbf{f}_{*} \mid X_{*}, X, \mathbf{f} \sim \mathcal{N}(& K\left(X_{*}, X\right) K(X, X)^{-1} \mathbf{f} \\ &\left.K\left(X_{*}, X_{*}\right)-K\left(X_{*}, X\right) K(X, X)^{-1} K\left(X, X_{*}\right)\right) \end{aligned} f∗∣X∗,X,f∼N(K(X∗,X)K(X,X)−1fK(X∗,X∗)−K(X∗,X)K(X,X)−1K(X,X∗))
- 从函数的prior distribution中Sample出三个函数如图a(蓝、绿、红),这些函数特性由covariance function指定
- 然后根据标有"+"号的样本训练后,这三个函数发生了相应的变化如图b
所以总结一下:
- 不再像多元高斯分布那样,每一维度对应数据的一个属性,高斯过程把一个具体的数据点(样本) x ⃗ \vec\mathbf x x当作一个随机变量,高斯过程回归的目的是预测函数 f ( x ⃗ ) f(\vec \mathbf x) f(x)的分布
- 根据高斯过程的定义,有限个随机变量 { x ⃗ 1 , . . . , x ⃗ k } \{\vec x_1,...,\vec x_k\} {x1,...,xk}之间服从联合多元的高斯分布 N ( μ k , Σ k ) \mathcal N(\mu_k,\Sigma_k) N(μk,Σk),而它们的函数值 f ( x ⃗ i ) , i ∈ k f(\vec x_i),i\in k f(xi),i∈k也服从对应的正态分布 N ( μ i , Σ i ) \mathcal N(\mu_i,\Sigma_i) N(μi,Σi)
- 定义高斯过程的prior distribution中covariance function是最为关键的,从输出函数的prior中sample一些函数,观察到observation后根据贝叶斯推断的公式如(3)式得到sampled function的posterior
2.3 更为一般的GPR:
在2.2节中并没有考虑导致测量误差、观测误差的噪声变量,因此更为General的GPR如下概率图所示:
核心的一点是:我们看到的数据是应该带有噪声的
不考虑噪声的数据集在2.2节中表示为
{
x
i
,
f
(
x
i
)
≜
f
i
}
\{\mathbf x_i,f(x_i)\triangleq f_i\}
{xi,f(xi)≜fi},那么考虑噪声后:
y
=
f
(
x
)
+
ϵ
,
ϵ
∼
N
(
0
,
σ
n
2
)
y=f(\mathbf x)+\epsilon,\epsilon\sim \mathcal N(\mathbf 0,\sigma_n^2)
y=f(x)+ϵ,ϵ∼N(0,σn2)
其中 σ n \sigma_n σn表示取决于样本本身的噪声,与其它样本独立,所以带噪声的数据集表示为: { x i , y i ≜ f i + ϵ } \{\mathbf x_i,y_i\triangleq f_i+\epsilon\} {xi,yi≜fi+ϵ}
-
考虑噪声的covariance function表示为:
cov ( y ) = K ( X , X ) + σ n 2 I o r cov ( y p , y q ) = k ( x p , x q ) + σ n 2 δ p q , δ p q ∼ N ( 0 , 1 ) \operatorname{cov} (\mathbf y) = K(X,X)+\sigma_n^2I\quad or \quad \operatorname{cov}(y_p,y_q)=k(\mathbf x_p,\mathbf x_q)+\sigma_n^2\delta_{pq},\delta_{pq}\sim \mathcal N(0,1) cov(y)=K(X,X)+σn2Iorcov(yp,yq)=k(xp,xq)+σn2δpq,δpq∼N(0,1) -
prior distribution为:
[ y f ∗ ] ∼ N ( 0 , [ K ( X , X ) + σ n 2 I K ( X , X ∗ ) K ( X ∗ , X ) K ( X ∗ , X ∗ ) ] ) \left[\begin{array}{c} \mathbf{y} \\ \mathbf{f}_{*} \end{array}\right] \sim \mathcal{N}\left(\mathbf{0},\left[\begin{array}{cc} K(X, X)+\sigma_{n}^{2} I & K\left(X, X_{*}\right) \\ K\left(X_{*}, X\right) & K\left(X_{*}, X_{*}\right) \end{array}\right]\right) [yf∗]∼N(0,[K(X,X)+σn2IK(X∗,X)K(X,X∗)K(X∗,X∗)]) -
posterior distribution为:
f ∗ ∣ X , y , X ∗ ∼ N ( f ‾ ∗ , cov ( f ∗ ) ) , where f ‾ ∗ ≜ E [ f ∗ ∣ X , y , X ∗ ] = K ( X ∗ , X ) [ K ( X , X ) + σ n 2 I ] − 1 y cov ( f ∗ ) = K ( X ∗ , X ∗ ) − K ( X ∗ , X ) [ K ( X , X ) + σ n 2 I ] − 1 K ( X , X ∗ ) \begin{aligned} \mathbf{f}_{*} \mid X, \mathbf{y}, X_{*} & \sim \mathcal{N}\left(\overline{\mathbf{f}}_{*}, \operatorname{cov}\left(\mathbf{f}_{*}\right)\right), \text { where } \\ \overline{\mathbf{f}}_{*} & \triangleq \mathbb{E}\left[\mathbf{f}_{*} \mid X, \mathbf{y}, X_{*}\right]=K\left(X_{*}, X\right)\left[K(X, X)+\sigma_{n}^{2} I\right]^{-1} \mathbf{y} \\ \operatorname{cov}\left(\mathbf{f}_{*}\right) &=K\left(X_{*}, X_{*}\right)-K\left(X_{*}, X\right)\left[K(X, X)+\sigma_{n}^{2} I\right]^{-1} K\left(X, X_{*}\right) \end{aligned} f∗∣X,y,X∗f∗cov(f∗)∼N(f∗,cov(f∗)), where ≜E[f∗∣X,y,X∗]=K(X∗,X)[K(X,X)+σn2I]−1y=K(X∗,X∗)−K(X∗,X)[K(X,X)+σn2I]−1K(X,X∗)
仔细看看mean以及variance的项
- mean取决于observed targets或观测或label即 y \mathbf y y、测试点与观测点在高维空间的距离 K ( X ∗ , X ) K(X_*,X) K(X∗,X)
- covariance function与观测值 y \mathbf y y无关!只与输入有关!方差一般衡量不确定性,所以只与测试点和观测点的距离相关,与直觉相符!
- covariance function第一项 K ( X ∗ , X ∗ ) K(X_*,X_*) K(X∗,X∗)只是单纯的prior covariances没有任何信息,然后减去了一项观测到 X X X的信息变化
三、高斯过程总结
3.1 两个角度的联系
用了大量篇幅描述了Weight-Space View以及Function Space View,现在用来点明它们之间的联系,说明它们本质上是同一个东西。
在Weight-Space View中的贝叶斯推断形式(posterior)为:
f ∗ ∣ x ∗ , X , y ∼ N ( ϕ ∗ ⊤ Σ p Φ ( K + σ n 2 I ) − 1 y , ϕ ∗ ⊤ Σ p ϕ ∗ − ϕ ∗ ⊤ Σ p Φ ( K + σ n 2 I ) − 1 Φ ⊤ Σ p ϕ ∗ ) where K = Φ ⊤ Σ p Φ (3) \begin{aligned} f_{*} \mid \mathbf{x}_{*}, X, \mathbf{y} \sim \mathcal{N}(& \boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \mathbf{y} ,\\ &\left.\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}-\boldsymbol{\phi}_{*}^{\top} \Sigma_{p} \Phi\left(K+\sigma_{n}^{2} I\right)^{-1} \Phi^{\top} \Sigma_{p} \boldsymbol{\phi}_{*}\right) \end{aligned}\tag{3}\\ \text{where}\quad K=\Phi^{\top}\Sigma_p\Phi f∗∣x∗,X,y∼N(ϕ∗⊤ΣpΦ(K+σn2I)−1y,ϕ∗⊤Σpϕ∗−ϕ∗⊤ΣpΦ(K+σn2I)−1Φ⊤Σpϕ∗)whereK=Φ⊤ΣpΦ(3)
在Function-Space View中的函数分布推断形式(posterior)为:
f
∗
∣
X
,
y
,
X
∗
∼
N
(
f
‾
∗
,
cov
(
f
∗
)
)
,
where
f
‾
∗
≜
E
[
f
∗
∣
X
,
y
,
X
∗
]
=
K
(
X
∗
,
X
)
[
K
(
X
,
X
)
+
σ
n
2
I
]
−
1
y
cov
(
f
∗
)
=
K
(
X
∗
,
X
∗
)
−
K
(
X
∗
,
X
)
[
K
(
X
,
X
)
+
σ
n
2
I
]
−
1
K
(
X
,
X
∗
)
\begin{aligned} \mathbf{f}_{*} \mid X, \mathbf{y}, X_{*} & \sim \mathcal{N}\left(\overline{\mathbf{f}}_{*}, \operatorname{cov}\left(\mathbf{f}_{*}\right)\right), \text { where } \\ \overline{\mathbf{f}}_{*} & \triangleq \mathbb{E}\left[\mathbf{f}_{*} \mid X, \mathbf{y}, X_{*}\right]=K\left(X_{*}, X\right)\left[K(X, X)+\sigma_{n}^{2} I\right]^{-1} \mathbf{y} \\ \operatorname{cov}\left(\mathbf{f}_{*}\right) &=K\left(X_{*}, X_{*}\right)-K\left(X_{*}, X\right)\left[K(X, X)+\sigma_{n}^{2} I\right]^{-1} K\left(X, X_{*}\right) \end{aligned}
f∗∣X,y,X∗f∗cov(f∗)∼N(f∗,cov(f∗)), where ≜E[f∗∣X,y,X∗]=K(X∗,X)[K(X,X)+σn2I]−1y=K(X∗,X∗)−K(X∗,X)[K(X,X)+σn2I]−1K(X,X∗)
所以选择Kernel Function K ( C , D ) = Φ ( C ) ⊤ Σ p Φ ( D ) K(C,D)=\Phi(C)^{\top}\Sigma_p\Phi(D) K(C,D)=Φ(C)⊤ΣpΦ(D),其中 C , D C,D C,D为样本矩阵或测试矩阵 X o r X ∗ XorX_* XorX∗,element-wise地看为 k ( x p , x q ) = ϕ ( x p ) ⊤ Σ p ϕ ( x q ) k(\mathbf x_p,\mathbf x_q)=\phi(\mathbf x_p)^{\top}\Sigma_p\phi(\mathbf x_q) k(xp,xq)=ϕ(xp)⊤Σpϕ(xq)
所以高斯过程
- 从Weight-space view角度看,就是将输入 x \mathbf x x进行高维映射 ϕ ( x ) \phi(\mathbf x) ϕ(x),需要确定这个 ϕ \phi ϕ,然后这些高维特征与参数是线性关系,建模输出 f ( x ) f(\mathbf x) f(x),经过噪声变量 ϵ ∼ N ( 0 , σ n ) \epsilon\sim \mathcal N(0,\sigma_n) ϵ∼N(0,σn)得到我们的观测或label即 y y y,其中参数是一个分布,而不是一个值。
- 从Function-Space View角度看,是通过Kernel Function设定了高维空间的内积关系 < ϕ ( x ) , ϕ ( x ′ ) > <\phi(\mathbf x),\phi(\mathbf x')> <ϕ(x),ϕ(x′)>,隐式地确定了这个映射 ϕ \phi ϕ,然后建模的是关于输出 f ( x ) f(x) f(x)的分布,实际上也就是参数 w w w的分布
3.2 两种View的量化
之前用的都是 K ( X , X ∗ ) K(X,X_*) K(X,X∗)这种矩阵描述,训练样本矩阵与测试样本矩阵的距离,有点懵。此处通过一个测试样本点 x ∗ x_* x∗来量化两种View,训练样本点为 x 1 , . . . , x n x_1,...,x_n x1,...,xn,对应的观测为 y 1 , . . . , y n y_1,...,y_n y1,...,yn,因此mean和covariance为:
f ∗ ‾ = [ k ( x ∗ , x 1 ) k ( x ∗ , x 2 ) ⋮ k ( x ∗ , x n ) ] ( K ( X , X ) + σ n 2 I ) − 1 [ y 1 y 2 ⋮ y n ] ≜ k ∗ ⊤ ( K + σ n 2 I ) − 1 y V [ f ∗ ] = k ( x ∗ , x ∗ ) − k ∗ ⊤ ( K + σ n 2 I ) − 1 k ∗ \begin{aligned} &\overline{f_*}=\left[\begin{matrix}k(x_*,x_1)\\ k(x_*,x_2)\\ \vdots\\ k(x_*,x_n) \end{matrix}\right](K(X,X)+\sigma_n^2I)^{-1}\left[\begin{matrix}y_1\\y_2\\\vdots\\y_n\end{matrix}\right]\triangleq \mathbf k_*^{\top}(K+\sigma_n^2I)^{-1}\mathbf y\\ &\mathbb V[f_*]=k(x_*,x_*)-\mathbf k_*^{\top}(K+\sigma_n^2I)^{-1}\mathbf k_* \end{aligned} f∗=⎣⎢⎢⎢⎡k(x∗,x1)k(x∗,x2)⋮k(x∗,xn)⎦⎥⎥⎥⎤(K(X,X)+σn2I)−1⎣⎢⎢⎢⎡y1y2⋮yn⎦⎥⎥⎥⎤≜k∗⊤(K+σn2I)−1yV[f∗]=k(x∗,x∗)−k∗⊤(K+σn2I)−1k∗
所以这个测试点
x
∗
x_*
x∗的mean输出
f
∗
‾
\overline {f_*}
f∗,可理解为训练样本观测值
y
\mathbf y
y的线性组合;从kernel的角度看,是n个kernel的线性组合即
f
∗
‾
=
∑
i
n
k
(
x
∗
,
x
i
)
α
i
,
=
k
∗
⊤
α
\overline{f_*}=\sum_i^nk(x_*,x_i)\alpha_i,=\mathbf k_*^{\top}\mathbf \alpha
f∗=∑ink(x∗,xi)αi,=k∗⊤α,其中
α
=
(
K
+
σ
n
2
I
)
−
1
y
\mathbf \alpha=(K+\sigma_n^2I)^{-1}\mathbf y
α=(K+σn2I)−1y。然后别忘了这个输出值是个后验分布:
f
∗
∣
X
,
y
,
x
∗
∼
N
(
f
‾
,
V
[
f
∗
]
)
\mathbf {f_*} |X,\mathbf y,x_*\sim \mathcal N(\overline f,\mathbb V[f_*])
f∗∣X,y,x∗∼N(f,V[f∗])
如果还记得贝叶斯:
posterior
=
likelihood
×
prior
marginal likelihood
⟺
p
(
w
∣
y
,
X
)
=
p
(
y
∣
X
,
w
)
p
(
w
)
p
(
y
∣
X
)
\text{posterior}=\frac{\text{likelihood}\times \text{prior}}{\text{marginal likelihood}}\Longleftrightarrow p(\mathbf w\mid \mathbf y,X)=\frac{p(\mathbf y|X,\mathbf w)p(\mathbf w)}{p(\mathbf y\mid X)}
posterior=marginal likelihoodlikelihood×prior⟺p(w∣y,X)=p(y∣X)p(y∣X,w)p(w)
有时可以利用marginal likelihood即 p ( y ∣ X ) p(\mathbf y\mid X) p(y∣X),计算这个的时候有 X → f ≜ f ( X ) → y X\rightarrow \mathbf f\triangleq f(X)\rightarrow \mathbf y X→f≜f(X)→y,即样本经过模型再经过噪声得到观测的过程。
p ( y ∣ X ) = ∫ p ( y ∣ f , X ) p ( f ∣ X ) d f \begin{aligned} p(\mathbf y\mid X)=\int p(\mathbf y|\mathbf f,X)p(\mathbf f|X)d\mathbf f \end{aligned} p(y∣X)=∫p(y∣f,X)p(f∣X)df
其中
f
∣
X
∼
N
(
0
,
K
)
,
y
∣
f
∼
N
(
f
,
σ
n
2
I
)
\mathbf f|X\sim \mathcal N(\mathbf 0,K),\mathbf y|\mathbf f\sim\mathcal N(\mathbf f,\sigma_n^2I)
f∣X∼N(0,K),y∣f∼N(f,σn2I),因此
y
∼
N
(
0
,
K
+
σ
n
2
I
)
\mathbf y\sim \mathcal N(\mathbf 0,K+\sigma_n^2I)
y∼N(0,K+σn2I),所以有:
log
p
(
y
∣
X
)
=
−
1
2
y
⊤
(
K
+
σ
n
2
I
)
−
1
y
−
1
2
log
∣
K
+
σ
n
2
I
∣
−
n
2
log
2
π
\log p(\mathbf{y} \mid X)=-\frac{1}{2} \mathbf{y}^{\top}\left(K+\sigma_{n}^{2} I\right)^{-1} \mathbf{y}-\frac{1}{2} \log \left|K+\sigma_{n}^{2} I\right|-\frac{n}{2} \log 2 \pi
logp(y∣X)=−21y⊤(K+σn2I)−1y−21log∣∣K+σn2I∣∣−2nlog2π
3.3 算法过程
选定高斯过程的covariance function以及噪声变量的noise level,这里用了cholesky分解对矩阵求逆。这个算法是得到了一个测试样本点
x
∗
x_*
x∗在样本矩阵
X
X
X以及观测值
y
\mathbf y
y下mean以及variance的输出,那一个测试矩阵就同理了。
3.4 Kernel
Kernel有很多种如Linear Kernel,Periodic Kernel,上述介绍的一维度的Squared-exponential covariance function即RBF,公式如下,就有length-scale即 ℓ \ell ℓ,signal variance即 σ f 2 \sigma_f^2 σf2,noise variance即 σ n 2 \sigma_n^2 σn2三个超参数的调整。
k y ( x p , x q ) = σ f 2 exp ( − 1 2 ℓ 2 ( x p − x q ) 2 ) + σ n 2 δ p q where δ p q ∼ N ( 0 , 1 ) k_{y}\left(x_{p}, x_{q}\right)=\sigma_{f}^{2} \exp \left(-\frac{1}{2 \ell^{2}}\left(x_{p}-x_{q}\right)^{2}\right)+\sigma_{n}^{2} \delta_{p q}\\ \text{where}\quad \delta_{pq}\sim \mathcal{N}(0,1) ky(xp,xq)=σf2exp(−2ℓ21(xp−xq)2)+σn2δpqwhereδpq∼N(0,1)
3.5 Decision Rule
一个测试样本 x ∗ x_* x∗,既然通过了3.3节得到了其mean以及variance的函数分布,那选哪个值?就是这里的Decision Rule,一般会选均值,但更为General的是确定一个loss function来进行调整。按照上述posterior得到一个 y ∗ y_* y∗的分布,从该分布中根据Decision Rule选择一个 y g u e s s y_{guess} yguess,然后根据loss function来从分布中选择最合适的值 y o p t i m a l y_{optimal} yoptimal,降低分布的随机定。
y o p t i m a l ∣ x ∗ = arg min y g u e s s ∫ p ( y ∗ ∣ x ∗ , X , y ) L ( y ∗ , y g u e s s ) d y ∗ \begin{aligned} y_{optimal}|x_*=\argmin_{y_{guess}}\int p(y_*|x_*,X,\mathbf y)\mathcal L(y_*,y_{guess})dy_* \end{aligned} yoptimal∣x∗=yguessargmin∫p(y∗∣x∗,X,y)L(y∗,yguess)dy∗
认真看看这个 y o p t i m a l y_{optimal} yoptimal的选取,就理解了,这个loss实际上就是在衡量不确定性造成的损失,然后从中选出使损失最小的 y o p t i m a l y_{optimal} yoptimal或者说做出最优的决定。
拓展
这个只是最原始的高斯回归过程,其中有很多需要选择的超参数以及改进的地方。
比如算法速度的优化,kernel函数的选取以及影响。具体可参考
Gaussian Process for Machine Learning
visual gaussian process