PyTorch 1.2 发布了一个基于论文《[Attention is All You Need](https://arxiv.org/pdf/1706.03762.pdf)》的标准transformer模块。transformer模型在很多seq2seq问题上效果更好,且更容易实现并行训练。`nn.Transformer`模块使用一种注意力机制(最近实现的另一种注意力为 [nn.MultiheadAttention](https://pytorch.org/docs/master/nn.html?highlight=multiheadattention#torch.nn.MultiheadAttention))来捕捉输出和输入之间的整体依赖关系。 `nn.Transformer`做到了高度模块化,其中的单个组件也很容易进行修改和使用(例如本教程中的 [nn.TransformerEncoder](https://pytorch.org/docs/master/nn.html?highlight=nn%20transformerencoder#torch.nn.TransformerEncoder))。