Scaled Dot-Product Attention 的公式中为什么要除以 ?
Scaled Dot-Product Attention 的公式中为什么要除以 ?
在学习 Scaled Dot-Product Attention 的过程中,遇到了如下公式
不禁产生疑问,其中的
Attention Is All You Need 中有一段解释
We suspect that for large values of
, the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients. To counteract this effect, we scale the dot products by .
这说明,两个向量的点积可能很大,导致 softmax 函数的梯度太小,因此需要除以一个因子,但是为什么是
文章中的一行注释提及到
To illustrate why the dot products get large, assume that the components of
and are independent random variables with mean and variance . Then their dot product, has mean and variance .
本期,我们将基于上文的思路进行完整的推导,以证明
基本假设
假设独立随机变量
其中
计算 的方差
由随机变量方差的定义可得
因为
从而
又因为
计算
因为
所以
同理,
计算 的方差
如果
到这里就可以解释为什么在最后要除以
可见这个因子的目的是让
参考文献/资料
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