CTC损失函数笔记

CTC损失函数笔记

1. 时序分类

假设训练集 S S S来自于分布 D X × Z D_{\mathcal{X}\times \mathcal{Z}} DX×Z,其中 X = ( R m ) ∗ \mathcal{X}=(\mathbb{R}^m)^* X=(Rm)表示输入序列空间, Z = L ∗ \mathcal{Z}=L^* Z=L表示目标序列空间。每个训练数据由一对序列 ( x , z ) (\mathbf{x},\mathbf{z}) (x,z)组成,其中目标序列 z = ( z 1 , z 2 , … , z U ) \mathbf{z}=(z_1,z_2,\ldots,z_U) z=(z1,z2,,zU)的元素个数 U U U至多不会超过输入序列 x = ( x 1 , x 2 , … , x T ) \mathbf{x}=(x_1,x_2,\ldots,x_T) x=(x1,x2,,xT)的元素个数 T T T

1.1 标签错误率(LER)

假设 S ′ S' S表示测试集, h h h表示时序分类器,标签错误率 L E R LER LER公式如下:

L E R ( h , S ′ ) = 1 Z ∑ ( x , z ) ∈ S ′ E D ( h ( x ) ) (1) LER(h,S')=\frac{1}{Z}\sum_{(\mathbf{x},\mathbf{z})\in S'}ED(h(\mathbf{x}))\tag{1} LER(h,S)=Z1(x,z)SED(h(x))(1)
其中 Z Z Z表示测试集样本个数, E D ED ED表示编辑距离。

编辑距离:指两个字符串相互转换所需要的最少编辑次数。转换操作包括替换、插入以及删除。

2. 连接时序分类(CTC)

2.1 从网络输出到标签序列

CTC网络最后一层为softmax输出层,其输出节点一共有 L + 1 L+1 L+1个,最后一个节点输出表示空标签( b l a n k blank blank)。假设某一权重为 w w w的循环神经网络 N w \mathcal{N}_w Nw,输入为 x ∈ ( R m ) T \mathbf{x}\in(\mathbb{R}^m)^T x(Rm)T,输出为 y ∈ ( R n ) T \mathbf{y}\in(\mathbb{R}^n)^T y(Rn)T,其映射公式如下:

y = N w ( x ) (2) \mathbf{y}=\mathcal{N}_w(\mathbf{x})\tag{2} y=Nw(x)(2)
y k t y_k^t ykt表示网络在 t t t时刻的第 k k k个节点输出,可被视作第 k k k个标签在字母表 L ′ = L ∪ { b l a n k } L'=L\cup \{blank\} L=L{blank}上的概率。假设输出层不存在反馈连接,则不同时刻 t t t网络 N w \mathcal{N}_w Nw的输出分布 y t y^t yt条件独立。任意序列 π ∈ ( L ′ ) T \pi\in (L')^T π(L)T π \pi π也被称作路径)在 x \mathbf{x} x输入下的概率密度公式如下:

p ( π ∣ x ) = ∏ t = 1 T y π t t (3) p(\pi|\mathbf{x})=\prod_{t=1}^Ty_{\pi_t}^t\tag{3} p(πx)=t=1Tyπtt(3)

不太明白为什么 y π t t y_{\pi_t}^t yπtt条件独立?

