在标准的 Transformer 网络结构(如经典论文 "Attention Is All You Need" 中所述)中,Dropout 是一种非常关键的正则化技术,用于防止模型过拟合。 通常,Transformer 会在以下 4 个核心位置 添加 Dropout: 1. 输入层:Embedding 与位置编码(Positional Encoding)相加之后 具体位置:在词向量(Token Embedding)与位置编码(Positional Encoding)相加之后,准备输入给第一层 Encoder 或 Decoder 之前。 作用:防止模型在训练初期过度依赖某些特定的词汇特征或绝对位置信息,增强输入表示的鲁棒性。 公式表达:$Input = Dropout(Embedding(x) + PositionalEncoding(x))$ 2. 多头注意力机制(Multi-Head Attention)内部:注意力权重上 具体位置:在计算出注意力分数并经过 Softmax 之后,在与 Value ($V$) 矩阵相乘之前。 作用:随机将一部分注意力权重(Attention W...