{"id":435,"date":"2024-04-18T06:35:35","date_gmt":"2024-04-18T14:35:35","guid":{"rendered":"https:\/\/karasu.tech\/?p=435"},"modified":"2025-01-31T06:06:45","modified_gmt":"2025-01-31T14:06:45","slug":"transformer%e8%ae%ad%e7%bb%83%e6%b7%b1%e5%85%a5%e6%b5%85%e5%87%ba%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88%e7%9c%9f%e7%9a%84%e6%b2%a1%e5%ad%a6%e4%bc%9a%ef%bc%8c%e5%85%88%e8%ae%b0%e7%9d%80","status":"publish","type":"post","link":"https:\/\/karasu.tech\/?p=435","title":{"rendered":"Transformer\u8bad\u7ec3\u6df1\u5165\u6d45\u51fa\u5b66\u4e60\u7b14\u8bb0\uff08\u7b97\u662f\u5427\uff09"},"content":{"rendered":"<h3><strong>\u4e00\u3001Transformer\u7684\u4e09\u5927\u6838\u5fc3\u673a\u5236<\/strong><\/h3>\n<h4><strong>1. \u81ea\u6ce8\u610f\u529b\uff08Self-Attention\uff09\u2014\u2014 \u5168\u5c40\u5173\u8054\u5206\u6790\u5668<\/strong><\/h4>\n<ul>\n<li><strong>\u95ee\u9898<\/strong>\uff1a\u4f20\u7edf\u6a21\u578b\uff08\u5982RNN\uff09\u53ea\u80fd\u9010\u4e2a\u5904\u7406\u8bcd\uff0c\u65e0\u6cd5\u76f4\u63a5\u770b\u5230\u5168\u5c40\u5173\u7cfb\u3002<\/li>\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u81ea\u6ce8\u610f\u529b\u8ba9\u6bcf\u4e2a\u8bcd\u540c\u65f6\u201c\u770b\u5230\u201d\u6240\u6709\u5176\u4ed6\u8bcd\uff0c\u5e76\u8ba1\u7b97\u5b83\u4eec\u7684\u91cd\u8981\u6027\u3002<\/li>\n<li><strong>\u5177\u4f53\u6b65\u9aa4<\/strong>\uff08\u7528\u300c\u67e5\u5b57\u5178\u300d\u7c7b\u6bd4\uff09\uff1a\n<ol>\n<li><strong>\u751f\u6210Query\u3001Key\u3001Value<\/strong>\uff1a<\/li>\n<\/ol>\n<ul>\n<li>\u6bcf\u4e2a\u8bcd\u751f\u6210\u4e09\u628a\u201c\u94a5\u5319\u201d\uff1aQuery\uff08\u6211\u8981\u627e\u4ec0\u4e48\uff09\u3001Key\uff08\u6211\u6709\u4ec0\u4e48\u6807\u7b7e\uff09\u3001Value\uff08\u6211\u5b9e\u9645\u7684\u5185\u5bb9\uff09<\/li>\n<li><em>\u7c7b\u6bd4<\/em>\uff1a\u56fe\u4e66\u9986\u6bcf\u672c\u4e66\u6709\u7d22\u5f15\u6807\u7b7e\uff08Key\uff09\u3001\u641c\u7d22\u5173\u952e\u8bcd\uff08Query\uff09\u3001\u4e66\u7684\u5185\u5bb9\uff08Value\uff09\n<ol start=\"2\">\n<li><strong>\u8ba1\u7b97\u6ce8\u610f\u529b\u6743\u91cd<\/strong>\uff1a<\/li>\n<\/ol>\n<\/li>\n<li>\u7528Query\u548c\u6240\u6709Key\u505a\u70b9\u79ef\uff08\u76f8\u4f3c\u5ea6\u8ba1\u7b97\uff09\uff0c\u518d\u901a\u8fc7Softmax\u5f52\u4e00\u5316\u4e3a\u6982\u7387<\/li>\n<li><em>\u516c\u5f0f\u7b80\u5316<\/em>\uff1a<code>\u6ce8\u610f\u529b\u5206\u6570 = Softmax(Query\u00b7Key^T \/ \u221ad_k)<\/code><\/li>\n<li><em>\u4e3a\u4ec0\u4e48\u9664\u4ee5\u221ad_k<\/em>\uff1f\u9632\u6b62\u70b9\u79ef\u8fc7\u5927\u5bfc\u81f4\u68af\u5ea6\u6d88\u5931\uff08\u7c7b\u4f3c\u201c\u538b\u7f29\u6570\u503c\u8303\u56f4\u201d\uff09\n<ol start=\"3\">\n<li><strong>\u52a0\u6743\u805a\u5408Value<\/strong>\uff1a<\/li>\n<\/ol>\n<\/li>\n<li>\u7528\u6ce8\u610f\u529b\u6743\u91cd\u5bf9Value\u52a0\u6743\u6c42\u548c\uff0c\u5f97\u5230\u5f53\u524d\u8bcd\u7684\u201c\u65b0\u8868\u793a\u201d\u3002<\/li>\n<li><em>\u6548\u679c<\/em>\uff1a\u6bd4\u5982\u201c\u5b83\u201d\u8fd9\u4e2a\u8bcd\uff0c\u53ef\u80fd\u805a\u5408\u4e86\u201c\u732b\u201d\u548c\u201c\u9c7c\u201d\u7684\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2. \u591a\u5934\u6ce8\u610f\u529b\uff08Multi-Head Attention\uff09\u2014\u2014 \u591a\u4e13\u5bb6\u59d4\u5458\u4f1a<\/h4>\n<ul>\n<li><strong>\u95ee\u9898<\/strong>\uff1a\u5355\u4e00\u6ce8\u610f\u529b\u53ea\u80fd\u6355\u6349\u4e00\u79cd\u7c7b\u578b\u7684\u5173\u7cfb\uff08\u5982\u8bed\u6cd5\u6216\u8bed\u4e49\uff09\u3002<\/li>\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u5e76\u884c\u591a\u4e2a\u72ec\u7acb\u7684\u6ce8\u610f\u529b\u5934\uff08\u59828\u4e2a\u5934\uff09\uff0c\u6bcf\u4e2a\u5934\u5173\u6ce8\u4e0d\u540c\u6a21\u5f0f\u3002<\/li>\n<li><em>\u7c7b\u6bd4<\/em>\uff1a\u8ba98\u4e2a\u4e13\u5bb6\u5206\u522b\u5206\u6790\u53e5\u5b50\uff08\u4e00\u4e2a\u770b\u4e3b\u8c13\u5bbe\uff0c\u4e00\u4e2a\u770b\u60c5\u611f\uff0c\u4e00\u4e2a\u770b\u6307\u4ee3\u5173\u7cfb\u2026\uff09\uff0c\u6700\u540e\u5408\u5e76\u7ed3\u679c\u3002<\/li>\n<li><strong>\u5177\u4f53\u6b65\u9aa4<\/strong>\uff1a\n<ol>\n<li>\u5c06Query\u3001Key\u3001Value\u62c6\u5206\u6210\u591a\u4e2a\u5934\uff08\u4f8b\u59828\u5934\uff09\u3002<\/li>\n<li>\u6bcf\u4e2a\u5934\u72ec\u7acb\u8ba1\u7b97\u6ce8\u610f\u529b\uff0c\u5f97\u52308\u7ec4\u4e0d\u540c\u7684\u7279\u5f81\u3002<\/li>\n<li>\u62fc\u63a5\u6240\u6709\u7ed3\u679c\u5e76\u901a\u8fc7\u7ebf\u6027\u5c42\u878d\u5408\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h4><strong>3. \u4f4d\u7f6e\u7f16\u7801\uff08Positional Encoding\uff09\u2014\u2014 \u8bcd\u7684\u201c\u4f4d\u7f6eGPS\u201d<\/strong><\/h4>\n<ul>\n<li><strong>\u95ee\u9898<\/strong>\uff1aTransformer\u6ca1\u6709\u987a\u5e8f\u5904\u7406\uff0c\u9700\u8981\u663e\u5f0f\u544a\u8bc9\u6a21\u578b\u8bcd\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u4e3a\u6bcf\u4e2a\u4f4d\u7f6e\u751f\u6210\u552f\u4e00\u7684\u7f16\u7801\u5411\u91cf\uff0c\u52a0\u5230\u8bcd\u5d4c\u5165\u4e0a\u3002<\/li>\n<li><strong>\u4e24\u79cd\u7f16\u7801\u65b9\u5f0f<\/strong>\uff1a\n<ul>\n<li><strong>\u6b63\u5f26\u51fd\u6570\u7f16\u7801<\/strong>\uff08\u539f\u59cb\u8bba\u6587\uff09\uff1a<\/li>\n<li>\u7528\u4e0d\u540c\u9891\u7387\u7684\u6b63\u5f26\/\u4f59\u5f26\u51fd\u6570\u751f\u6210\u4f4d\u7f6e\u5411\u91cf\u3002<\/li>\n<li><em>\u4f18\u70b9<\/em>\uff1a\u6a21\u578b\u80fd\u5b66\u4f1a\u76f8\u5bf9\u4f4d\u7f6e\uff08\u5982\u4f4d\u7f6e5\u548c\u4f4d\u7f6e6\u7684\u5173\u7cfb\u7c7b\u4f3c\u4f4d\u7f6e10\u548c11\uff09<\/li>\n<li><strong>\u53ef\u5b66\u4e60\u7f16\u7801<\/strong>\uff08BERT\u7b49\u6a21\u578b\u4f7f\u7528\uff09\uff1a<\/li>\n<li>\u76f4\u63a5\u8bad\u7ec3\u4e00\u4e2a\u4f4d\u7f6e\u5d4c\u5165\u77e9\u9635\uff0c\u7c7b\u4f3c\u8bcd\u5d4c\u5165\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3><strong>\u4e8c\u3001\u8bad\u7ec3\u7ec6\u8282\u6df1\u5165<\/strong><\/h3>\n<h4><strong>1. \u89e3\u7801\u5668\u7684\u63a9\u7801\u6ce8\u610f\u529b\uff08Masked Attention\uff09<\/strong><\/h4>\n<ul>\n<li><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u63a9\u7801<\/strong>\uff1f\u8bad\u7ec3\u65f6\u89e3\u7801\u5668\u53ea\u80fd\u770b\u5230\u5df2\u751f\u6210\u7684\u8bcd\uff08\u4e0d\u80fd\u4f5c\u5f0a\u770b\u672a\u6765\uff09\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f<\/strong>\uff1a\u5728\u6ce8\u610f\u529b\u6743\u91cd\u8ba1\u7b97\u65f6\uff0c\u7528\u201c\u4e0a\u4e09\u89d2\u63a9\u7801\u77e9\u9635\u201d\u906e\u6321\u672a\u6765\u4f4d\u7f6e\u3002\n<ul>\n<li><em>\u793a\u4f8b<\/em>\uff1a\u751f\u6210\u7b2c3\u4e2a\u8bcd\u65f6\uff0c\u53ea\u80fd\u5173\u6ce8\u7b2c1\u30012\u4e2a\u8bcd\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4><strong>2. \u6b8b\u5dee\u8fde\u63a5\uff08Residual Connection\uff09\u4e0e\u5c42\u5f52\u4e00\u5316\uff08LayerNorm\uff09<\/strong><\/h4>\n<ul>\n<li><strong>\u6b8b\u5dee\u8fde\u63a5<\/strong>\uff1a\u6bcf\u4e00\u5c42\u7684\u8f93\u51fa = \u539f\u59cb\u8f93\u5165 + \u5904\u7406\u540e\u7684\u7ed3\u679c\u3002\n<ul>\n<li><em>\u4f5c\u7528<\/em>\uff1a\u9632\u6b62\u6df1\u5c42\u7f51\u7edc\u68af\u5ea6\u6d88\u5931\uff08\u7c7b\u4f3c\u201c\u4fdd\u7559\u539f\u59cb\u8bb0\u5fc6\uff0c\u53ea\u5b66\u5dee\u5f02\u90e8\u5206\u201d\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5c42\u5f52\u4e00\u5316<\/strong>\uff1a\u5bf9\u6bcf\u4e00\u5c42\u7684\u8f93\u51fa\u505a\u6807\u51c6\u5316\uff08\u5747\u503c\u4e3a0\uff0c\u65b9\u5dee\u4e3a1\uff09\u3002\n<ul>\n<li><em>\u4f5c\u7528<\/em>\uff1a\u7a33\u5b9a\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u52a0\u901f\u6536\u655b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4><strong>3. \u524d\u9988\u7f51\u7edc\uff08Feed-Forward Network\uff09\u2014\u2014 \u975e\u7ebf\u6027\u589e\u5f3a\u5668<\/strong><\/h4>\n<ul>\n<li><strong>\u7ed3\u6784<\/strong>\uff1a\u4e24\u5c42\u5168\u8fde\u63a5\u5c42\uff0c\u4e2d\u95f4\u7528ReLU\u6fc0\u6d3b\u51fd\u6570\u3002\n<ul>\n<li><em>\u516c\u5f0f<\/em>\uff1a<code>FFN(x) = ReLU(xW1 + b1)W2 + b2<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u4e3a\u6ce8\u610f\u529b\u8f93\u51fa\u589e\u52a0\u975e\u7ebf\u6027\u53d8\u6362\uff0c\u589e\u5f3a\u6a21\u578b\u8868\u8fbe\u80fd\u529b\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3><strong>\u4e09\u3001Transformer\u4e3a\u4f55\u80fd\u540a\u6253RNN\uff1f<\/strong><\/h3>\n<h4><strong>1. \u5e76\u884c\u5316\u8ba1\u7b97<\/strong><\/h4>\n<ul>\n<li>RNN\u5fc5\u987b\u6309\u5e8f\u5217\u9010\u6b65\u8ba1\u7b97\uff0cTransformer\u6240\u6709\u4f4d\u7f6e\u540c\u65f6\u5904\u7406\u3002\n<ul>\n<li><em>\u7ed3\u679c<\/em>\uff1aGPU\u5e76\u884c\u52a0\u901f\uff0c\u8bad\u7ec3\u901f\u5ea6\u63d0\u534710\u500d\u4ee5\u4e0a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4><strong>2. \u957f\u8ddd\u79bb\u4f9d\u8d56\u76f4\u63a5\u5efa\u6a21<\/strong><\/h4>\n<ul>\n<li>RNN\u9760\u201c\u8bb0\u5fc6\u201d\u4f20\u9012\u4fe1\u606f\uff0c\u8ddd\u79bb\u8d8a\u8fdc\u4fe1\u606f\u4e22\u5931\u8d8a\u591a\u3002<\/li>\n<li>Transformer\u7684\u81ea\u6ce8\u610f\u529b\u4e00\u6b65\u5230\u4f4d\u6355\u6349\u4efb\u610f\u8ddd\u79bb\u5173\u7cfb\uff08\u5982\u53e5\u9996\u548c\u53e5\u5c3e\uff09\u3002<\/li>\n<\/ul>\n<h4><strong>3. \u6a21\u5757\u5316\u8bbe\u8ba1<\/strong><\/h4>\n<ul>\n<li>\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u7531\u76f8\u540c\u7ed3\u6784\u7684\u5c42\u5806\u53e0\u800c\u6210\uff0c\u6613\u4e8e\u6269\u5c55\uff08\u5982GPT-3\u670996\u5c42\uff09\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3><strong>\u56db\u3001\u5173\u952e\u95ee\u9898\u89e3\u6790<\/strong><\/h3>\n<h4><strong>Q1\uff1a\u6ce8\u610f\u529b\u6743\u91cd\u53ef\u89c6\u5316\u540e\u80fd\u770b\u51fa\u4ec0\u4e48\uff1f<\/strong><\/h4>\n<ul>\n<li>\u4f8b\u5982\u5728\u7ffb\u8bd1\u4e2d\uff0c\u6a21\u578b\u53ef\u80fd\u4f1a\u5bf9\u201cit\u201d\u5206\u914d\u9ad8\u6743\u91cd\u5230\u201c\u732b\u201d\u548c\u201c\u9c7c\u201d\uff0c\u663e\u793a\u6307\u4ee3\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<h4><strong>Q2\uff1aTransformer\u5982\u4f55\u5904\u7406\u4e00\u8bcd\u591a\u4e49\uff1f<\/strong><\/h4>\n<ul>\n<li>\u901a\u8fc7\u4e0a\u4e0b\u6587\u6ce8\u610f\u529b\u52a8\u6001\u8c03\u6574\u8bcd\u5411\u91cf\u3002\u4f8b\u5982\u201c\u82f9\u679c\u201d\u5728\u201c\u5403\u82f9\u679c\u201d\u548c\u201c\u82f9\u679c\u624b\u673a\u201d\u4e2d\u4f1a\u805a\u5408\u4e0d\u540c\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h4><strong>Q3\uff1a\u4e3a\u4ec0\u4e48\u5927\u6a21\u578b\uff08\u5982GPT\uff09\u53ea\u7528\u89e3\u7801\u5668\uff1f<\/strong><\/h4>\n<ul>\n<li>GPT\u8fd9\u7c7b\u751f\u6210\u6a21\u578b\u4e0d\u9700\u8981\u7f16\u7801\u5668-\u89e3\u7801\u5668\u7ed3\u6784\uff0c\u76f4\u63a5\u7528\u5806\u53e0\u7684\u89e3\u7801\u5668\u5c42\uff08\u5e26\u63a9\u7801\u6ce8\u610f\u529b\uff09\u81ea\u56de\u5f52\u751f\u6210\u6587\u672c\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3><strong>\u4e94\u3001\u603b\u7ed3\uff1aTransformer\u7684\u7ec8\u6781\u79d8\u5bc6<\/strong><\/h3>\n<pre><code class=\"line-numbers\">\u8f93\u5165 \u2192 \u8bcd\u5d4c\u5165 + \u4f4d\u7f6e\u7f16\u7801 \u2192 \u81ea\u6ce8\u610f\u529b\uff08\u5168\u5c40\u5173\u8054\uff09 \u2192 \u524d\u9988\u7f51\u7edc\uff08\u975e\u7ebf\u6027\u589e\u5f3a\uff09 \u2192 \u6b8b\u5dee\u8fde\u63a5\uff08\u4fdd\u5e95\u8bb0\u5fc6\uff09 \u2192 \u5c42\u5f52\u4e00\u5316\uff08\u7a33\u5b9a\u8bad\u7ec3\uff09 \u2192 \u91cd\u590dN\u6b21 \u2192 \u8f93\u51fa\n<\/code><\/pre>\n<p><strong>\u6838\u5fc3\u516c\u5f0f\u603b\u7ed3<\/strong>\uff1a<br \/>\n&#8211; \u6ce8\u610f\u529b\uff1a<code>Output = Softmax(QK^T\/\u221ad_k) \u00b7 V<\/code><br \/>\n&#8211; \u6b8b\u5dee\u8fde\u63a5\uff1a<code>x = x + Sublayer(LayerNorm(x))<\/code><br \/>\n&#8211; \u5c42\u5f52\u4e00\u5316\uff1a<code>LayerNorm(x) = (x - \u03bc)\/\u03c3 * \u03b3 + \u03b2<\/code><\/p>\n<hr \/>\n<ul>\n<li>\u7cbe\u8bfb\u300aAttention Is All You Need\u300b<\/li>\n<li>\u4f7f\u7528<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/tensorflow\/tensor2tensor\">Tensor2Tensor<\/a>\u53ef\u89c6\u5316\u6ce8\u610f\u529b\u6743\u91cd\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u771f\u7684\u6ca1\u5b66\u4f1a\uff0c\u5148\u8bb0\u7740\u3002\u3002\u3002<\/p>\n","protected":false},"author":2,"featured_media":438,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,2],"tags":[22,11,26],"class_list":["post-435","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-ai-images","tag-ai","tag-11","tag-26"],"_links":{"self":[{"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/karasu.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":6,"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions\/452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/karasu.tech\/index.php?rest_route=\/wp\/v2\/media\/438"}],"wp:attachment":[{"href":"https:\/\/karasu.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/karasu.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/karasu.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}