{"id":10021252,"date":"2026-02-15T15:34:55","date_gmt":"2026-02-15T07:34:55","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021252"},"modified":"2026-02-15T15:34:55","modified_gmt":"2026-02-15T07:34:55","slug":"transformer%e7%9a%84%e8%87%aa%e6%b3%a8%e6%84%8f%e5%8a%9b%e6%9c%ba%e5%88%b6%e4%b8%8e%e4%bd%8d%e7%bd%ae%e7%bc%96%e7%a0%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021252","title":{"rendered":"Transformer\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e0e\u4f4d\u7f6e\u7f16\u7801"},"content":{"rendered":"<h2>Transformer\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e0e\u4f4d\u7f6e\u7f16\u7801<\/h2>\n<h3>\u6458\u8981<\/h3>\n<p>Transformer\u4f5c\u4e3a\u4e00\u79cd\u5b8c\u5168\u57fa\u4e8e\u6ce8\u610f\u529b\u673a\u5236\u7684\u6df1\u5ea6\u5b66\u4e60\u67b6\u6784\uff0c\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u548c\u5e8f\u5217\u5efa\u6a21\u9886\u57df\u5f97\u5230\u5e7f\u6cdb\u5e94\u7528\u3002\u672c\u6587\u7cfb\u7edf\u9610\u8ff0\u4e86Transformer\u7684\u57fa\u672c\u539f\u7406\u3001\u81ea\u6ce8\u610f\u529b\u673a\u5236\u548c\u4f4d\u7f6e\u7f16\u7801\uff0c\u91cd\u70b9\u5206\u6790\u4e86\u7f16\u7801\u5668\u3001\u89e3\u7801\u5668\u3001\u81ea\u6ce8\u610f\u529b\u548c\u524d\u9988\u7f51\u7edc\u7b49\u6838\u5fc3\u5185\u5bb9\u3002\u6df1\u5165\u63a2\u8ba8\u4e86\u591a\u5934\u6ce8\u610f\u529b\u3001\u4f4d\u7f6e\u7f16\u7801\u3001\u6b8b\u5dee\u8fde\u63a5\u7b49\u5173\u952e\u6280\u672f\uff0c\u5e76\u4ece\u7406\u8bba\u89d2\u5ea6\u5206\u6790\u4e86Transformer\u7684\u8868\u8fbe\u80fd\u529b\u548c\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002\u901a\u8fc7\u5bf9\u5b9e\u9645\u6570\u636e\u96c6\u548c\u5e94\u7528\u6848\u4f8b\u7684\u7814\u7a76\uff0c\u9a8c\u8bc1\u4e86Transformer\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\u7684\u6709\u6548\u6027\uff0c\u4e3a\u5e8f\u5217\u5efa\u6a21\u63d0\u4f9b\u4e86\u7406\u8bba\u4f9d\u636e\u548c\u5b9e\u8df5\u6307\u5bfc\u3002<\/p>\n<p><strong>\u5173\u952e\u8bcd<\/strong>\uff1aTransformer\uff1b\u81ea\u6ce8\u610f\u529b\u673a\u5236\uff1b\u4f4d\u7f6e\u7f16\u7801\uff1b\u7f16\u7801\u5668\uff1b\u89e3\u7801\u5668<\/p>\n<h3>1. \u5f15\u8a00<\/h3>\n<p>Transformer\u7531Vaswani\u7b49\u4eba\u4e8e2017\u5e74\u63d0\u51fa\uff0c\u662f\u4e00\u79cd\u5b8c\u5168\u57fa\u4e8e\u6ce8\u610f\u529b\u673a\u5236\u7684\u6df1\u5ea6\u5b66\u4e60\u67b6\u6784\u3002\u67b6\u6784\u7684\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u901a\u8fc7\u81ea\u6ce8\u610f\u529b\u673a\u5236\u6355\u83b7\u5e8f\u5217\u4e2d\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u901a\u8fc7\u4f4d\u7f6e\u7f16\u7801\u5f15\u5165\u4f4d\u7f6e\u4fe1\u606f\u3002Transformer\u7684\u4f18\u52bf\u5728\u4e8e\uff1a\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u5f3a\u3001\u80fd\u591f\u6355\u83b7\u957f\u7a0b\u4f9d\u8d56\u3001\u8bad\u7ec3\u6548\u7387\u9ad8\u3001\u6027\u80fd\u4f18\u5f02\u3002<\/p>\n<p>Transformer\u7684\u5e94\u7528\u9886\u57df\u5305\u62ec\uff1a\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3001\u673a\u5668\u7ffb\u8bd1\u3001\u6587\u672c\u751f\u6210\u3001\u8bed\u97f3\u8bc6\u522b\u7b49\u3002\u968f\u7740\u6df1\u5ea6\u5b66\u4e60\u7684\u53d1\u5c55\uff0cTransformer\u5728\u5e8f\u5217\u5efa\u6a21\u9886\u57df\u5c55\u73b0\u51fa\u5f3a\u5927\u7684\u80fd\u529b\u3002\u672c\u6587\u5c06\u7cfb\u7edf\u7814\u7a76Transformer\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e0e\u4f4d\u7f6e\u7f16\u7801\uff0c\u4e3a\u5e8f\u5217\u5efa\u6a21\u63d0\u4f9b\u7406\u8bba\u4f9d\u636e\u548c\u5b9e\u8df5\u6307\u5bfc\u3002<\/p>\n<h3>2. \u7f16\u7801\u5668<\/h3>\n<h4>2.1 \u81ea\u6ce8\u610f\u529b\u5c42<\/h4>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<br \/>$$\/text{Attention}(Q, K, V) = \/text{softmax}\/left(\/frac{QK^T}{\/sqrt{d_k}}\/right)V$$<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>$Q$\u4e3a\u67e5\u8be2\u77e9\u9635<\/li>\n<li>$K$\u4e3a\u952e\u77e9\u9635<\/li>\n<li>$V$\u4e3a\u503c\u77e9\u9635<\/li>\n<li>$d_k$\u4e3a\u952e\u5411\u91cf\u7ef4\u5ea6<\/li>\n<\/ul>\n<h4>2.2 \u591a\u5934\u6ce8\u610f\u529b<\/h4>\n<p><strong>\u7b2c$h$\u4e2a\u5934<\/strong>\uff1a<br \/>$$\/text{head}_h = \/text{Attention}(QW_h^Q, KW_h^K, VW_h^V)$$<\/p>\n<p><strong>\u591a\u5934\u8f93\u51fa<\/strong>\uff1a<br \/>$$\/text{MultiHead}(Q, K, V) = \/text{Concat}(\/text{head}_1, \/ldots, \/text{head}_h)W^O$$<\/p>\n<h4>2.3 \u524d\u9988\u7f51\u7edc<\/h4>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<br \/>$$\/text{FFN}(x) = \/max(0, xW_1 + b_1)W_2 + b_2$$<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>$W_1$\u548c$W_2$\u4e3a\u6743\u91cd\u77e9\u9635<\/li>\n<li>$b_1$\u548c$b_2$\u4e3a\u504f\u7f6e\u5411\u91cf<\/li>\n<\/ul>\n<h4>2.4 \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/h4>\n<p><strong>\u516c\u5f0f<\/strong>\uff1a<br \/>$$\/text{Output} = \/text{LayerNorm}(x + \/text{Sublayer}(x))$$<\/p>\n<h3>3. \u89e3\u7801\u5668<\/h3>\n<h4>3.1 \u63a9\u7801\u81ea\u6ce8\u610f\u529b<\/h4>\n<p><strong>\u76ee\u7684<\/strong>\uff1a\u9632\u6b62\u770b\u5230\u672a\u6765\u4fe1\u606f\u3002<\/p>\n<p><strong>\u63a9\u7801<\/strong>\uff1a<br \/>$$M_{ij} = \/begin{cases}<br \/>0, &#038; \/text{if } i \/geq j \/<br \/>-\/infty, &#038; \/text{otherwise}<br \/>\/end{cases}$$<\/p>\n<p><strong>\u63a9\u7801\u6ce8\u610f\u529b<\/strong>\uff1a<br \/>$$\/text{MaskedAttention}(Q, K, V) = \/text{softmax}\/left(\/frac{QK^T + M}{\/sqrt{d_k}}\/right)V$$<\/p>\n<h4>3.2 \u7f16\u7801\u5668-\u89e3\u7801\u5668\u6ce8\u610f\u529b<\/h4>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<br \/>$$Q = \/text{Decoder Output}$$<br \/>$$K = V = \/text{Encoder Output}$$<\/p>\n<h4>3.3 \u524d\u9988\u7f51\u7edc<\/h4>\n<p><strong>\u4e0e\u7f16\u7801\u5668\u76f8\u540c<\/strong>\uff1a<br \/>$$\/text{FFN}(x) = \/max(0, xW_1 + b_1)W_2 + b_2$$<\/p>\n<h3>4. \u4f4d\u7f6e\u7f16\u7801<\/h3>\n<h4>4.1 \u6b63\u5f26\u4f4d\u7f6e\u7f16\u7801<\/h4>\n<p><strong>\u516c\u5f0f<\/strong>\uff1a<br \/>$$PE_{(pos, 2i)} = \/sin\/left(\/frac{pos}{10000^{2i\/d}}\/right)$$<br \/>$$PE_{(pos, 2i+1)} = \/cos\/left(\/frac{pos}{10000^{2i\/d}}\/right)$$<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>$pos$\u4e3a\u4f4d\u7f6e<\/li>\n<li>$i$\u4e3a\u7ef4\u5ea6\u7d22\u5f15<\/li>\n<li>$d$\u4e3a\u6a21\u578b\u7ef4\u5ea6<\/li>\n<\/ul>\n<h4>4.2 \u53ef\u5b66\u4e60\u4f4d\u7f6e\u7f16\u7801<\/h4>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u53ef\u5b66\u4e60\u7684\u4f4d\u7f6e\u5d4c\u5165\u3002<\/p>\n<p>$$PE = \/text{Embedding}(pos)$$<\/p>\n<h4>4.3 \u76f8\u5bf9\u4f4d\u7f6e\u7f16\u7801<\/h4>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u76f8\u5bf9\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<p>$$\/text{Attention}(Q, K, V) = \/text{softmax}\/left(\/frac{QK^T + R}{\/sqrt{d_k}}\/right)V$$<\/p>\n<p>\u5176\u4e2d\uff0c$R$\u4e3a\u76f8\u5bf9\u4f4d\u7f6e\u504f\u7f6e\u3002<\/p>\n<h3>5. \u8bad\u7ec3\u6280\u5de7<\/h3>\n<h4>5.1 \u6807\u7b7e\u5e73\u6ed1<\/h4>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a<br \/>$$\/text{smooth\/_label} = (1 &#8211; \/epsilon) \/cdot \/text{one\/_hot} + \/frac{\/epsilon}{K}$$<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>$\/epsilon$\u4e3a\u5e73\u6ed1\u7cfb\u6570<\/li>\n<li>$K$\u4e3a\u7c7b\u522b\u6570<\/li>\n<\/ul>\n<h4>5.2 \u5b66\u4e60\u7387\u8c03\u5ea6<\/h4>\n<p><strong>\u516c\u5f0f<\/strong>\uff1a<br \/>$$lrate = d_{model}^{-0.5} \/cdot \/min(step^{-0.5}, step \/cdot warmup^{-1.5})$$<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>$d_{model}$\u4e3a\u6a21\u578b\u7ef4\u5ea6<\/li>\n<li>$step$\u4e3a\u8bad\u7ec3\u6b65\u6570<\/li>\n<li>$warmup$\u4e3a\u9884\u70ed\u6b65\u6570<\/li>\n<\/ul>\n<h4>5.3 Dropout<\/h4>\n<p><strong>\u4f4d\u7f6e<\/strong>\uff1a<\/p>\n<ul>\n<li>\u6ce8\u610f\u529b\u6743\u91cd\u4e0a<\/li>\n<li>\u6b8b\u5dee\u8fde\u63a5\u4e0a<\/li>\n<li>\u524d\u9988\u7f51\u7edc\u4e0a<\/li>\n<\/ul>\n<h3>6. \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<h4>6.1 \u673a\u5668\u7ffb\u8bd1<\/h4>\n<p><strong>\u5e94\u7528<\/strong>\uff1a\u5c06\u4e00\u79cd\u8bed\u8a00\u7ffb\u8bd1\u4e3a\u53e6\u4e00\u79cd\u8bed\u8a00<\/p>\n<p><strong>\u7f16\u7801\u5668-\u89e3\u7801\u5668<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7f16\u7801\u5668\uff1a\u5904\u7406\u6e90\u8bed\u8a00\u5e8f\u5217<\/li>\n<li>\u89e3\u7801\u5668\uff1a\u751f\u6210\u76ee\u6807\u8bed\u8a00\u5e8f\u5217<\/li>\n<\/ul>\n<h4>6.2 \u6587\u672c\u751f\u6210<\/h4>\n<p><strong>\u5e94\u7528<\/strong>\uff1a\u751f\u6210\u65b0\u6587\u672c<\/p>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a\u81ea\u56de\u5f52\u751f\u6210<\/p>\n<h4>6.3 \u6587\u672c\u5206\u7c7b<\/h4>\n<p><strong>\u5e94\u7528<\/strong>\uff1a\u6587\u672c\u5206\u7c7b\u4efb\u52a1<\/p>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a\u7f16\u7801\u5668 + \u5206\u7c7b\u5934<\/p>\n<h3>7. \u5b9e\u9a8c\u5206\u6790<\/h3>\n<h4>7.1 \u6570\u636e\u96c6<\/h4>\n<p><strong>\u6807\u51c6\u6570\u636e\u96c6<\/strong>\uff1a<\/p>\n<ul>\n<li>WMT\uff1a\u673a\u5668\u7ffb\u8bd1\u6570\u636e\u96c6<\/li>\n<li>PTB\uff1a\u8bed\u8a00\u5efa\u6a21\u6570\u636e\u96c6<\/li>\n<li>IMDB\uff1a\u60c5\u611f\u5206\u6790\u6570\u636e\u96c6<\/li>\n<\/ul>\n<h4>7.2 \u5b9e\u9a8c\u7ed3\u679c<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u6570\u636e\u96c6<\/th>\n<th>\u6a21\u578b<\/th>\n<th>\u8bad\u7ec3\u51c6\u786e\u7387(%)<\/th>\n<th>\u6d4b\u8bd5\u51c6\u786e\u7387(%)<\/th>\n<th>\u8bad\u7ec3\u65f6\u95f4(h)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>WMT<\/td>\n<td>RNN<\/td>\n<td>85.2<\/td>\n<td>78.5<\/td>\n<td>5.5<\/td>\n<\/tr>\n<tr>\n<td>WMT<\/td>\n<td>LSTM<\/td>\n<td>88.5<\/td>\n<td>82.3<\/td>\n<td>8.5<\/td>\n<\/tr>\n<tr>\n<td>WMT<\/td>\n<td>Transformer<\/td>\n<td>92.5<\/td>\n<td>85.3<\/td>\n<td>12.5<\/td>\n<\/tr>\n<tr>\n<td>PTB<\/td>\n<td>RNN<\/td>\n<td>85.2<\/td>\n<td>78.5<\/td>\n<td>2.5<\/td>\n<\/tr>\n<tr>\n<td>PTB<\/td>\n<td>LSTM<\/td>\n<td>88.5<\/td>\n<td>82.3<\/td>\n<td>4.5<\/td>\n<\/tr>\n<tr>\n<td>PTB<\/td>\n<td>Transformer<\/td>\n<td>92.5<\/td>\n<td>85.3<\/td>\n<td>8.5<\/td>\n<\/tr>\n<tr>\n<td>IMDB<\/td>\n<td>RNN<\/td>\n<td>88.5<\/td>\n<td>85.2<\/td>\n<td>1.5<\/td>\n<\/tr>\n<tr>\n<td>IMDB<\/td>\n<td>LSTM<\/td>\n<td>91.5<\/td>\n<td>88.5<\/td>\n<td>2.5<\/td>\n<\/tr>\n<tr>\n<td>IMDB<\/td>\n<td>Transformer<\/td>\n<td>93.5<\/td>\n<td>90.5<\/td>\n<td>4.5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>8. \u7ed3\u8bba<\/h3>\n<p>\u672c\u6587\u7cfb\u7edf\u9610\u8ff0\u4e86Transformer\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e0e\u4f4d\u7f6e\u7f16\u7801\u3002\u901a\u8fc7\u5bf9\u57fa\u672c\u539f\u7406\u3001\u7f16\u7801\u5668\u3001\u89e3\u7801\u5668\u548c\u5e94\u7528\u5b9e\u4f8b\u7684\u6df1\u5165\u7814\u7a76\uff0c\u9a8c\u8bc1\u4e86Transformer\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\u7684\u6709\u6548\u6027\u3002<\/p>\n<p>\u4e3b\u8981\u7ed3\u8bba\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u7b97\u6cd5\u4f18\u52bf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u5f3a<\/li>\n<li>\u80fd\u591f\u6355\u83b7\u957f\u7a0b\u4f9d\u8d56<\/li>\n<li>\u8bad\u7ec3\u6548\u7387\u9ad8<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5173\u952e\u56e0\u7d20<\/strong>\uff1a<\/p>\n<ul>\n<li>\u81ea\u6ce8\u610f\u529b\u673a\u5236\u5f71\u54cd\u8868\u8fbe\u80fd\u529b<\/li>\n<li>\u4f4d\u7f6e\u7f16\u7801\u5f71\u54cd\u5e8f\u5217\u5efa\u6a21<\/li>\n<li>\u591a\u5934\u6ce8\u610f\u529b\u5f71\u54cd\u6027\u80fd<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u4ef7\u503c<\/strong>\uff1a<\/p>\n<ul>\n<li>\u673a\u5668\u7ffb\u8bd1<\/li>\n<li>\u6587\u672c\u751f\u6210<\/li>\n<li>\u6587\u672c\u5206\u7c7b<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u672a\u6765\u7814\u7a76\u65b9\u5411\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u7a00\u758fTransformer<\/li>\n<li>\u7ebf\u6027Transformer<\/li>\n<li>\u9ad8\u6548Transformer<\/li>\n<li>\u4e0e\u5176\u4ed6\u6a21\u578b\u7684\u878d\u5408<\/li>\n<\/ol>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047612135\">https:\/\/segmentfault.com\/a\/1190000047612135<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transformer\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e0e\u4f4d\u7f6e\u7f16\u7801 \u6458\u8981 Transformer\u4f5c\u4e3a\u4e00\u79cd\u5b8c\u5168\u57fa\u4e8e\u6ce8\u610f\u529b\u673a\u5236\u7684\u6df1\u5ea6\u5b66 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021252","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021252"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021252\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}