Lazy loaded image
为什么Transformer中位置编码前要将Embedding乘以根号d
Words 533Read Time 2 min
2025-6-24
2026-3-7
type
Post
status
Published
date
Jun 24, 2025
slug
summary
tags
category
icon
password
😀
这里写文章的前言: 一个简单的开头,简述这篇文章讨论的问题、目标、人物、背景是什么?并简述你给出的答案。
可以说说你的故事:阻碍、努力、结果成果,意外与转折。
 

📝 主旨内容

为了让信息不被“淹没”

为了使位置编码相对较小。这意味着当我们相加时,嵌入向量中的原始意义不会丢失。
将Embedding与位置编码PE缩放到相近的维度

与Softmax共享有关

可能为了在解码器嵌入和解码器预softmax线性权重之间共享权重
Similarly to other sequence transduction models, we use learned embeddings to convert the input tokens and output tokens to vectors of dimension 𝑑model. We also use the usual learned linear transformation and softmax function to convert the decoder output to predicted next-token probabilities. In our model, we share the same weight matrix between the two embedding layers and the pre-softmax linear transformation, similar to [24]. In the embedding layers, we multiply those weights by √𝑑model

与Embedding的初始化有关

transformer中embedding层输出为什么要乘上sqrt(d_k)? - Kerry的回答 - 知乎 https://www.zhihu.com/question/12138033385/answer/100458991151
根据知乎回答
embedding的初始化采用了Xavier初始化,它的方差是1/sqrt(dk),所以这样做可以把方差恢复成1。
 
但是经过查看pytorch的代码,我发现W是这样被初始化的
init.normal_(self.weight)
官网的信息也说明,Pytorch的Embedding使用的并不是Xavier
weight (Tensor) – the learnable weights of the module of shape (num_embeddings, embedding_dim) initialized from N(0,1)
 
 
 
词嵌入的初始化均值为 0.0,标准差为 embedding_dim ** -0.5

为了让训练更加稳定

并确保输入标记和权重之间的点积在合理的范围内。缩放权重有助于防止在训练过程中梯度变得过大或过小,这可能导致学习动态不稳定。

🤗 总结归纳

总结文章的内容

📎 参考文章

💡
有关Notion安装或者使用上的问题,欢迎您在底部评论区留言,一起交流~
上一篇
主流Serverless平台免费额度 良心大PK
下一篇
硬件选型-海康威视工业相机型号定义和选购指南