学习笔记之机器翻译和文本摘要3

一、Self-attention

Multi-head Self-attention的维度变化 head = 8
embeding_size =4, seq_len = 2,中间维度3
X [ 2 , 4 ] ∗ W q [ 4 , 3 ] = Q [ 2 , 3 ] , X ∗ W k = K [ 2 , 3 ] , X ∗ W v = V [ 2 , 3 ] X[2,4]*W_q[4,3]=Q[2,3] , X*W_k = K[2,3], X * W_v = V[2,3] X[2,4]Wq[4,3]=Q[2,3],XWk=K[2,3],XWv=V[2,3]
s c o r e = Q K T / d k [ 2 , 2 ] score=QK^T/\sqrt{d_k}[2,2] score=QKT/dk [2,2]
a = s o f t m a x ( s c o r e ) [ 2 , 2 ] a =softmax(score)[2,2] a=softmax(score)[2,2]
c o n t e x t = Σ a V [ 2 , 3 ] context = \Sigma aV [2,3] context=ΣaV[2,3]
C o n t e x t = h e a d 个 c o n t e x t s c o n c a t 起 来 [ 2 , 24 ] Context = head 个 contexts concat 起来 [2,24] Context=headcontextsconcat[2,24]
C o n t e x t [ 2 , 24 ] ∗ W [ 24 , 4 ] = o u t p u t [ 2 , 4 ] Context [2,24] * W[24,4] =output [2,4] Context[2,24]W[24,4]=output[2,4]

二、文本摘要baseline

1. 使用Multi-head self attention

因为文本摘要的难度相对较大,我们会希望把attention的机制升级到Multi-head self attention.

2. 使用Layer Normilazition

Batch Normilazition和Layer Normilazition 都是将数据标准化,可以减少梯度消失现象。
前者是通过标准化不同样本的同一个特征来标准化;后者是对同一个样本的不同特征来标准化。
前者的缺点是依赖于Batch size,并且数据需要记录。(不过比较适合CNN)。再机器翻译和文本摘要的场景中因为seq_len的不同所以不太适合用BN。

3. mask机制

原理是padding后的向量经过一系列变换后padding的位置不一定是零,计算loss的时候如果计算进去就会不准确,引入mask机制可以避免。一般场景{1,0},softmax前则补{1,-999}。

4. 引入先验知识

原理是在文本摘要这个场景下输出强烈依赖于输入,摘要的词几乎全部来源于原文。那么我们可以将文章的原文做一个One_hot,再加一个平移变换后和softmax前的概率值做一个平均后再softmax。

三、Pointer Generator Network

1. Pointer Network

定义 s t s_t st是decoder的第t步, h i h_i hi 是encoder的第i步
e i t = V t a n h ( W 1 s t + W 2 h i + b ) e_i ^{t} = Vtanh(W_1s_t + W_2 h_i+ b) eit=Vtanh(W1st+W2hi+b)

a i t = s o f t m a x ( e i t ) a_i ^{t}= softmax(e_i^{t}) ait=softmax(eit)
h t ∗ = Σ i a i t h i h^*_t = \Sigma_i a_i ^{t}h_i ht=Σiaithi
p v o c a b = s o f t m a x ( V ′ V [ h t ∗ , s t ] + b ) + b ′ ) p_{vocab} = softmax(V^{'}V[h^*_t,s_t] + b) + b^{'}) pvocab=softmax(VV[ht,st]+b)+b)
p g e n = σ ( W 1 h t ∗ + W 2 s t + W ∗ D e c o d e r i n p u t + b ) p_{gen} = \sigma(W_1h^*_t + W_2 s_t + W * Decoder_{input} + b) pgen=σ(W1ht+W2st+WDecoderinput+b)
p ( w ) = p v o c a b ( w ) ∗ p g e n + ( 1 − p g e n ) ∗ Σ i : w i = w a i t p(w) = p_{vocab} (w) * p_{gen} + (1-p_{gen}) * \Sigma _{i:w_i = w}a_i ^{t} p(w)=pvocab(w)pgen+(1pgen)Σi:wi=wait

2. Covarage机制

可以让模型关注之前没关注的角落。
c t = Σ i = 0 t − 1 a i c_t = \Sigma_ {i=0}^{t-1}a^i ct=Σi=0t1ai
e i t = V t a n h ( W 1 s t + W 2 h i + W 3 c t + b ) e_i ^{t} = Vtanh(W_1s_t + W_2 h_i +W_3 c_t+ b) eit=Vtanh(W1st+W2hi+W3ct+b)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值