- 请你介绍一下Transformer模型;

Transformer 结构是一种完全采用注意力或自注意力机制的深度学习模型,可以并行训练 avoid recursion in order to allow parallel computation (to reduce training time)

由 encoder 和 decoder 组成,分别各自有6个相同的层堆叠而成;

- encoder 每个layer 有 2个 sub-layer 组成,分别是

- 多头注意力 multi-head self-attention

- 前馈神经网络并且加了 residual connection 和 layer norm。

- decoder 的每一层是

- 首先用masked multi-head self-attention,

- 再加上和 encoder 做了cross-attention

- 再用前馈神经网络并且加了 residual connection 和 layer norm

---

- Transformer 相对于传统的 RNN 网络有什么好处;

比 RNN -> RNN 更适合处理位置信息,但是不能处理长距离依赖,梯度消失;原本的RNN依赖于前一个词传来的 hidden states 来捕获信息,而Transformer 把输入整个处理,而不是像RNN LSTM 一个一个词地处理,因此不会丢失信息。 **Non sequential**: sentences are processed as a whole rather than word by word. reduce drops due to long dependencies.

并且 multi-head attention and positional embeddings 都会补充位置关系信息。

- Transformer相对RNN为什么能避免梯度消失;

- Transformer 相对于传统的 LSTM 网络有什么好处;

比 LSTM:self-attention 处理长距离的依赖,获取上下文,比如说判断两个句子是否是前后句,可以计算两个句子之间的attention(拼接起来加一个[SEP]然后self-attention),在此之前只能单独encode然后在用个什么结构计算一下相关性

- Transformer 相对于传统的 CNN 网络有什么好处;

比 CNN:CNN 考虑位置信息,但是在池化操作中经常丢失有价值的信息(位置信息),池化+卷积检测到了某种特征的存在,丢失了特征的空间关系

---

- 为什么要用 multihead?多头机制为什么有效

同时有多个 attention 机制在运行,可以帮助网络在同时注意到多个信息,增加学习能力,词与词之间的关系从不同的角度学习,类似CNN中的多通道机制,每个头选择不同的特征;

Multi head attention means, that we have multiple scaled dot product attention mechanisms running in parallel. This helps the network to attend to multiple pieces of information at the same time.

通过切分之后 scaled-dot-product attention 计算维度不大,防止梯度消失

- attention 怎么计算的?简单说公式

$Attention(Q,K,V)=softmax(\frac{Q\times K^T}{\sqrt{d_k}})V$

- Transformer里的Self-Attention作用是什么,有什么优势;

self-attention 计算同一个输入中词语之间的相似度。

如果 Query,Key,Value 都是从同一个序列得到的,就成为了self-attention。self-attention 在输出的时候关注到输入自己。

If keys, values and queries are generated from the same sequence, it is called self-attention.

- cross-attention 作用是什么,有什么优势;

连接encoder 和 decoder 的 attention 叫做 cross-attention,因为keys和values是从不同的序列中得到的。attention机制让 decoder 在输出的时候集中注意到输入的特定部分

In cross attention, the queries are generated by a different sequence, than the key-value pairs.

- scaled-dot-product 为什么要缩放(scale)

输入信息维度 d 比较大,点积有比较大的方差,导致softmax函数的梯度比较小;通过切分之后scaled-dot-product attention 计算维度不大,防止梯度消失

- 位置编码 positional embeddings 是做什么的:

学习相对位置关系,因为没有recurrent模块,所以需要额外的位置信息。 embedding和位置编码简单的相加,transformer足够强大可以学习到信息了。

another innovation introduced to replace recurrence. The idea is to use fixed or learned weights which encode information related to a specific position of a token in a sentence.

The networks learns the order of words in the sentence from the positional encoding provided in the form of sine and cosine waves with different frequencies.

更多推荐