Attention 机制

Attention 机制简单来说就是给定 Q(Query), K(Key), V(Value),通过 Query 和 Key 的匹配程度来决定从 Value 中提取多少信息(就是一个加权求和的过程)。这个可以参考数据库中的查询,根据查询键 Q 去匹配数据库中的键 K,找到对应的记录并取出该记录的值 V。Attention 机制与此类似,先通过 $ Q \cdot K^T $ 来计算相似度,由于$Q \cdot K^T$的结果是一个实数值向量,它的取值范围可能会很大,所以跟 V 相乘之前还需要先进行 softmax,softmax 会把它们归一化成一个和为 1 的概率分布,因此可以写成 $softmax(Q \cdot K^T) \cdot V$。不过 Transformer 的原论文《Attention Is All You Need》中还对$Q \cdot K^T$后的结果除了一个缩放因子$\sqrt{d_k}$,因为随着 $d_k$ 增大,点积的方差会变大,softmax 更容易饱和,导致梯度变小,而除以 $\sqrt{d_k}$ 可以避免这种饱和。由此可以得出最终的公式为 $Attention(Q,K,V)=softmax(\frac {Q \cdot K^T}{\sqrt{d_k}}) \cdot V$。由于加了“缩放因子”,所以这种Attention机制也叫Scaled Dot-Product Attention机制。 Self-Attention vs. Cross-Attention Self-Attention 之所以叫 “Self”,就是因为它的 Query、Key、Value 都是由同一个 x 分别通过不同的线性层(k_proj、q_proj、v_proj)来得到的。而 Cross-Attention 的 “Cross” 则是因为在 Transformer 中,它的 Key、Value 来自 Encoder 中的输出,而 Query 则是 Decoder 中的输入。 ...

 · 4 min · Ruoying Tan

不等式证明

题目描述 已知: $a > 0, b > 0$ 且 $a + b = 2$。 求证: $\sqrt{a+1} + \sqrt{b+1} \le 2\sqrt{2}$。 证明过程 方法一:利用最基础的代数运算性质 要证:$\sqrt{a+1}+\sqrt{b+1} \le 2\sqrt{2}$ 即证:$(\sqrt{a+1}+\sqrt{b+1})^2 \le (2\sqrt{2})^2$ 化简可得:$a+1+2\sqrt{(a+1)(b+1)}+b+1 \le 8$ 整理可得:$a+b+2+2\sqrt{ab+a+b+1} \le 8$ 代入$a+b=2$可得:$4+2\sqrt{ab+3} \le 8$ 即$\sqrt{ab+3} \le 2$ 两边平方可得:$ab \le 1$ 代入$a+b=2$可得:$a(2-a) \le 1$ 由于$a>0$且$b=2-a>0$,故$0<a<2$ 由二次函数单调性可知当$a=1$时$a(2-a)$最大值为1 即$a(2-a) \le 1$成立,原不等式得证 方法二:利用柯西不等式 (Cauchy-Schwarz Inequality) 根据柯西不等式:$(x_1y_1 + x_2y_2)^2 \le (x_1^2 + x_2^2)(y_1^2 + y_2^2)$ 令 $x_1 = \sqrt{a+1}, x_2 = \sqrt{b+1}$, $y_1 = 1, y_2 = 1$。 ...

 · 1 min · Ruoying Tan

2025年寒假英语作文

应用文 - Should We Establish a Graffiti Wall? 题目 你校英文报 Campus Culture 栏目正在开展关于是否设立涂鸦墙(graffiti wall)讨论。请你写一篇短文投稿,内容包括: 1. 你的见解; 2. 你的建议。 范文 Recently, our school newspaper Campus Culture has initiated a discussion regarding(发起了一场关于…的讨论) the establishment of a graffiti wall on campus. Personally, I heartily endorse(由衷地支持) this proposal. To begin with, a graffiti wall serves as an ideal vehicle(是…的理想载体) for students to unleash their creativity(释放他们的创造力) and vent their emotions(宣泄他们的情绪). Given the grueling academic schedule(鉴于繁重的学业安排), a designated space for artistic expression acts as a stress-reliever(起到了减压作用), fostering individuality and imagination. Moreover, a well-curated graffiti wall can breathe new life into(为…注入新的活力) our campus culture, transforming dull corners into vibrant galleries. Rather than leaving random scribbles on desks, students are encouraged to showcase their talents in a more constructive and organized manner. ...

 · 3 min · Ruoying Tan