<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://yuhangwu.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://yuhangwu.com/" rel="alternate" type="text/html" hreflang="en" /><updated>2026-07-21T12:33:25+02:00</updated><id>https://yuhangwu.com/feed.xml</id><title type="html">Yuhang Wu</title><subtitle>Yuhang (Johann) Wu — MSc student in Sound and Music Computing at Universitat Pompeu Fabra, Barcelona. Research on music information retrieval, LLMs for music, and audio AI. Publications at ISMIR and ACL.</subtitle><author><name>Yuhang (Johann) Wu</name><email>yuhang.wu01@estudiant.upf.edu</email></author><entry xml:lang="zh"><title type="html">视觉的由来 | Receptive Fields, Binocular Interaction and Functional Architecture in the Cat’s Visual Cortex</title><link href="https://yuhangwu.com/blog/2023/11/origin-of-vision/" rel="alternate" type="text/html" title="视觉的由来 | Receptive Fields, Binocular Interaction and Functional Architecture in the Cat’s Visual Cortex" /><published>2023-11-02T05:00:00+01:00</published><updated>2023-11-02T05:00:00+01:00</updated><id>https://yuhangwu.com/blog/2023/11/origin-of-vision</id><content type="html" xml:base="https://yuhangwu.com/blog/2023/11/origin-of-vision/"><![CDATA[<p>先占个坑</p>

<p>瞅了一眼论文发现一时半会看不懂，留着长个教训，没事干别挖坑</p>]]></content><author><name>Yuhang (Johann) Wu</name><email>yuhang.wu01@estudiant.upf.edu</email></author><category term="computer-vision" /><category term="classics" /><summary type="html"><![CDATA[先占个坑]]></summary></entry><entry xml:lang="zh"><title type="html">Transformer | Attention Is All You Need</title><link href="https://yuhangwu.com/blog/2023/11/transformer-attention-is-all-you-need/" rel="alternate" type="text/html" title="Transformer | Attention Is All You Need" /><published>2023-11-01T05:00:00+01:00</published><updated>2023-11-01T05:00:00+01:00</updated><id>https://yuhangwu.com/blog/2023/11/transformer-attention-is-all-you-need</id><content type="html" xml:base="https://yuhangwu.com/blog/2023/11/transformer-attention-is-all-you-need/"><![CDATA[<h2 id="自注意力机制">自注意力机制</h2>

<p>$Q（query）,K（key）,V（value）$分别为一个输入的原始矩阵和一个可学习的矩阵进行叉乘得来的，假设原有的输入矩阵为$X$,则$Q = X \times W_Q$。</p>

<p>可以在感性上对QKV进行以下理解：</p>

<ul>
  <li>Query(to match others)：可以看作是在搜索引擎中输入查找的关键字，其中包含了需要查询的信息，也可以理解成借阅者在图书馆需要借到的书的信息</li>
  <li>Key(to be matched)：需要被查询的信息，如搜索引擎中原始信息片段的描述信息或者是一张可以描述书籍的索引卡</li>
  <li>Attention(Q, K)，即$Q \cdot K$, 可以看成是QK之间的匹配程度，即按照匹配程度给予相应的Q，K对一定的评分，在经过SoftMax之后该评分转化成为相应位置的QK匹配成功的可能性（能这么理解吗？）</li>
  <li>Value(information to be extracted)：信息本身，即搜索引擎指向的网址或者是图书馆的书藉本身</li>
</ul>

<p>在已经拥有了Q，K，V之后，首先对Q，K进行点乘，此处可以联系向量乘法的意义（向量$A\cdot B$可以看作A在B上的投影长度与B长度的乘积，代表着两个向量在同一方向上的匹配度），即使用点乘计算query和key之间的匹配度，得到的Attention(Q, K)可以用作后续的提取信息。将Attention(Q, K)与V进行点积就可以得出提取之后的信息，即理想中的有用信息。</p>

<p>本文中注意力的计算公式为：</p>

\[Attention(Q,K,V)=softmax(\frac{QK^T}{\sqrt{d_k}})V\]

<p>其解释说明为： <em>Dot-product attention is identical to our algorithm, except for the scaling factor of $\frac{1}{\sqrt{d_k}}$</em></p>

<p>下面让我们理解一下为什么Dot-product attention is identical to our algorithm</p>

<p>假设矩阵Q、K、V都是一个由很多向量构成的矩阵（如一个由许多词向量构成的句子），如下所示Q：</p>

\[\begin{equation}Q=
 \left[
 \begin{array}{ccc}
     q_{00} &amp; q_{01} &amp; q_{02}  \\
     q_{10} &amp; q_{11} &amp; q_{12}  \\
     q_{20} &amp; q_{21} &amp; q_{22} 
 \end{array}
 \right]        
 \end{equation}\]

<p>其中每一行可以看作一个向量（也就是一个单词），矩阵可以被记为：</p>

\[\begin{equation}Q=
 \left[
 \begin{array}{ccc}
     q_{0}  \\
     q_{1}  \\
     q_{2} 
 \end{array}
 \right]        
 \end{equation}\]

\[\begin{equation}K=
 \left[
 \begin{array}{ccc}
     k_{0}  \\
     k_{1}  \\
     k_{2} 
 \end{array}
 \right]        
 \end{equation}\]

\[\begin{equation}V=
 \left[
 \begin{array}{ccc}
     v_{0}  \\
     v_{1}  \\
     v_{2} 
 \end{array}
 \right]        
 \end{equation}\]

<p>此时：</p>

