transformer

transformer 结构

在这里插入图片描述

transformer的掩码

注意力方式掩码
自注意力src_mask = src_att_mask + src_pad_mask
自注意力tgt_mask = tgt_att_mask + tgt_pad_mask
交叉注意力mem_mask = mem_att_mask + mem_pad_mask
掩码类型作用
*_pad_mask为了让当前单词不注意到填充的单词. 一般为不规则锯齿形
*_att_mask为了让当前单词不注意到它“不该”注意到的单词。例如tgt_att_mask为上三角矩阵
掩码类型初始形状拓展后的形状
src_mask(batch_size, src_len)(batch_size, head, src_len, src_len)
tgt_mask(batch_size, tgt_len)(batch_size, head, tgt_len, tgt_len)
mem_mask(batch_size, src_len)(batch_size, head, tgt_len, src_len)

transformer中dropout出现的地方

  1. 位置嵌入之后。

  2. 注意力softmax之后。

  3. 多头注意力之后的线性变换后。

  4. feed_forward 第二个线性层之后。

transformer中layer_norm出现的地方

  1. 残差块之前(或之后)

  2. (最后一个Decoder之后)

torch.nn.Transformer的mask使用

Binary and float masks are supported. For a binary mask, a True value indicates that the corresponding position is not allowed to attend. For a float mask, the mask values will be added to the attention weight.
If both attn_mask and key_padding_mask are supplied, their types should match.

句子的处理流程

在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值