{"id":44,"date":"2026-04-05T20:50:25","date_gmt":"2026-04-05T12:50:25","guid":{"rendered":"http:\/\/zyf.js.cn\/?p=44"},"modified":"2026-04-05T20:59:58","modified_gmt":"2026-04-05T12:59:58","slug":"%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0_2","status":"publish","type":"post","link":"http:\/\/zyf.js.cn\/index.php\/2026\/04\/05\/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0_2\/","title":{"rendered":"\u6df1\u5ea6\u5b66\u4e60\u7b14\u8bb0_2"},"content":{"rendered":"<p>### Transformer<\/p>\n<p>[\u70b9\u51fb\u67e5\u770b\u8bfe\u7a0b\u94fe\u63a5](https:\/\/www.bilibili.com\/video\/BV1YsqSY8EiW?spm_id_from=333.788.videopod.sections&#038;vd_source=5b20ceba8ceda029b4d352ff81c5dd06&#038;p=55)<\/p>\n<p>Transformer\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u795e\u7ecf\u7f51\u7edc\uff0c\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u6838\u5fc3\u4efb\u52a1\u662f\u7406\u89e3\u4e0a\u4e0b\u6587\u548c\u5e8f\u5217\u8f6c\u6362\u3002<\/p>\n<p>\u4f20\u7edf\u7684RNN\u7f51\u7edc\uff0c\u6bcf\u6b21\u8ba1\u7b97\u90fd\u9700\u8981\u4e0a\u4e00\u4e2a\u5355\u5143\u7684\u7ed3\u679c\uff0c\u4e0d\u80fd\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<p>#### \u6ce8\u610f\u529b\u673a\u5236<\/p>\n<p>self-attention\u8981\u505a\u7684\u4e8b\uff1a \u5bf9\u4e8e\u4e00\u4e2a\u5e8f\u5217\uff0c\u8ba9\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u641e\u6e05\u695a\u81ea\u5df1\u548c\u5176\u5b83\u5143\u7d20\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u53e5\u8bdd\u4e2d\u7684\u67d0\u4e2a\u8bcd\uff0c\u8981\u641e\u6e05\u5176\u548c\u4e0a\u4e0b\u6587\u7684\u5173\u7cfb\u3002<\/p>\n<p>##### self_attention\u7684\u8ba1\u7b97\u65b9\u6cd5<\/p>\n<p>###### \u751f\u6210 Q, K, V<\/p>\n<p>\u8f93\u5165\u7684\u5e8f\u5217\uff08\u6bd4\u5982\u4e09\u4e2a\u5355\u8bcd\u7684\u8bcd\u5411\u91cf\uff09\u7ec4\u6210\u4e00\u4e2a\u77e9\u9635 $X$\u3002\u901a\u8fc7\u4e09\u4e2a\u4e0d\u540c\u7684\u6743\u91cd\u77e9\u9635 $W^Q, W^K, W^V$ \u4e0e $X$ \u76f8\u4e58\uff0c\u5f97\u5230\u4e09\u4e2a\u65b0\u7684\u77e9\u9635\uff1a<\/p>\n<p>![image-20260322092403435](\/wp-content\/uploads\/2026\/04\/image-20260322092403435-255&#215;300.png){.alignnone}<\/p>\n<p>$Q$ (Query)\uff1a\u8981\u53bb\u67e5\u8be2\u7684<\/p>\n<p>$K$ (Key)\uff1a\u7b49\u5f85\u88ab\u67e5\u8be2\u7684<\/p>\n<p>$V$ (Value)\uff1a\u5b9e\u9645\u7279\u5f81\u4fe1\u606f<\/p>\n<p>$$Q = XW^Q, \\quad K = XW^K, \\quad V = XW^V$$<\/p>\n<p>###### \u8ba1\u7b97\u6ce8\u610f\u529b\u5f97\u5206<\/p>\n<p>\u7528 $Q$ \u4e58\u4ee5 $K$ \u7684\u8f6c\u7f6e $K^T$\u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u5f85\u67e5\u7684\u5355\u8bcd\uff0c\u5373Q\u4e2d\u7684\u6bcf\u4e00\u884c\u5411\u91cf\uff0c\u4e0e\u5f85\u67e5\u7684\u5355\u8bcd\uff0c\u5373K\u4e2d\u7684\u6bcf\u4e00\u884c\u5411\u91cf\u8ba1\u7b97\u5185\u79ef\uff0c\u5c31\u5f97\u5230\u4e86\u4e24\u4e2a\u8bcd\u4e4b\u95f4\u7684\u539f\u59cb\u76f8\u5173\u6027\u5f97\u5206\u3002\u5bf9\u4e8e\u4e24\u4e2a\u5411\u91cf\uff0c\u8d8a\u76f8\u5173\u5185\u79ef\u8d8a\u5927\uff0c\u5b8c\u5168\u4e0d\u76f8\u5173\u65f6\uff0c\u5185\u79ef\u4e3a0\u3002<\/p>\n<p>\u9664\u4ee5 $\\sqrt{d_k}$\uff08$d_k$ \u662f\u5411\u91cf\u7ef4\u5ea6\uff09\uff0c\u4e3a\u4e86\u9632\u6b62\u5411\u91cf\u7ef4\u5ea6\u8d8a\u5927\u70b9\u79ef\u7ed3\u679c\u8d8a\u5927\u3002<\/p>\n<p>\u5f52\u4e00\u5316\uff08Softmax\uff09\uff0c\u5bf9\u6bcf\u4e00\u884c\u505a Softmax\u3002\u628a\u5f97\u5206\u8f6c\u6362\u6210 $0 \\sim 1$ \u4e4b\u95f4\u7684\u6982\u7387\uff0c\u5f97\u5230\u6ce8\u610f\u529b\u6743\u91cd\u3002<\/p>\n<p>###### \u52a0\u6743\u6c42\u548c<\/p>\n<p>$$\\text{Attention}(Q, K, V) = \\text{softmax}\\left(\\frac{QK^T}{\\sqrt{d_k}}\\right)V$$<\/p>\n<p>V\u5373\u4e0a\u9762\u7684value\u77e9\u9635<\/p>\n<p>#### Multi-headed\u673a\u5236<\/p>\n<p>\u5355\u4e00\u7684\u6ce8\u610f\u529b\u673a\u5236\uff08\u5355\u5934\uff09\u5f88\u96be\u540c\u65f6\u6355\u6349\u5230\u591a\u79cd\u4e0d\u540c\u7684\u8bed\u4e49\u5173\u7cfb\uff0c\u591a\u5934\u673a\u5236\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684head\u5f97\u5230\u591a\u4e2a\u7279\u5f81\u8868\u8fbe\uff0c\u53ef\u4ee5\u7c7b\u6bd4CNN\u4e2d\u7684\u591a\u4e2a\u5377\u79ef\u6838\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u539f\u59cb\u7684 $Q, K, V$ \u901a\u8fc7 $h$ \u7ec4\u4e0d\u540c\u7684\u6743\u91cd\u77e9\u9635 $W^Q_i, W^K_i, W^V_i$ \u8fdb\u884c\u7ebf\u6027\u6620\u5c04\u3002\u5047\u8bbe\u6709 8 \u4e2a\u5934\uff0c\u5c31\u4f1a\u5f97\u5230 8 \u7ec4\u4e0d\u540c\u7684 $Q_i, K_i, V_i$\u3002<\/p>\n<p>\u8fd9 8 \u7ec4\u5411\u91cf\u5404\u81ea\u72ec\u7acb\u5730\u8fdb\u884c\u8fd0\u7b97\uff1a$$\\text{head}_i = \\text{Attention}(Q_i, K_i, V_i)$$<\/p>\n<p>\u628a\u8fd9 8 \u4e2a\u5934\u7684\u8f93\u51fa\u7ed3\u679c\u6a2a\u5411\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u53d8\u6210\u4e00\u4e2a\u5927\u957f\u77e9\u9635\u3002<\/p>\n<p>\u901a\u8fc7\u4e00\u4e2a\u6700\u7ec8\u7684\u6743\u91cd\u77e9\u9635 $W^O$ \u8fdb\u884c\u878d\u5408\uff0c\u628a\u591a\u5934\u7684\u4fe1\u606f\u63c9\u5728\u4e00\u8d77\u3002<\/p>\n<p>#### Decoder\u4e0eMask<\/p>\n<p>Decoder\u7aef\u662f\u8f93\u51fa\u751f\u6210\u7aef\uff0c\u5176\u76ee\u6807\u662f\u751f\u6210\u5e8f\u5217\uff0c\u4f8b\u5982\u7ffb\u8bd1\u51fa\u7684\u82f1\u6587\u53e5\u5b50\u3002<\/p>\n<p>\u4e3b\u8981\u6709\u4e24\u4e2a\u7279\u5f81\uff1a<\/p>\n<p>1.\u751f\u6210\u8bcd\u65f6\uff0c\u5fc5\u987b\u4f9d\u8d56\u524d\u9762\u5df2\u7ecf\u751f\u6210\u7684\u8bcd\u3002<\/p>\n<p>2.\u4e0d\u4ec5\u8981\u770b\u81ea\u5df1\u5199\u4e86\u4ec0\u4e48\uff08Self-Attention\uff09\uff0c\u8fd8\u8981\u7ed3\u5408Encoder\u7aef\u7ed9\u7684\u4fe1\u606f\uff08Cross-Attention\uff09<\/p>\n<p>Mask \u7684\u4f5c\u7528\uff1a \u5f3a\u8feb\u6a21\u578b\u5728\u8ba1\u7b97\u7b2c $i$ \u4e2a\u8bcd\u7684\u6ce8\u610f\u529b\u65f6\uff0c\u628a\u7b2c $i+1$ \u4e2a\u53ca\u4e4b\u540e\u7684\u8bcd\u5168\u90e8\u201c\u6321\u4f4f\u201d\u3002<\/p>\n<p>#### \u591a\u5c42\u5806\u53e0\u4e0e\u4f4d\u7f6e\u7f16\u7801<\/p>\n<p>##### \u591a\u5c42\u5806\u53e0<\/p>\n<p>Transformer \u5e76\u4e0d\u662f\u53ea\u6709\u4e00\u5c42 Encoder \u548c Decoder\uff0c\u901a\u5e38\u4f1a\u5806\u53e0\u5f88\u591a\u5c42\u3002<\/p>\n<p>\u591a\u5c42\u7684\u5806\u53e0\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ed3\u679c\u8d8a\u6765\u8d8a\u5dee\uff0c\u6240\u4ee5\u6bcf\u4e00\u5c42\u90fd\u8981\u914d\u4e0a\u6b8b\u5dee\u94fe\u63a5\uff08\u7c7b\u6bd4\u4e4b\u524dCNN\u4e2d\u7684\u6b8b\u5dee\u7f51\u7edc\uff09\u3002<\/p>\n<p>##### \u4f4d\u7f6e\u7f16\u7801<\/p>\n<p>\u81ea\u6ce8\u610f\u529b\u673a\u5236\uff08Self-Attention\uff09\u662f\u5e76\u884c\u7684\uff0c\u5931\u53bb\u4e86\u8bcd\u5e8f\u4fe1\u606f\u3002<\/p>\n<p>\u4e3a\u4e86\u4fdd\u7559\u8bcd\u5e8f\u4fe1\u606f\uff0c\u6211\u4eec\u5411\u8bcd\u5411\u91cf\u4e2d\u6ce8\u5165\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<p>Transformer \u91c7\u7528\u7684\u662f \u6b63\u5f26\u548c\u4f59\u5f26\u51fd\u6570\u3002<\/p>\n<p>#### Transformer\u6574\u4f53\u67b6\u6784<\/p>\n<p>![image-20260322111338585](\/wp-content\/uploads\/2026\/04\/image-20260322111338585-203&#215;300.png){.alignnone}<\/p>\n<p>Transformer\u7684\u6700\u5927\u4f18\u52bf\u5c31\u662f\u5e76\u884c\u8ba1\u7b97\u3002\u8fd9\u4e2a\u4f18\u52bf\u4e3b\u8981\u662f\u901a\u8fc7\u4e00\u4e0b\u4e09\u4e2a\u65b9\u9762\u5b9e\u73b0\u7684\uff1a<\/p>\n<p>1.\u6d88\u9664\u4e86\u65f6\u95f4\u5e8f\u5217\u4f9d\u8d56\u3002\u5728 Encoder \u7684 Self-Attention \u5c42\uff0c\u53e5\u5b50\u4e2d\u6240\u6709\u7684\u8bcd\u540c\u65f6\u8f93\u5165\u3002\u8ba1\u7b97\u7b2c\u4e00\u4e2a\u8bcd\u5bf9\u5168\u53e5\u7684\u6ce8\u610f\u529b\uff0c\u548c\u8ba1\u7b97\u6700\u540e\u4e00\u4e2a\u8bcd\u5bf9\u5168\u53e5\u7684\u6ce8\u610f\u529b\uff0c\u5728\u6570\u5b66\u4e0a\u662f\u72ec\u7acb\u7684\u3002GPU \u53ef\u4ee5\u540c\u65f6\u5f00\u542f\u51e0\u5343\u4e2a\u7ebf\u7a0b\uff0c\u77ac\u95f4\u7b97\u51fa\u6574\u4e2a $QK^T$ \u77e9\u9635\u3002<\/p>\n<p>2.\u591a\u5934\u6ce8\u610f\u529b\uff08Multi-Head\uff09\u7684\u5e76\u884c\u5b50\u7a7a\u95f4\u3002\u591a \u4e2a\u5934\u5728 GPU \u91cc\u662f\u4f5c\u4e3a\u72ec\u7acb\u7684\u7ef4\u5ea6\u5e76\u884c\u8fd0\u7b97\u7684\u3002\u4e0d\u7528\u4e92\u76f8\u7b49\u5f85\uff0c\u7b97\u5b8c\u540e\u76f4\u63a5\u62fc\u63a5\u5728\u4e00\u8d77\u5373\u53ef\u3002<\/p>\n<p>3.Transformer \u7684\u6bcf\u4e00\u5c42\u7ed3\u6784\u90fd\u662f\u9ad8\u5ea6\u4e00\u81f4\u7684<\/p>\n<p>#### Layer Normalization<\/p>\n<p>\u5c42\u5f52\u4e00\u5316\u7528\u6765\u7a33\u5b9a\u795e\u7ecf\u7f51\u7edc\u7684\u8bad\u7ec3\uff0c\u907f\u514d\u8bad\u7ec3\u4e2d\u4ea7\u751f\u68af\u5ea6\u6d88\u5931\u6216\u68af\u5ea6\u7206\u70b8\u3002\u5c42\u5f52\u4e00\u5316\u4f1a\u5bf9\u795e\u7ecf\u7f51\u7edc\u67d0\u4e00\u5c42\u7684\u6240\u6709\u795e\u7ecf\u5143\u7684\u8f93\u51fa\u7ed3\u679c\u8fdb\u884c\u5f52\u4e00\u5316\u8ba1\u7b97<\/p>\n<p>\u8ba1\u7b97\u516c\u5f0f\uff1a<\/p>\n<p>$$\\hat{x}_i = \\frac{x_i &#8211; \\mu}{\\sqrt{\\sigma^2 + \\epsilon}}$$<\/p>\n<p>\u7531\u4e8e$\\epsilon$\u5f88\u5c0f\uff0c\u53ef\u4ee5\u5c06\u4e0a\u9762\u516c\u5f0f\u8f6c\u5316\u4e3a$$\\hat{x}_i = \\frac{x_i &#8211; \\mu}{\\sigma}$$<\/p>\n<p>&#8212;<\/p>\n<p>**$x_i$**\uff1a\u7b2ci\u4e2a\u795e\u7ecf\u5143\u7684\u8f93\u51fa<\/p>\n<p>**$\\mu$ **\uff1a\u6240\u6709\u795e\u7ecf\u5143\u8f93\u51fa\u7684\u5747\u503c\u3002<\/p>\n<p>$\\sigma$\uff1a\u6807\u51c6\u5dee<\/p>\n<p>$\\hat{x}_i$\uff1a\u5c42\u5f52\u4e00\u5316\u7684\u8ba1\u7b97\u7ed3\u679c<\/p>\n<p>$\\epsilon$\uff1a\u504f\u7f6e\u9879\/\u5e73\u6ed1\u9879\uff0c\u7528\u4e8e\u9632\u6b62\u5206\u6bcd\u4e3a0<\/p>\n<p>$$y_i = \\gamma \\hat{x}_i + \\beta$$<\/p>\n<p>&#8212;<\/p>\n<p>**$\\gamma$**\uff1a\u53ef\u5b66\u4e60\u7684\u7f29\u653e\u53c2\u6570\u3002 <\/p>\n<p>**$\\beta$**\uff1a\u53ef\u5b66\u4e60\u7684\u5e73\u79fb\u53c2\u6570\u3002 <\/p>\n<p>**$y_i$**\uff1aLayerNorm \u6700\u7ec8\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p>\u82e5\u4e0d\u8fdb\u884c\u53c2\u6570\u5b66\u4e60\uff0c\u5219\u4f1a\u4ee5\u4e0a\u9762\u7684\u539f\u59cb\u516c\u5f0f\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transformer\u3001\u6ce8\u610f\u529b\u673a\u5236<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":6,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":54,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions\/54"}],"wp:attachment":[{"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyf.js.cn\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}