定义一种多到一的变换关系 B : ( L ′ ) T → ( L ) ≤ T \mathcal{B}:(L')^T\rightarrow (L)^{\leq T} B:(L)T(L)T B \mathcal{B} B变换会删除序列中的空标签以及重复标签(例如 B ( a − a b − ) = B ( − a a − − a b b ) = a a b \mathcal{B}(a-ab-)=\mathcal{B}(-aa--abb)=aab B(aab)=B(aaabb)=aab)。利用 B − 1 \mathcal{B}^{-1} B1反变换可以定义标签序列 l ∈ ( L ) ≤ T \mathbf{l}\in(L)^{\leq T} l(L)T x \mathbf{x} x输入下的概率密度:

p ( l ∣ x ) = ∑ π ∈ B − 1 ( l ) p ( π ∣ x ) (4) p(\mathbf{l}|\mathbf{x})=\sum_{\pi\in\mathcal{B}^{-1}(\mathbf{l})}p(\pi|\mathbf{x})\tag{4} p(lx)=πB1(l)p(πx)(4)

B \mathcal{B} B变换先删除重复标签再删除空标签?

2.2 构造分类器

根据式 ( 4 ) (4) (4),我们定义时序分类器输出如下:

h ( x ) = arg ⁡ max ⁡ l ∈ L ≤ T p ( l ∣ x ) (5) h(\mathbf{x})=\arg \max_{\mathbf{l}\in L^{\leq T}}p(\mathbf{l}|\mathbf{x})\tag{5} h(x)=arglLTmaxp(lx)(5)
利用隐马尔可夫术语,我们将分类器从输入序列到标签序列的映射称作解码。由于标签序列 l \mathbf{l} l B − 1 \mathcal{B}^{-1} B1反变换所对应的路径 π \pi π是指数级数量,所以式 ( 5 ) (5) (5)所表示的解码算法难以直接计算。在实际中,我们用如下两种近似方式实现解码算法。

第一种方式为最佳路径解码(best path decoding),其数学表示如下:

h ( x ) ≈ B ( π ∗ ) , π ∗ = arg ⁡ max ⁡ π ∈ N t p ( π ∣ x ) (6) h(\mathbf{x})\approx\mathcal{B}(\pi^*),\pi^*=\arg\max_{\pi\in N^t}p(\pi|\mathbf{x})\tag{6} h(x)B(π),π=argπNtmaxp(πx)(6)
最佳路径解码在计算上的开销可以忽略不计,但这种方式不能保证找到最大概率标签序列。

第二种方式为前缀搜索解码(prefix search decoding),这是一种前向-反向算法。只要有足够的时间,前缀搜索解码总能找到最大概率标签序列。

3. 训练网络

3.1 CTC前向-反向算法

定义前向变量 α t ( s ) \alpha_t(s) αt(s)如下:

α t ( s ) = ∑ π ∈ N T B ( π 1 : t ) = l 1 : s ∏ t ′ = 1 t y π t ′ t ′ (7) \alpha_t(s)=\sum_{\begin{matrix}\pi\in N^T\\\mathcal{B}(\pi_{1:t})=\mathbf{l}_{1:s}\end{matrix}}\prod_{t'=1}^ty_{\pi_{t'}}^{t'}\tag{7} αt(s)=πNTB(π1:t)=l1:st=1tyπtt(7)
为了考虑输出序列中存在空标签的情况,在标签序列 l \mathbf{l} l的每个标签前后插入空标签生成一个长度为 2 ∣ l ∣ + 1 2|\mathbf{l}|+1 2l+1修正标签序列 l ′ \mathbf{l}' l。定义在 l ′ \mathbf{l}' l上的前向变量初始条件如下:

{ α 1 ( 1 ) = y b 1 α 1 ( 2 ) = y l 1 1 α 1 ( s ) = 0 , ∀ s > 2 (8) \left\{\begin{aligned}\alpha_1(1)&=y_b^1\\\alpha_1(2)&=y_{\mathbf{l}_1}^1\\\alpha_1(s)&=0,\forall s>2\end{aligned}\right.\tag{8} α1(1)α1(2)α1(s)=yb1=yl11=0,s>2(8)
前向变量递推公式如下:

α t ( s ) = { α ‾ t ( s ) y l s ′ t , if ⁡   l s ′ = b   or ⁡   l s − 2 ′ = l s ′ ( α ‾ t ( s ) + α t − 1 ( s − 2 ) ) y l s ′ t , otherwise ⁡ where ⁡   α ‾ t ( s ) = α t − 1 ( s ) + α t − 1 ( s − 1 ) (9) \alpha_t(s)=\left\{\begin{aligned}&\overline\alpha_t(s)y_{\mathbf{l}'_s}^t,&\operatorname{if}\ \mathbf{l}'_s=b\ \operatorname{or}\ \mathbf{l}'_{s-2}=\mathbf{l}'_s\\&(\overline\alpha_t(s)+\alpha_{t-1}(s-2))y_{\mathbf{l}'_s}^t,&\operatorname{otherwise}\end{aligned}\right.\\\operatorname{where}\ \overline\alpha_t(s)=\alpha_{t-1}(s)+\alpha_{t-1}(s-1)\tag{9} αt(s)={αt(s)ylst,(αt(s)+αt1(s2))ylst,if ls=b or ls2=lsotherwisewhere αt(s)=αt1(s)+αt1(s1)(9)
条件概率 p ( l ∣ x ) p(\mathbf{l}|\mathbf{x}) p(lx)可用前向变量表示为:

p ( l ∣ x ) = α T ( ∣ l ′ ∣ ) + α T ( ∣ l ′ ∣ − 1 ) (10) p(\mathbf{l}|\mathbf{x})=\alpha_T(|\mathbf{l}'|)+\alpha_T(|\mathbf{l}'|-1)\tag{10} p(lx)=αT(l)+αT(l1)(10)
定义后向变量 β t ( s ) \beta_t(s) βt(s)如下:

β t ( s ) = ∑ π ∈ N T B ( π t : T ) = l s : ∣ l ∣ ∏ t ′ = t T y π t ′ t ′ (11) \beta_t(s)=\sum_{\begin{matrix}\pi\in N^T\\\mathcal{B}(\pi_{t:T})=\mathbf{l}_{s:|\mathbf{l}|}\end{matrix}}\prod_{t'=t}^Ty_{\pi_{t'}}^{t'}\tag{11} βt(s)=πNTB(πt:T)=ls:lt=tTyπtt(11)
定义在 l ′ \mathbf{l}' l上的后向变量初始条件如下:

{ β T ( ∣ l ′ ∣ ) = y b T β T ( ∣ l ′ ∣ − 1 ) = y l ∣ l ∣ T β T ( s ) = 0 , ∀ s < ∣ l ′ ∣ − 1 (12) \left\{\begin{aligned}\beta_T(|\mathbf{l}'|)&=y_b^T\\\beta_T(|\mathbf{l}'|-1)&=y_{\mathbf{l}_{|\mathbf{l}|}}^T\\\beta_T(s)&=0,\forall s<|\mathbf{l}'|-1\end{aligned}\right.\tag{12} βT(l)βT(l1)βT(s)=ybT=yllT=0,s<l1(12)
后向变量递推公式如下:

β t ( s ) = { β ‾ t ( s ) y l s ′ t , if ⁡   l s ′ = b   or ⁡   l s + 2 ′ = l s ′ ( β ‾ t ( s ) + β t + 1 ( s + 2 ) ) y l s ′ t , otherwise ⁡ where ⁡   β ‾ t ( s ) = β t + 1 ( s ) + β t + 1 ( s + 1 ) (13) \beta_t(s)=\left\{\begin{aligned}&\overline\beta_t(s)y_{\mathbf{l}'_s}^t,&\operatorname{if}\ \mathbf{l}'_s=b\ \operatorname{or}\ \mathbf{l}'_{s+2}=\mathbf{l}'_s\\&(\overline\beta_t(s)+\beta_{t+1}(s+2))y_{\mathbf{l}'_s}^t,&\operatorname{otherwise}\end{aligned}\right.\\\operatorname{where}\ \overline\beta_t(s)=\beta_{t+1}(s)+\beta_{t+1}(s+1)\tag{13} βt(s)={βt(s)ylst,(βt(s)+βt+1(s+2))ylst,if ls=b or ls+2=lsotherwisewhere βt(s)=βt+1(s)+βt+1(s+1)(13)
在实际计算中,前向或后向变量的递推很可能造成数据溢出。为了避免这种情况的发生需要对变量进行归一化操作:

{ α ^ t ( s ) = α t ( s ) C t , C t = ∑ s α t ( s ) β ^ t ( s ) = β t ( s ) D t , D t = ∑ s β t ( s ) (14) \left\{\begin{aligned}\hat{\alpha}_t(s)=\frac{\alpha_t(s)}{C_t},C_t=\sum_s\alpha_t(s)\\\hat{\beta}_t(s)=\frac{\beta_t(s)}{D_t},D_t=\sum_s\beta_t(s)\end{aligned}\right.\tag{14} α^t(s)=Ctαt(s),Ct=sαt(s)β^t(s)=Dtβt(s),Dt=sβt(s)(14)
对数条件概率 p ( l ∣ x ) p(\mathbf{l}|\mathbf{x}) p(lx)可用归一化变量表示为:

ln ⁡ ( p ( l ∣ x ) ) = ∑ t = 1 T ln ⁡ ( C t ) (15) \ln(p(\mathbf{l}|\mathbf{x}))=\sum_{t=1}^T\ln(C_t)\tag{15} ln(p(lx))=t=1Tln(Ct)(15)

3.2 极大似然训练

极大似然训练的优化目标如下式所示:

O M L ( S , N w ) = − ∑ ( x , z ) ∈ S ln ⁡ ( p ( z ∣ x ) ) (16) O^{ML}(S,\mathcal{N}_w)=-\sum_{(\mathbf{x},\mathbf{z})\in S}\ln(p(\mathbf{z}|\mathbf{x}))\tag{16} OML(S,Nw)=(x,z)Sln(p(zx))(16)
由于训练集样本独立,考虑单个样本点偏导:

∂ O M L ( { ( x ∣ z ) } , N w ) ∂ y k t = − ∂ ln ⁡ ( p ( z ∣ x ) ) ∂ y k t (17) \frac{\partial O^{ML}(\{(\mathbf{x}|\mathbf{z})\},\mathcal{N}_w)}{\partial y_k^t}=-\frac{\partial \ln(p(\mathbf{z}|\mathbf{x}))}{\partial y_k^t}\tag{17} yktOML({(xz)},Nw)=yktln(p(zx))(17)
根据前向变量以及后向变量定义可得:

α t ( s ) β t ( s ) = ∑ π ∈ B − 1 ( l ) π t = l s ′ y l s ′ t ∏ t = 1 T y π t t (18) \alpha_t(s)\beta_t(s)=\sum_{\begin{matrix}\pi\in \mathcal{B}^{-1}(\mathbf{l})\\\pi_{t}=\mathbf{l}'_s\end{matrix}}y_{\mathbf{l}'_s}^t\prod_{t=1}^Ty_{\pi_t}^t\tag{18} αt(s)βt(s)=πB1(l)πt=lsylstt=1Tyπtt(18)

( 18 ) (18) (18)应从前向变量和后向变量所表达的概率含义理解,不能从等式理解。

结合式 ( 3 ) (3) (3)可得:

α t ( s ) β t ( s ) y l s ′ t = ∑ π ∈ B − 1 ( l ) π t = l s ′ p ( π ∣ x ) (19) \frac{\alpha_t(s)\beta_t(s)}{y_{\mathbf{l}'_s}^t}=\sum_{\begin{matrix}\pi\in \mathcal{B}^{-1}(\mathbf{l})\\\pi_{t}=\mathbf{l}'_s\end{matrix}}p(\pi|\mathbf{x})\tag{19} ylstαt(s)βt(s)=πB1(l)πt=lsp(πx)(19)
由式 ( 19 ) (19) (19)可得,条件概率 p ( l ∣ x ) p(\mathbf{l}|\mathbf{x}) p(lx)公式如下:

p ( l ∣ x ) = ∑ s = 1 ∣ l ′ ∣ α t ( s ) β t ( s ) y l s ′ t (20) p(\mathbf{l}|\mathbf{x})=\sum_{s=1}^{|\mathbf{l}'|}\frac{\alpha_t(s)\beta_t(s)}{y_{\mathbf{l}'_s}^t}\tag{20} p(lx)=s=1lylstαt(s)βt(s)(20)
定义一种位置集合 l a b ( l , k ) = { s : l s ′ = k } lab(\mathbf{l},k)=\{s:\mathbf{l}'_s=k\} lab(l,k)={s:ls=k},对式 ( 20 ) (20) (20)求偏导可得:

∂ p ( l ∣ x ) ∂ y k t = 1 y k t 2 ∑ l a b ( l , k ) = { s : l s ′ = k } α t ( s ) β t ( s ) (21) \frac{\partial p(\mathbf{l}|\mathbf{x})}{\partial y_k^t}=\frac{1}{{y_k^t}^2}\sum_{lab(\mathbf{l},k)=\{s:\mathbf{l}'_s=k\}}\alpha_t(s)\beta_t(s)\tag{21} yktp(lx)=ykt21lab(l,k)={s:ls=k}αt(s)βt(s)(21)

结合式 ( 9 ) (9) (9) ( 13 ) (13) (13),取 y l s ′ t = y k t y_{\mathbf{l}'_s}^t=y_k^t ylst=ykt

对对数条件概率 ln ⁡ p ( l ∣ x ) \ln p(\mathbf{l}|\mathbf{x}) lnp(lx)求偏导可得:

∂ ln ⁡ ( p ( l ∣ x ) ) ∂ y k t = 1 p ( l ∣ x ) ∂ p ( l ∣ x ) ∂ y k t (22) \frac{\partial \ln(p(\mathbf{l}|\mathbf{x}))}{\partial y_k^t}=\frac{1}{p(\mathbf{l}|\mathbf{x})}\frac{\partial p(\mathbf{l}|\mathbf{x})}{\partial y_k^t}\tag{22} yktln(p(lx))=p(lx)1yktp(lx)(22)
可将 l = z \mathbf{l}=\mathbf{z} l=z带入式 ( 22 ) (22) (22)求得对数条件概率 ln ⁡ p ( z ∣ x ) \ln p(\mathbf{z}|\mathbf{x}) lnp(zx)的偏导。 y k t y_k^t ykt是softmax层输出,优化目标函数 O M L ( { x ∣ z } , N w ) O^{ML}(\{\mathbf{x}|\mathbf{z}\},\mathcal{N}_w) OML({xz},Nw)对softmax层输入 u k t u_k^t ukt求偏导可得:

∂ O M L ( { ( x , z ) } , N w ) ∂ u k t = y k t − 1 y k t Z t ∑ s ∈ l a b ( z , k ) α ^ t ( s ) β ^ t ( s ) where ⁡   Z t = ∑ s = 1 ∣ l ′ ∣ α ^ t ( s ) β ^ t ( s ) y l s ′ t (23) \frac{\partial O^{ML}(\{(\mathbf{x},\mathbf{z})\},\mathcal{N}_w)}{\partial u_k^t}=y_k^t-\frac{1}{y_k^tZ_t}\sum_{s\in lab(\mathbf{z},k)}\hat{\alpha}_t(s)\hat{\beta}_t(s)\\\operatorname{where}\ Z_t=\sum_{s=1}^{|\mathbf{l}'|}\frac{\hat{\alpha}_t(s)\hat{\beta}_t(s)}{y_{\mathbf{l}'_s}^t}\tag{23} uktOML({(x,z)},Nw)=yktyktZt1slab(z,k)α^t(s)β^t(s)where Zt=s=1lylstα^t(s)β^t(s)(23)

不太明白式 ( 23 ) (23) (23)是怎么来的?

参考文献

[1]Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值