\[\begin{equation}\begin{aligned}Q\times K^T &amp;=
 \left[\begin{array}{ccc}
     q_{0}  \\
     q_{1}  \\
     q_{2} 
 \end{array}\right]\times\left[\begin{array}{ccc}
     k_{0}  &amp;
     k_{1}  &amp;
     k_{2} 
 \end{array}\right]\\&amp;=\left[\begin{array}{ccc}
     q_{0}\cdot k_{0}  &amp;q_{0}\cdot k_{1}&amp;q_{0}\cdot k_{2}\\
     q_{1}\cdot k_{0}  &amp;q_{1}\cdot k_{1}&amp;q_{1}\cdot k_{2}\\
     q_{2}\cdot k_{0}  &amp;q_{2}\cdot k_{1}&amp;q_{2}\cdot k_{2}
 \end{array}\right]\\
 &amp;=
 \left[\begin{array}{ccc}
     a_{00}  &amp;a_{01}&amp;a_{02}\\
     a_{10}&amp;a_{11}&amp;a_{12}\\
     a_{20}&amp;a_{21}&amp;a_{22}
 \end{array}\right]\end{aligned}\end{equation}\]

<p>在生成的$3\times3$矩阵中每一项都是原有的一个词向量与自己的dot product，即$Attention(Q_n, K_n)$，整个矩阵实现了Q，K两个矩阵的每一向量的两两点乘。</p>

<p>假设原有的矩阵$X$是一句话:「<strong>I love cats</strong>」</p>

\[\begin{equation}Q/K/V=
 \left[
 \begin{array}{ccc}
     q_{0}/k_{0}/v_{0}  \\
     q_{1}/k_{1}/v_{1}  \\
     q_{2}/k_{2}/v_{2} 
 \end{array}
 \right] \underleftrightarrow{\text{  对应  }}\left[
 \begin{array}{ccc}
     I  \\
     love  \\
     cats 
 \end{array}
 \right]\end{equation}\]

<p>那么$Q\times K^T$为：</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>I</th>
      <th>love</th>
      <th>cats</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>I</strong></td>
      <td>$q_0\cdot k_0(a_{00})$</td>
      <td>$q_0\cdot k_1$</td>
      <td>$q_0\cdot k_2$</td>
    </tr>
    <tr>
      <td><strong>love</strong></td>
      <td>$q_1\cdot k_0$</td>
      <td>$q_1\cdot k_1$</td>
      <td>$q_1\cdot k_2$</td>
    </tr>
    <tr>
      <td><strong>cats</strong></td>
      <td>$q_2\cdot k_0$</td>
      <td>$q_2\cdot k_1(a_{21})$</td>
      <td>$q_2\cdot k_2$</td>
    </tr>
  </tbody>
</table>

<p>此时$q_0k_0(a_{00})$即为单词「<strong>I</strong>」与「<strong>I</strong>」的注意力分数(query和key的相似度)，同理$q_2k_1(a_{21})$则为单词「<strong>cats</strong>」与「<strong>love</strong>」的注意力分数</p>

<p>至此我们已经成功得到了$Attention(Q, K)$, 先暂时忽略$\sqrt{d_k}和softmax$，让我们看看$Q\times K^T\times V$:</p>

<p>结合（6）我们可以得出：</p>

\[\begin{equation}\begin{aligned} Q\times K^T \times V &amp;=
 \left[\begin{array}{ccc}
     a_{00}  &amp;a_{01}&amp;a_{02}\\
     a_{10}&amp;a_{11}&amp;a_{12}\\
     a_{20}&amp;a_{21}&amp;a_{22}
 \end{array}\right]
 \times 
 \left[
 \begin{array}{ccc}
     v_{0}  \\
     v_{1}  \\
     v_{2} 
 \end{array}
 \right]\\
 &amp;= 
 \left[\begin{array}{ccc}
     a_{00}\cdot v_0  +a_{01}\cdot v_1+a_{02}\cdot v_2\\
     a_{10}\cdot v_0  +a_{11}\cdot v_1+a_{12}\cdot v_2\\
     a_{20}\cdot v_0  +a_{21}\cdot v_1+a_{22}\cdot v_2
 \end{array}\right] \underleftarrow{\text{  关联最强(最需要注意)  }}\left[
 \begin{array}{ccc}
     I  \\
     love  \\
     cats 
 \end{array}
 \right]\end{aligned}\end{equation}\]

<p>其结果的每一行可以看作是对Value不同项的加权求和，每一行的输出为与X关联最强的元素，在上述例子中$v_0,v_1,v_2$分别与「<strong>I</strong>」「<strong>love</strong>」「<strong>cats</strong>」相关联，这一步所做的是针对每一个单词按照不同的比例将这三个单词提取出来，例如 $a_{00}\cdot v_0  +a_{01}\cdot v_1+a_{02}\cdot v_2$是与单词「<strong>I</strong>」关联最强的元素。</p>

<p>以上过程在<a href="https://speech.ee.ntu.edu.tw/~tlkagk/courses/ML_2019/Lecture/Transformer%20(v5).pptx">李宏毅老师的ppt</a>中有详细说明</p>

<p><img src="https://i.imgur.com/CUY9bvM.png" alt="image-20231101005051329" /></p>]]></content><author><name>Yuhang (Johann) Wu</name><email>yuhang.wu01@estudiant.upf.edu</email></author><category term="deep-learning" /><category term="transformer" /><summary type="html"><![CDATA[自注意力机制]]></summary></entry></feed>