{"id":783,"date":"2024-06-24T09:03:22","date_gmt":"2024-06-24T01:03:22","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=783"},"modified":"2024-06-24T09:03:22","modified_gmt":"2024-06-24T01:03:22","slug":"%e7%90%86%e8%a7%a3-transformer-%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e4%b8%ad%e7%9a%84%e6%b3%a8%e6%84%8f%e5%8a%9b%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2024\/06\/24\/%e7%90%86%e8%a7%a3-transformer-%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e4%b8%ad%e7%9a%84%e6%b3%a8%e6%84%8f%e5%8a%9b%e6%9c%ba%e5%88%b6\/","title":{"rendered":"\u7406\u89e3 Transformer \u795e\u7ecf\u7f51\u7edc\u4e2d\u7684\u6ce8\u610f\u529b\u673a\u5236"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiah4vwH6iaqultBjVQ584FwTenI6btJs3P0fvwI4hZG10gQdUEicvgdGezQ\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\" title=\"\u795e\u7ecf\u81ea\u6211\u6ce8\u610f\u5c01\u9762\u56fe\u7247 \u2013 LearnOpenCV\u00a0\"\/><\/figure>\n\n\n\n<p>\u521a\u521a\u7ed3\u675f\u7684 2022 \u5e74\u662f\u4eba\u5de5\u667a\u80fd\u9886\u57df\u53d6\u5f97\u8bf8\u591a\u8fdb\u6b65\u7684\u4e0d\u53ef\u601d\u8bae\u7684\u4e00\u5e74\u3002\u4eba\u5de5\u667a\u80fd\u9886\u57df\u6700\u8fd1\u53d6\u5f97\u7684\u5927\u591a\u6570\u91cc\u7a0b\u7891\u5f0f\u6210\u5c31\u90fd\u662f\u7531\u4e00\u79cd\u79f0\u4e3a transformers \u7684\u7279\u6b8a\u6a21\u578b\u63a8\u52a8\u7684\uff0c\u65e0\u8bba\u662f chatGPT \u7684\u60ca\u4eba\u8fdb\u6b65\uff0c\u8fd8\u662f\u7a33\u5b9a\u4f20\u64ad\uff0c\u5b83\u90fd\u4e3a\u4f60\u7684\u667a\u80fd\u624b\u673a\u5e26\u6765\u4e86\u79d1\u5e7b\u822c\u7684\u529f\u80fd\u3002\u5373\u4f7f\u662f\u7279\u65af\u62c9\u7684\u81ea\u52a8\u9a7e\u9a76\u8f6f\u4ef6\u5806\u6808\uff0c\u4e5f\u8bb8\u662f\u4e16\u754c\u4e0a\u90e8\u7f72\u6700\u5e7f\u6cdb\u7684\u6df1\u5ea6\u5b66\u4e60\u7cfb\u7edf\uff0c\u4e5f\u5728\u5e95\u5c42\u4f7f\u7528\u4e86 transformer \u6a21\u578b\uff08\u53cc\u5173\u8bed\uff09\u3002\u201c\u795e\u7ecf\u6ce8\u610f\u529b\u673a\u5236\u201d\u662f transformers \u5728\u5404\u79cd\u4efb\u52a1\u548c\u6570\u636e\u96c6\u4e0a\u5982\u6b64\u6210\u529f\u7684\u79d8\u8bc0\u3002&nbsp;<\/p>\n\n\n\n<p>\u8fd9\u662f\u5173\u4e8e\u89c6\u89c9\u8f6c\u6362\u5668 (ViT) \u7684\u7cfb\u5217\u6587\u7ae0\u4e2d\u7684\u7b2c\u4e00\u7bc7\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u6ce8\u610f\u529b\u673a\u5236\uff0c\u5e76\u56de\u987e\u5bfc\u81f4\u8be5\u673a\u5236\u7684\u601d\u60f3\u6f14\u53d8\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u76f4\u89c2\u5730\u7406\u89e3\u5b83\u3002\u6211\u4eec\u5c06\u7528\u6570\u5b66\u7ec6\u8282\u5de9\u56fa\u76f4\u89c2\u7406\u89e3\uff0c\u5e76\u6700\u7ec8\u901a\u8fc7\u5728 PyTorch \u6846\u67b6\u4e2d\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u6ce8\u610f\u529b\u673a\u5236\u5c06\u8fd9\u79cd\u7406\u89e3\u8f6c\u5316\u4e3a\u4ee3\u7801\u3002\u867d\u7136\u6211\u4eec\u5c06\u5728\u672c\u6587\u7ed3\u5c3e\u5904\u4e13\u95e8\u8ba8\u8bba\u89c6\u89c9\u8f6c\u6362\u5668\uff0c\u4f46\u5927\u90e8\u5206\u8ba8\u8bba\u540c\u6837\u9002\u7528\u4e8e\u5927\u578b\u8bed\u8a00\u6a21\u578b (LLM)\uff0c\u4f8b\u5982 GPT-3 \u548c\u6700\u8fd1\u53d1\u5e03\u7684 chatGPT \u6a21\u578b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u795e\u7ecf\u6ce8\u610f\u529b\u7b80\u4ecb\n<ol class=\"wp-block-list\">\n<li>\u5faa\u73af\u795e\u7ecf\u7f51\u7edc<\/li>\n\n\n\n<li>\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc<\/li>\n\n\n\n<li>\u795e\u7ecf\u81ea\u6ce8\u610f\u529b\u673a\u5236<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u795e\u7ecf\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u76f4\u89c2\u89e3\u91ca\n<ol class=\"wp-block-list\">\n<li>\u6b65\u9aa41\uff0c\u67e5\u8be2\uff0c\u952e\u503c<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u6b65\uff0c\u8ba1\u7b97\u6ce8\u610f\u529b\u77e9\u9635<\/li>\n\n\n\n<li>\u6b65\u9aa4 3\uff0c\u89c4\u8303\u5316\u548c\u6ce8\u610f\u529b\u5206\u6570<\/li>\n\n\n\n<li>\u7b2c 4 \u6b65\uff0c\u53c2\u52a0<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u591a\u5934\u81ea\u6ce8\u610f\u529b<\/li>\n\n\n\n<li>\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u6570\u5b66\u516c\u5f0f<\/li>\n\n\n\n<li>PyTorch \u5b9e\u73b0 Self Attention\n<ol class=\"wp-block-list\">\n<li>\u57fa\u672c\u7ed3\u679c<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u6982\u62ec<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u795e\u7ecf\u6ce8\u610f\u529b\u7b80\u4ecb<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u5bf9\u81ea\u6ce8\u610f\u529b\u673a\u5236\u6709\u6700\u76f4\u89c2\u7684\u7406\u89e3\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u56de\u987e\u4e00\u4e0b\u8fc7\u53bb\u5341\u5e74\u81ea\u7136\u8bed\u8a00\u5904\u7406 (NLP) \u67b6\u6784\u7684\u53d1\u5c55\u60c5\u51b5\u3002\u6211\u4eec\u5c06\u4fdd\u6301\u8ba8\u8bba\u7684\u72ec\u7acb\u6027\uff0c\u4e0d\u9700\u8981\u719f\u6089 NLP \u9886\u57df\u3002\u6211\u4eec\u5728\u8fd9\u91cc\u4e3b\u8981\u5173\u6ce8\u7684\u662f\u601d\u60f3\u7684\u6f14\u53d8\uff0c\u800c\u4e0d\u662f\u6bcf\u4e2a\u67b6\u6784\u7684\u7cbe\u786e\u6570\u5b66\u7ec6\u8282\u3002\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u56de\u987e\u4e00\u4e0b NLP \u4e2d\u7684\u601d\u60f3\u6d41\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5faa\u73af\u795e\u7ecf\u7f51\u7edc<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahGNEcC4ksxhA2X40TjRibxUXdjZicxfVJtJNoflFr5EtmbFHjQtVbU22w\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><figcaption class=\"wp-element-caption\">\u56fe 1. \u5faa\u73af\u5757\u7684\u793a\u610f\u56fe\uff08\u6765\u6e90\uff1a\u7ef4\u57fa\u767e\u79d1\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u673a\u5668\u5b66\u4e60\u6587\u732e\u4e2d\u5df2\u7ecf\u5145\u5206\u8ba4\u8bc6\u5230\uff0c\u81ea\u7136\u8bed\u8a00\u4e2d\u7684\u53e5\u5b50\u662f\u5e8f\u5217\uff0c\u56e0\u6b64\u80fd\u591f\u5904\u7406\u5e8f\u5217\u7684\u6a21\u578b\u6bd4\u201c\u65e0\u72b6\u6001\u201d\u6a21\u578b\uff08\u4f8b\u5982\u666e\u901a\u7684\u5168\u8fde\u63a5\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\uff09\u66f4\u9002\u5408 NLP \u4efb\u52a1\u3002\u56e0\u6b64\uff0c\u63d0\u51fa\u4e86\u5faa\u73af\u5757\uff0c\u5176\u793a\u610f\u56fe\u5982\u56fe 1 \u6240\u793a\u3002\u57fa\u672c\u601d\u60f3\u662f\u53ef\u4ee5\u5728\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u4e2d\u9010\u5b57\u5904\u7406\u53e5\u5b50\uff0c\u8fd9\u6837\u524d\u4e00\u4e2a\u5355\u8bcd\u7684\u8868\u793a\u5c31\u4f1a\u88ab\u6ce8\u5165\u5230\u5f53\u524d\u5355\u8bcd\u7684\u9690\u85cf\u5c42\u4e2d\u3002\u8fd9\u4f7f\u5f97 RNN \u80fd\u591f\u5c06\u6570\u636e\u7684\u65f6\u95f4\u7279\u6027\u660e\u786e\u5730\u7eb3\u5165\u6a21\u578b\u4e2d\u3002&nbsp;<\/p>\n\n\n\n<p>\u5c3d\u7ba1 RNN \u80cc\u540e\u7684\u7406\u5ff5\u975e\u5e38\u6df1\u523b\uff0c\u4f46\u5b83\u4eec\u7684\u6570\u5b66\u516c\u5f0f\u4f7f\u5f97\u5b83\u4eec\u65e0\u6cd5\u5bf9\u957f\u5e8f\u5217\u8fdb\u884c\u5efa\u6a21\u3002RNN \u5c06\u5e8f\u5217\u5efa\u6a21\u4e3a\u5355\u8bcd\u9690\u85cf\u8868\u793a\u4e4b\u95f4\u7684\u4e58\u6cd5\u4ea4\u4e92\u3002\u56e0\u6b64\uff0c\u5c1d\u8bd5\u5bf9\u957f\u5e8f\u5217\u8fdb\u884c\u5efa\u6a21\u4f1a\u5bfc\u81f4\u68af\u5ea6\u7206\u70b8\u548c\u6d88\u5931\u3002\u9700\u8981\u4fee\u6539 RNN \u7684\u8bbe\u8ba1\u4ee5\u7a33\u5b9a\u8bad\u7ec3\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahdl1m3PjT4oxQdejpc9bVbM17C7JEv6pAwjc6LQxASmwgmRJhtic1FIw\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><figcaption class=\"wp-element-caption\">\u56fe 2. LSTM \u5355\u5143\u793a\u610f\u56fe\u3002\u9700\u8981\u6ce8\u610f\u7684\u5173\u952e\u533a\u522b\u662f\u5b58\u5728\u52a0\u6cd5\u76f8\u4e92\u4f5c\u7528\uff0c\u800c\u56fe 1 \u4e2d\u5219\u662f\u4e58\u6cd5\u76f8\u4e92\u4f5c\u7528\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc (LSTM) \u4fee\u6539\u4e86\u539f\u59cb RNN \u7684\u67b6\u6784\uff0c\u4ee5\u7a33\u5b9a\u8bad\u7ec3\u3002Schmidhuber \u6559\u6388\u4e0e\u4ed6\u7684\u540c\u4e8b\u901a\u8fc7\u4e13\u95e8\u7814\u7a76 RNN \u7684\u7f3a\u70b9\u63d0\u51fa\u4e86 LSTM\u3002\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0cLSTM \u7684\u5173\u952e\u521b\u65b0\u662f\u7528\u52a0\u6cd5\u4ea4\u4e92\u53d6\u4ee3 RNN \u4e2d\u7684\u4e58\u6cd5\u4ea4\u4e92\u3002\u56e0\u6b64\uff0c\u68af\u5ea6\u503c\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u8870\u51cf\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u52a0\u6cd5\u5206\u5e03\u800c\u4e0d\u662f\u4e58\u6cd5\u5206\u5e03\u3002&nbsp;<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5728 LSTM \u4e2d\uff0c\u68af\u5ea6\u5e45\u5ea6\u4e0d\u4f1a\u7206\u53d1\u6216\u6d88\u5931\uff0c\u56e0\u6b64\u53ef\u4ee5\u5bf9\u957f\u5e8f\u5217\u8fdb\u884c\u5efa\u6a21\u3002\u4f5c\u4e3a\u8ba1\u7b97\u673a\u89c6\u89c9\u4e13\u5bb6\uff0c\u60a8\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0cHe \u7b49\u4eba\u5728\u8bbe\u8ba1 ResNet\uff08\u79f0\u4e3a\u6b8b\u5dee\u5757\uff09\u65f6\u4f7f\u7528\u4e86\u76f8\u540c\u7684\u52a0\u6027\u76f8\u4e92\u4f5c\u7528\u601d\u60f3\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u770b\u5230\u4e86\u4f7f\u7528\u76f8\u540c\u601d\u60f3\u6539\u8fdb NLP \u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u6a21\u578b\u7684\u7b2c\u4e00\u4e2a\u8bc1\u636e\u3002\u8fd9\u4e00\u8d8b\u52bf\u5728\u89c6\u89c9\u8f6c\u6362\u5668\u7684\u8bbe\u8ba1\u4e2d\u5f97\u5230\u4e86\u660e\u786e\u7684\u63a8\u52a8\uff0c\u6211\u4eec\u5c06\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\u770b\u5230\u3002<\/p>\n\n\n\n<p>\u5c3d\u7ba1 LSTM \u6bd4 RNN \u6709\u6240\u6539\u8fdb\uff0c\u4f46\u5efa\u6a21\u8d85\u957f\u5e8f\u5217\u7684\u95ee\u9898\u4ecd\u7136\u5b58\u5728\u3002\u6b64\u5916\uff0cLSTM \u5c31\u50cf\u666e\u901a\u7684 RNN \u4e00\u6837\uff0c\u6309\u987a\u5e8f\uff08\u5373\u4e00\u4e2a\u63a5\u4e00\u4e2a\uff09\u5904\u7406\u5355\u8bcd\u3002\u56e0\u6b64\uff0c\u5bf9\u957f\u5e8f\u5217\u7684\u63a8\u7406\u901f\u5ea6\u5f88\u6162\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u795e\u7ecf\u81ea\u6ce8\u610f\u529b\u673a\u5236<\/h3>\n\n\n\n<p>\u867d\u7136 LSTM \u65e8\u5728\u89e3\u51b3 RNN \u7684\u6570\u5b66\u7f3a\u9677\uff0c\u4f46\u795e\u7ecf\u81ea\u6ce8\u610f\u529b\u673a\u5236\uff08\u6216\u7b80\u79f0\u6ce8\u610f\u529b\u673a\u5236\uff09\u5219\u4ece\u5934\u5f00\u59cb\uff0c\u91cd\u65b0\u601d\u8003 RNN \u80cc\u540e\u7684\u52a8\u673a\u3002\u867d\u7136\u53e5\u5b50\u786e\u5b9e\u662f\u5355\u8bcd\u5e8f\u5217\uff0c\u4f46\u4eba\u7c7b\u4e0d\u4f1a\u6309\u987a\u5e8f\u3001\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u5904\u7406\u5355\u8bcd\uff0c\u800c\u662f\u4ee5\u5757\u4e3a\u5355\u4f4d\u3002\u6b64\u5916\uff0c\u6709\u4e9b\u5355\u8bcd\u5bf9\u4e8e\u9884\u6d4b\u4e0b\u4e00\u4e2a\u5355\u8bcd\u9ad8\u5ea6\u76f8\u5173\uff0c\u4f46\u5927\u591a\u6570\u5355\u8bcd\u5e76\u975e\u5982\u6b64\u3002&nbsp;<\/p>\n\n\n\n<p>LSTM \u88ab\u8feb\u6309\u987a\u5e8f\u5904\u7406\u6240\u6709\u5355\u8bcd\uff0c\u800c\u4e0d\u7ba1\u5b83\u4eec\u7684\u76f8\u5173\u6027\u5982\u4f55\uff0c\u8fd9\u9650\u5236\u4e86\u6a21\u578b\u53ef\u4ee5\u5b66\u4e60\u7684\u5185\u5bb9\u3002\u662f\u5426\u6709\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u8ba9\u6a21\u578b\u5c06\u7279\u5b9a\u5355\u8bcd\u7684\u76f8\u5173\u6027\u7eb3\u5165\u5176\u81ea\u8eab\uff1f&nbsp;<\/p>\n\n\n\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9884\u6d4b\u4e0b\u4e00\u4e2a\u5355\u8bcd\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u5173\u6ce8\u54ea\u4e9b\u5148\u524d\u7684\u5355\u8bcd\uff1f\u8fd9\u662f\u6ce8\u610f\u529b\u673a\u5236\u8d77\u6e90\u80cc\u540e\u7684\u52a8\u673a\u95ee\u9898\u3002\u4e8b\u5b9e\u8bc1\u660e\uff0c\u7b54\u6848\u4e0d\u4ec5\u4e0e\u8bed\u8a00\u6a21\u578b\u76f8\u5173\uff0c\u800c\u4e14\u8fd8\u5bfc\u81f4\u4e86 ViT \u7684\u53d1\u5c55\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u795e\u7ecf\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u76f4\u89c2\u89e3\u91ca<\/h2>\n\n\n\n<p>\u6ce8\u610f\u673a\u5236\u4e0d\u662f\u5c06\u8f93\u5165\u4f5c\u4e3a\u5e8f\u5217\u6765\u5904\u7406\uff0c\u800c\u662f\u5c06\u5176\u4f5c\u4e3a\u6ca1\u6709\u4efb\u4f55\u660e\u786e\u65f6\u95f4\u8054\u7cfb\u7684\u5757\u6765\u5904\u7406\u3002\u8ba9\u6211\u4eec\u5728\u5bf9\u53e5\u5b50\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5355\u8bcd\u8fdb\u884c\u5efa\u6a21\u7684\u80cc\u666f\u4e0b\u9010\u6b65\u4e86\u89e3\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahMzTqfiaAJ9dXqCeprCYErY8pdia3uN4Fr7p4m9Lmh8bqneNEzSaXiaGoA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><figcaption class=\"wp-element-caption\">\u56fe 3. \u6ce8\u610f\u529b\u673a\u5236\u7b2c\u4e00\u6b65<\/figcaption><\/figure>\n\n\n\n<p>\u6b65\u9aa4 1\uff0c\u67e5\u8be2\uff0c\u952e\u503c\uff1a\u4e0e RNN \u975e\u5e38\u76f8\u4f3c\uff0c\u53e5\u5b50\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u90fd\u4f1a\u201c\u53d1\u51fa\u201d\u5176\u81ea\u8eab\u7684\u9690\u85cf\u8868\u793a\u3002\u4e3a\u4e86\u8ba9\u8be5\u673a\u5236\u80fd\u591f\u6a21\u62df\u76f8\u5173\u6027\uff0c\u6211\u4eec\u8ba9\u6bcf\u4e2a\u5355\u8bcd\u63d0\u51fa\u95ee\u9898\u5e76\u5f97\u5230\u7b54\u6848\u3002&nbsp;<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5355\u8bcd\u63d0\u51fa\u7684\u95ee\u9898\u4e0e\u53e6\u4e00\u4e2a\u5355\u8bcd\u53d1\u9001\u7684\u7b54\u6848\u5339\u914d\u65f6\uff0c\u6211\u4eec\u4f1a\u5c06\u6b64\u89e3\u91ca\u4e3a\u4e24\u4e2a\u5355\u8bcd\u5f7c\u6b64\u5177\u6709\u9ad8\u5ea6\u76f8\u5173\u6027\u3002\u4e00\u4e2a\u5355\u8bcd\u4f7f\u7528\u201c\u67e5\u8be2\u201d\u5411\u91cf\u5411\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u5355\u8bcd\u63d0\u51fa\u76f8\u540c\u7684\u95ee\u9898\u3002\u540c\u6837\uff0c\u5b83\u4f7f\u7528\u201c\u5173\u952e\u201d\u5411\u91cf\u5411\u6240\u6709\u5355\u8bcd\u63d0\u4f9b\u76f8\u540c\u7684\u7b54\u6848\u3002&nbsp;<\/p>\n\n\n\n<p>\u5728\u6ce8\u610f\u529b\u673a\u5236\u6700\u5e38\u7528\u7684\u53d8\u4f53\u4e2d\uff0c\u8fd8\u4f7f\u7528\u5355\u72ec\u7684\u201c\u503c\u201d\u5411\u91cf\uff0c\u4ee5\u5141\u8bb8\u6a21\u578b\u975e\u7ebf\u6027\u5730\u7ec4\u5408\u67e5\u8be2\u548c\u952e\u5411\u91cf\u7684\u8f93\u51fa\u3002\u8fd9\u589e\u52a0\u4e86\u6a21\u578b\u7684\u8868\u8fbe\u80fd\u529b\u3002\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u5411\u91cf\u90fd\u662f\u5176\u5355\u72ec\u5bc6\u96c6\u5c42\u7684\u8f93\u51fa\uff0c\u5e76\u4e14\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u5411\u91cf\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\uff0c\u4f8b\u5982 d\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5728\u6ce8\u610f\u529b\u673a\u5236\u7684\u7b2c 1 \u6b65\u4e2d\uff0c\u5982\u56fe 3 \u6240\u793a\uff0c\u53e5\u5b50\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u4f7f\u7528\u4e09\u4e2a\u5355\u72ec\u7684\u5bc6\u96c6\u5c42\u53d1\u51fa\u81ea\u8eab\u7684\u4e09\u4e2a\u72ec\u7acb\u8868\u793a\uff0c\u79f0\u4e3a\u67e5\u8be2 Q\u3001\u952e K \u548c\u503c V \u5411\u91cf\u3002&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahyzruMkyIsg4py8ib6b8lPgRCC8picLJPMZkghh0PoYcW5gxBAcmOk4sg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><figcaption class=\"wp-element-caption\">\u56fe 4.\u7b2c 2 \u6b65\uff0c\u6839\u636e\u67e5\u8be2\u548c\u5173\u952e\u5411\u91cf\u8ba1\u7b97\u6ce8\u610f\u529b\u77e9\u9635\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u7b2c 2 \u6b65\uff0c\u8ba1\u7b97\u6ce8\u610f\u529b\u77e9\u9635\uff1a\u5982\u679c\u6211\u4eec\u7528 N \u4e2a\u5355\u8bcd\u6765\u5efa\u6a21\u4e00\u4e2a\u53e5\u5b50\uff0c\u90a3\u4e48\u7ecf\u8fc7\u7b2c 1 \u6b65\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u6709 N \u4e2a\u67e5\u8be2\u5411\u91cf\u3001N \u4e2a\u952e\u5411\u91cf\u548c N \u4e2a\u503c\u5411\u91cf\u3002\u90a3\u4e48\uff0c\u4e00\u4e2a\u5355\u8bcd\u7a76\u7adf\u662f\u5982\u4f55\u63d0\u51fa\u95ee\u9898\u5e76\u5f97\u5230\u7b54\u6848\u7684\u5462\uff1f\u8fd9\u662f\u7531\u6ce8\u610f\u529b\u77e9\u9635\u5b8c\u6210\u7684\uff0c\u5b83\u662f\u6ce8\u610f\u529b\u673a\u5236\u7684\u6838\u5fc3\u3002\u6211\u4eec\u5c06\u6240\u6709 N \u4e2a\u67e5\u8be2\u5411\u91cf\u4e0e\u6240\u6709 N \u4e2a\u952e\u5411\u91cf\u8fdb\u884c\u70b9\u79ef\uff0c\u5982\u56fe 4 \u6240\u793a\u3002\u7531\u4e8e\u4e24\u4e2a\u5411\u91cf\u7684\u70b9\u79ef\uff08\u65e0\u8bba\u591a\u5927\uff09\u53ea\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u56e0\u6b64\u8fd9\u4e9b NxN \u70b9\u79ef\u7684\u7ed3\u679c\u662f\u4e00\u4e2a NxN \u77e9\u9635<img loading=\"lazy\" decoding=\"async\" height=\"15\" width=\"16\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahAJalicVbWhVT6SvXicmAFHoek0QtiamoyIz5tJXteMQXkrez2T1j6SD8g\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\uff0c\u5176\u6bcf\u4e2a\u5143\u7d20<img loading=\"lazy\" decoding=\"async\" height=\"21\" width=\"29\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahoLgpKHYyr5icY8tQ7hqpTtrZVibbaXNdftOUHYDro5P1ON7icvXpGjA3w\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u662f\u7b2c i \u4e2a\u67e5\u8be2\u5411\u91cf\u4e0e\u7b2c j \u4e2a\u5173\u952e\u5b57\u5411\u91cf\u7684\u70b9\u79ef\u3002\u8fd9\u91cc\u6709\u4e09\u4ef6\u91cd\u8981\u7684\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9996\u5148\uff0c\u7531\u4e8e\u5728\u8ba1\u7b97\u6ce8\u610f\u529b\u77e9\u9635\u65f6\u6bcf\u4e2a\u5355\u8bcd\u90fd\u4f1a\u4e0e\u5176\u4ed6\u5355\u8bcd\u4ea4\u4e92\uff0c\u56e0\u6b64\u8f93\u5165\u6ca1\u6709\u65f6\u95f4\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u539f\u59cb\u6ce8\u610f\u529b\u673a\u5236\u4e0d\u8003\u8651\u8f93\u5165\u7684\u987a\u5e8f\u6027\u3002<\/li>\n\n\n\n<li>\u5176\u6b21\uff0c\u8ba1\u7b97\u6ce8\u610f\u529b\u77e9\u9635\u7684\u8ba1\u7b97\u590d\u6742\u5ea6\u4e3a<img loading=\"lazy\" decoding=\"async\" height=\"19\" width=\"27\" class=\"\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahHaY8cof1AjOQqAVTu6CfPdETW4PbbK9Ty1oT7fkQ7qUDyabxfy9BGg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\">\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u7528\u4e24\u500d\u7684\u5355\u8bcd\u6570\u91cf\u6765\u5efa\u6a21\u4e00\u4e2a\u53e5\u5b50\uff0c\u5c31\u9700\u8981 4 \u500d\u7684\u8ba1\u7b97\u8d44\u6e90\u3002\u5f53\u6211\u4eec\u6784\u5efa\u89c6\u89c9\u53d8\u6362\u5668\u6a21\u578b\u65f6\uff0c\u8fd9\u5c06\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u90e8\u5206\u4e2d\u53d8\u5f97\u5f88\u91cd\u8981\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e09\uff0c\u6211\u4eec\u6ce8\u610f\u5230\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e2d\u7684\u201c\u81ea\u6211\u201d\u6307\u7684\u662f\uff0c\u6211\u4eec\u8fd8\u5c06\u7ed9\u5b9a\u5355\u8bcd\u7684\u67e5\u8be2\u4e0e\u5176\u81ea\u5df1\u7684\u5173\u952e\u5411\u91cf\u8fdb\u884c\u70b9\u79ef\u8fd0\u7b97\u3002\u6ce8\u610f\u529b\u673a\u5236\u7684\u4e00\u4e9b\u65e9\u671f\u53d8\u4f53\u8df3\u8fc7\u4e86\u8ba1\u7b97\u81ea\u76f8\u5173\u6027\uff0c\u5bfc\u81f4\u8f93\u51fa\u5305\u542b NxN \u2013 N \u4e2a\u5143\u7d20\uff08\u77e9\u9635\u7684\u6240\u6709\u5bf9\u89d2\u7ebf\u5143\u7d20\u90fd\u7f3a\u5931\uff09\u3002\u7136\u800c\uff0c\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u6548\u679c\u6700\u597d\uff0c\u5e76\u4e14\u5728\u73b0\u4ee3 GPU \u4e0a\u53ef\u9ad8\u5ea6\u5e76\u884c\u5316\u3002\u8fd9\u5c31\u662f\u8be5\u673a\u5236\u88ab\u79f0\u4e3a\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u539f\u56e0\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6b65\u9aa4 3\uff0c\u89c4\u8303\u5316\u548c\u6ce8\u610f\u529b\u5f97\u5206\uff1a\u5982\u4e0a\u6240\u8ff0\uff0c\u6ce8\u610f\u529b\u77e9\u9635\u5c06\u5305\u542b\u4e00\u4e9b\u5927\u6570\u5b57\u548c\u4e00\u4e9b\u5c0f\u6570\u5b57\u3002\u6211\u4eec\u5c06\u5927\u6570\u5b57\u89e3\u91ca\u4e3a\u4ee3\u8868\u9ad8\u76f8\u5173\u6027\uff0c\u5c0f\u76f8\u5173\u6027\u89e3\u91ca\u4e3a\u4f4e\u76f8\u5173\u6027\u3002&nbsp;<\/p>\n\n\n\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4f7f\u7528\u8be5\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6700\u5927\u6c60\u5316\u64cd\u4f5c\u6311\u9009\u51fa\u6bcf\u884c\u4e2d\u7684\u6700\u5927\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u4eec\u7528\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u7136\u800c\uff0c\u8fd9\u4f1a\u4e22\u5f03\u5927\u90e8\u5206\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u6709\u65f6\u7ed9\u5b9a\u7684\u5355\u8bcd\u53ef\u80fd\u9700\u8981\u6765\u81ea\u591a\u4e2a\u5148\u524d\u5355\u8bcd\u7684\u4e0a\u4e0b\u6587\u624d\u80fd\u6b63\u786e\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5efa\u6a21\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5e94\u8be5\u5c3d\u53ef\u80fd\u4fdd\u6301\u8868\u8fbe\u80fd\u529b\u3002\u53e6\u4e00\u4e2a\u91cd\u8981\u8981\u6c42\u662f\uff0c\u65e0\u8bba\u6211\u4eec\u9009\u62e9\u4f7f\u7528\u6ce8\u610f\u77e9\u9635\uff0c\u5b83\u90fd\u5e94\u8be5\u4fdd\u6301\u6a21\u578b\u7684\u53ef\u5fae\u6027\uff0c\u56e0\u4e3a\u6a21\u578b\u9700\u8981\u901a\u8fc7\u53cd\u5411\u4f20\u64ad\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n\n\n\n<p>softmax \u5c42\u6ee1\u8db3\u4e86\u6240\u6709\u8fd9\u4e9b\u8981\u6c42\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5bf9\u6ce8\u610f\u529b\u77e9\u9635\u8fdb\u884c\u9010\u884c softmax\uff0c\u5f97\u5230\u4e00\u4e2a NxN \u77e9\u9635\uff0c\u5176\u6bcf\u884c\u90fd\u662f\u4e00\u4e2a\u6982\u7387\u5206\u5e03\u3002\u8fd9\u91cc\u7684\u4e00\u4e2a\u5c0f\u5b9e\u73b0\u7ec6\u8282\u662f\uff0c\u5728\u8fdb\u884c softmax \u4e4b\u524d\uff0c\u6211\u4eec\u5c06\u6ce8\u610f\u529b\u77e9\u9635\u9664\u4ee5\u6bcf\u4e2a\u67e5\u8be2\u5411\u91cf\u7ef4\u5ea6\u7684\u5e73\u65b9\u6839\u3002\u8fd9\u5145\u5f53\u4e86\u4e00\u4e2a\u5408\u9002\u7684\u7f29\u653e\u56e0\u5b50\uff0c\u4ee5\u201c\u8f6f\u5316\u201d\u5f97\u5230\u7684\u6982\u7387\u5206\u5e03\u3002&nbsp;<\/p>\n\n\n\n<p>\u7f29\u653e\u540e\uff0c\u5927\u6982\u7387\u503c\u548c\u5c0f\u6982\u7387\u503c\u4e4b\u95f4\u7684\u5dee\u5f02\u4f1a\u51cf\u5c0f\uff0c\u68af\u5ea6\u53ef\u4ee5\u76f8\u5bf9\u5747\u5300\u5730\u6d41\u5165\u591a\u4e2a\u5355\u8bcd\u4f4d\u7f6e\u3002\u8fd9\u7c7b\u4f3c\u4e8e\u77e5\u8bc6\u84b8\u998f\u7b97\u6cd5\u5982\u4f55\u5c06\u201c\u6e29\u5ea6\u201d\u7eb3\u5165\u6559\u5e08\u6a21\u578b\u7684\u903b\u8f91\u4e2d\u3002\u5c31\u50cf\u5728\u77e5\u8bc6\u84b8\u998f\u9886\u57df\u4e00\u6837\uff0c\u901a\u8fc7\u6e29\u5ea6\u56e0\u5b50\u7f29\u653e\u5230 softmax \u7684\u8f93\u5165\u53ef\u4ee5\u6539\u5584\u68af\u5ea6\u6d41\u5e76\u52a0\u5feb\u8bad\u7ec3\u901f\u5ea6\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\u770b\u5230\uff0c\u8fd9\u662f\u89c6\u89c9\u53d8\u6362\u5668\u6210\u529f\u7684\u5173\u952e\u539f\u56e0\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa4 4\uff0c\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u4e0a\u4e00\u6b65\u4e2d\u83b7\u5f97\u7684\u5f52\u4e00\u5316\u6ce8\u610f\u529b\u5206\u6570\u4e0e\u503c\u5411\u91cf\u8fdb\u884c\u70b9\u79ef\u3002\u4ece\u6570\u5b66\u4e0a\u8bb2\uff0c\u8fd9\u8868\u793a\u4e3a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahHbibtUSXSicoVP4jtTSQqric7VYobKq95iaWFtic9GUTlZriaayl1kdKvKiaA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u7531\u4e8e\u4f7f\u7528\u5bc6\u96c6\u5c42\u4e3a\u6bcf\u4e2a\u5355\u8bcd\u83b7\u53d6\u503c\u5411\u91cf V\uff0c\u56e0\u6b64\u6ce8\u610f\u529b\u673a\u5236\u7684\u8f93\u51fa\u5177\u6709 N \u4e2a\u5411\u91cf\uff0c\u4e0e\u8f93\u5165\u5355\u8bcd\u7684\u6570\u91cf\u76f8\u540c\u3002\u6700\u540e\uff0c\u4e3a\u4e86\u83b7\u5f97\u81ea\u6ce8\u610f\u529b\u5c42\u7684\u8f93\u51fa\uff0c\u6211\u4eec\u4f7f\u7528\u5bc6\u96c6\u5c42\u5c06 h \u5411\u91cf\u8f6c\u6362\u4e3a\u4e0e\u8f93\u5165\u76f8\u540c\u7684\u5927\u5c0f\u3002\u5728\u4e0a\u9762\u7684\u7b49\u5f0f\u4e2d\uff0c\u5706\u5f62\u7b26\u53f7\u8868\u793a\u70b9\u79ef\uff0c\u800c\u4e0d\u662f\u77e9\u9635\u4e58\u6cd5\u3002\u5c31\u77e9\u9635\u4e58\u6cd5\u800c\u8a00\uff0c\u8fd0\u7b97\u53ef\u80fd\u53d6\u51b3\u4e8e\u67e5\u8be2\u548c\u952e\u5411\u91cf\u7684\u5f62\u72b6\u3002\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u8282\u4e2d\u66f4\u5177\u4f53\u5316\u7b26\u53f7\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u5934\u81ea\u6ce8\u610f\u529b<\/h2>\n\n\n\n<p>\u60a8\u53ef\u80fd\u5bf9\u5377\u79ef\u7f51\u7edc\u975e\u5e38\u719f\u6089\u3002\u5377\u79ef\u8fd0\u7b97\u662f CNN \u7684\u57fa\u672c\u6784\u5efa\u5757\u3002\u540c\u6837\uff0c\u4e0a\u9762\u4ecb\u7ecd\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u662f\u6240\u6709 Transformer \u6a21\u578b\uff08\u5305\u62ec\u5927\u578b\u8bed\u8a00\u6a21\u578b\u548c\u89c6\u89c9 Transformer\uff09\u7684\u57fa\u672c\u6784\u5efa\u5757\u3002&nbsp;<\/p>\n\n\n\n<p>\u7ee7\u7eed\u7c7b\u6bd4\uff0c\u6211\u4eec\u6c38\u8fdc\u4e0d\u4f1a\u8bbe\u8ba1\u4e00\u4e2a\u53ea\u6709\u4e00\u4e2a\u5377\u79ef\u6ee4\u6ce2\u5668\u7684\u5377\u79ef\u5c42\uff0c\u56e0\u4e3a\u4e00\u4e2a\u6ee4\u6ce2\u5668\u4e0d\u8db3\u4ee5\u6a21\u62df\u81ea\u7136\u56fe\u50cf\u7684\u590d\u6742\u6027\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5728\u4e00\u4e2a\u5c42\u4e2d\u770b\u5230\u8d85\u8fc7 500 \u4e2a\u5377\u79ef\u6ee4\u6ce2\u5668\u7684\u5377\u79ef\u662f\u5f88\u5e38\u89c1\u7684\u3002\u540c\u6837\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u4e00\u4e2a\u5355\u4e00\u7684\u6ce8\u610f\u529b\u5c42\u4e0d\u8db3\u4ee5\u6a21\u62df\u81ea\u7136\u8bed\u8a00\u7684\u6240\u6709\u590d\u6742\u6027\u3002&nbsp;<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u591a\u4e2a\u6ce8\u610f\u529b\u5757\uff08\u79f0\u4e3a\u201c\u5934\u201d\uff09\u5e76\u884c\u5e94\u7528\u4e8e\u540c\u4e00\u5e8f\u5217\u3002\u751f\u6210\u7684\u5c42\u79f0\u4e3a\u591a\u5934\u81ea\u6ce8\u610f\u529b (MHSA)\u3002\u4e0e\u5377\u79ef\u7684\u533a\u522b\u5728\u4e8e\uff0c\u4e00\u4e2a\u6ce8\u610f\u529b\u5934\u6bd4\u5355\u4e2a\u5377\u79ef\u8fc7\u6ee4\u5668\u66f4\u5177\u8868\u73b0\u529b\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528\u7ea6 500 \u4e2a\u6ce8\u610f\u529b\u5934\uff0c\u53ea\u9700\u51e0\u5341\u4e2a\u5373\u53ef\u3002ViT \u4e2d\u6ce8\u610f\u529b\u5934\u6570\u91cf\u7684\u4e00\u4e9b\u5e38\u89c1\u9009\u62e9\u662f 12\u300124 \u548c 32\u3002\u4e0e\u5377\u79ef\u7684\u53e6\u4e00\u4e2a\u533a\u522b\u662f\uff0c\u5728 CNN \u4e2d\uff0c\u6bcf\u4e2a\u540e\u7eed\u5c42\u4e2d\u7684\u8fc7\u6ee4\u5668\u6570\u91cf\u90fd\u4f1a\u589e\u52a0\uff0c\u800c\u6700\u6d41\u884c\u7684 ViT \u4e2d\u7684\u6ce8\u610f\u529b\u5934\u6570\u91cf\u901a\u5e38\u662f\u6052\u5b9a\u7684\uff0c\u4f8b\u5982 12\u3002<\/p>\n\n\n\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u81ea\u6ce8\u610f\u529b\u673a\u5236\u8d85\u8d8a\u4e86\u5bc6\u96c6\u5c42\u3001\u5377\u79ef\u5c42\u548c LSTM \u5355\u5143\uff0c\u5e76\u63d0\u51fa\u4e86\u4e00\u79cd\u5168\u65b0\u7684\u901a\u7528\u8ba1\u7b97\u673a\u5236\u6765\u5bf9\u6570\u636e\u4e2d\u7684\u5173\u7cfb\u8fdb\u884c\u5efa\u6a21\u3002\u5728 NLP \u4e2d\uff0c\u6570\u636e\u5177\u6709\u987a\u5e8f\u6027\uff0c\u4f46\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u53ef\u80fd\u4e0d\u662f\u3002\u7136\u800c\uff0c\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u4e2d\uff0c\u6211\u4eec\u5c06\u770b\u5230\u4f7f\u7528\u6ce8\u610f\u529b\u673a\u5236\u6784\u5efa\u7684\u89c6\u89c9\u8f6c\u6362\u5668\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\u4e2d\u6548\u679c\u5f88\u597d\u3002<\/p>\n\n\n\n<p>\u4e00\u4e9b\u8bf4\u660e\uff1a\u6211\u4eec\u5728\u672c\u7cfb\u5217\u6587\u7ae0\u4e2d\u7684\u76ee\u6807\u662f\u4ecb\u7ecd\u89c6\u89c9\u53d8\u6362\u5668\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u4e86\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u7684\u4f8b\u5b50\u6765\u6fc0\u53d1\u81ea\u6211\u6ce8\u610f\u673a\u5236\u7684\u5f15\u5165\u3002\u7136\u800c\uff0c\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u5c06\u8131\u79bb NLP\uff0c\u4e13\u95e8\u7814\u7a76\u89c6\u89c9\u53d8\u6362\u5668\u3002MHSA\u5728\u8ba1\u7b97\u673a\u89c6\u89c9 (CV) \u548c NLP \u4e2d\u7684\u4f7f\u7528\u65b9\u5f0f\u5b58\u5728\u7ec6\u5fae\u5dee\u522b\u3002\u7279\u522b\u662f\uff0c\u73b0\u9636\u6bb5\u6709\u4e24\u4e2a\u663e\u7740\u7684\u5dee\u5f02\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u89c6\u89c9\u53d8\u6362\u6a21\u578b\u4e00\u822c\u4e0d\u4f7f\u7528\u63a9\u853d\u6ce8\u610f\u529b\uff08\u81ea\u76d1\u7763\u5b66\u4e60\u9664\u5916\uff09<\/li>\n\n\n\n<li>\u89c6\u89c9\u53d8\u6362\u6a21\u578b\u4e0d\u4f7f\u7528\u4ea4\u53c9\u6ce8\u610f\u529b\uff08DETR \u7b49\u7269\u4f53\u68c0\u6d4b\u6a21\u578b\u9664\u5916\uff09<\/li>\n<\/ol>\n\n\n\n<p>\u968f\u7740\u672c\u7cfb\u5217\u7684\u8fdb\u5c55\uff0c\u6211\u4eec\u5c06\u5728\u9002\u5f53\u7684\u65f6\u5019\u4ecb\u7ecd\u8fd9\u4e9b\u53d8\u4f53\uff0c\u4f46\u76ee\u524d\uff0c\u8bf7\u6ce8\u610f\u5b58\u5728\u5176\u4ed6\u53d8\u4f53\uff0c\u672c\u535a\u6587\u7684\u5176\u4f59\u90e8\u5206\u5c06\u4ec5\u4ecb\u7ecd\u89c6\u89c9\u53d8\u6362\u5668\u4e2d\u6700\u5e38\u7528\u7684 MHSA \u7248\u672c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u6ce8\u610f\u529b\u673a\u5236\u7684\u6570\u5b66\u516c\u5f0f<\/h2>\n\n\n\n<p>\u8ba9\u6211\u4eec\u7528\u6570\u5b66\u672f\u8bed\u6765\u8868\u8ff0\u5355\u5934\u6ce8\u610f\u529b\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u8003\u8651\u4e00\u5f20\u4ee5\u67d0\u79cd\u65b9\u5f0f\u88ab\u5206\u5272\u6210\u4e00\u7cfb\u5217\u5757\u7684\u56fe\u50cf<img loading=\"lazy\" decoding=\"async\" height=\"19\" width=\"95\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahlib2U7Sr7e2L5icPW0WrBqrLkJma9oRib4ARbLUZ2pykMZ7x8TmfehxJw\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\uff0c\u5176\u4e2d\u6bcf\u4e2a<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"22\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahjdarfJibpK7ng3ibP76HXlfic3kOwichnhwtBnTQ5wrHtKYwWQCNQDq51Q\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u662f\u7ef4\u5ea6\u4e3a<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"27\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahdYicHgAfFIPJLFy4QzX7KEo7NhLRGBoBp2ptucRSKqILQs6JuqjZSdg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u3002\u8fd9\u91cc\u5c06\u56fe\u50cf\u5206\u5272\u6210\u5757\u7684\u5177\u4f53\u65b9\u6cd5\u5e76\u4e0d\u91cd\u8981\u3002\u8ba9\u6211\u4eec\u6784\u9020\u77e9\u9635<img loading=\"lazy\" decoding=\"async\" height=\"15\" width=\"18\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahsw1IBVdcmUIxIpzl95B2DsdtE1M9DdC6mmu6DUyqtEmFJtibA5F6TlQ\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u7b2c\u4e00\u7ef4\u4e3a\u957f\u5ea6 N\uff0c\u7b2c\u4e8c\u7ef4\u4e3a<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"27\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahl5605aNqtfX5W9TNQZhIDNibnXnjEBdTK6cxOTiaYjU538lILJibNVuLA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u3002\u5982\u524d\u6240\u8ff0\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5bc6\u96c6\u5c42\u6765\u83b7\u53d6\u67e5\u8be2\u3001\u952e\u548c\u503c\u8868\u793a\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u5bc6\u96c6\u5c42\u5c06\u5728\u6ca1\u6709\u4efb\u4f55\u504f\u7f6e\u9879\u548c\u6fc0\u6d3b\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff0c\u56e0\u6b64\u5bc6\u96c6\u5c42\u53ea\u662f\u7b80\u5355\u7684\u77e9\u9635\u4e58\u6cd5\u3002\u8fd9\u4e09\u4e2a\u5c42\u7684\u53c2\u6570\u5206\u522b\u4e3a\u67e5\u8be2\u3001\u952e\u548c\u503c\u7684 WQ \u3001WK \u3001WV \uff0c\u4f7f\u5f97 WQ\u3001WK\u3001WV<img loading=\"lazy\" decoding=\"async\" height=\"20\" width=\"107\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahMFxSKySwZjF3lGMIiaohgRqYCDKPyNKEflBI1eXUcVsQ4d30iaribDcBQ\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">. \u53e6\u4e00\u4e2a\u5e26\u6743\u91cd\u7684\u5bc6\u96c6\u5c42<img loading=\"lazy\" decoding=\"async\" height=\"22\" width=\"142\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahqJw0Fv6M1IqehMQq48AGCBykC1KXCbdCaHxEPichZ18iaGxgjZibkneWg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u548c\u504f\u89c1<img loading=\"lazy\" decoding=\"async\" height=\"22\" width=\"82\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahB2ayNwXv9gvXdaicJbRN0kiazSdmmuXkStlTB7ZljibaJje6UeBETXXuw\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u4e5f\u4f7f\u7528\u3002\u8ba9\u6211\u4eec\u6309\u7167\u4e0a\u4e00\u8282\u76f4\u89c2\u89e3\u91ca\u7684\u90a3\u6837\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa4 1\uff1a\u67e5\u8be2\u3001\u952e\u548c\u503c\u77e9\u9635\uff08\u8fd9\u4e9b\u662f\u77e9\u9635\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u8ba1\u7b97\u5e8f\u5217\u4e2d\u6240\u6709\u5355\u8bcd\u7684\u5411\u91cf\uff09\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahsPjf1St98qKXHuq7KIXTsQ84z3Y8BpwicNw1l3GoUPzCcLX6L2Vdia7Q\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahdbo7niaxIetd5ib420mwcRviabWWFbjIPPV7kHd3gwzcsXuU78GxuaBrQ\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahkI5rGatYxNu3QDrsQHGeEiaeFhjpKHACGDpdGia3lZO5CGOXS3gkEERQ\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u8fd9\u91cc\uff0c<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"42\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiah7gEQCMwr3bUNc0ibbMvaAswy3ECrXkpHOYJSeMDJKibFQ0qIhTMVgbQg\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u662f\u4e00\u4e2a\u8d85\u53c2\u6570\uff0c\u8868\u793a\u952e\u3001\u67e5\u8be2\u548c\u503c\u5411\u91cf\u7684\u7ef4\u5ea6\u3002\u901a\u5e38\uff0c<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"42\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahVZPjyhpsQtLpbcKy6oLZp336A2UIpiaQXh3ibQxd2gx87y4wNuPcyWFA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u8fdc\u5c0f\u4e8e<img loading=\"lazy\" decoding=\"async\" height=\"18\" width=\"27\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahl5605aNqtfX5W9TNQZhIDNibnXnjEBdTK6cxOTiaYjU538lILJibNVuLA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" class=\"\" alt=\"\u56fe\u7247\">\u3002<\/p>\n\n\n\n<p>\u7b2c 2 \u6b65\uff1a\u6ce8\u610f\u529b\u77e9\u9635\u8ba1\u7b97\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahkQuLncndHckneq0OcO74HG4QahRSrnTodhEYkLKt7LnOXxaw3Hwpvw\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5355\u4e2a<code>matmul<\/code>\u64cd\u4f5c\u4f1a\u8ba1\u7b97\u6240\u6709\u5355\u8bcd\u7684\u6ce8\u610f\u529b\u77e9\u9635\uff0c\u5305\u62ec\u540c\u4e00\u5355\u8bcd\u7684\u952e\u4e0e\u67e5\u8be2\u7684\u70b9\u79ef\u3002\u56e0\u6b64\uff0c\u5b83\u5b8c\u5168\u5b9e\u73b0\u4e86\u81ea\u6ce8\u610f\u529b\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa4 3\uff1a\u7f29\u653e\u548c softmax \u5b9e\u73b0\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahw0wF4ShAZwK2R0iaicUXPyE44ltgVpU76R710ezezkUl9xFOkWfc1WOA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u8fd9\u91cc\uff0cdim=-1 \u8868\u793a softmax \u662f\u5728\u884c\u800c\u4e0d\u662f\u5217\u4e2d\u8fdb\u884c\u7684\uff08\u8fd9\u6ca1\u6709\u610f\u4e49\uff09\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa4 4\uff1a\u6ce8\u610f\u529b\u7684\u8f93\u51fa\u8ba1\u7b97\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahzg9duK3OadtZK8lpOv2xZQ1Tpx160zEjnKtXsPO6RAk2fpN7oZB6oA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u6ce8\u610f\u5c42\u7684\u8f93\u51fa\u8ba1\u7b97\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahXvg5J37lSrAWAicSMkFHUKoYficxrQ4P2UknOQAibje5bxkI2TSGFJrVA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><\/figure>\n\n\n\n<p>\u56e0\u6b64\uff0c\u8f93\u51fa\u7684\u5927\u5c0f\u4e0e\u8f93\u5165\u7684\u5927\u5c0f\u76f8\u540c\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u6211\u4eec\u5728\u8fd9\u91cc\u53ea\u5236\u5b9a\u4e86\u5355\u5934\u81ea\u6ce8\u610f\u529b\uff0c\u4f46\u5bf9\u591a\u5934\u7248\u672c\u7684\u63a8\u5e7f\u5305\u542b\u591a\u4e2a\u72ec\u7acb\u7684\u67e5\u8be2\u3001\u952e\u548c\u503c\u6743\u91cd\u77e9\u9635\uff0c\u5e76\u4e14\u975e\u5e38\u7b80\u5355\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PyTorch \u5b9e\u73b0 Self Attention<\/h2>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5bf9\u81ea\u6ce8\u610f\u529b\u5c42\u6709\u4e86\u76f4\u89c2\u548c\u6570\u5b66\u4e0a\u7684\u7406\u89e3\uff0c\u8ba9\u6211\u4eec\u5728 PyTorch \u4e2d\u5b9e\u73b0\u5b83\u3002\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7531\u4e8e\u81ea\u6ce8\u610f\u529b\u673a\u5236\u4e2d\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u5bc6\u96c6\u5c42\uff0c\u56e0\u6b64\u5b58\u5728\u8fc7\u5ea6\u62df\u5408\u7684\u5371\u9669\u3002\u56e0\u6b64\uff0c\u5728MHSA\u7684\u5b9e\u9645\u5b9e\u73b0\u4e2d\uff0c\u5927\u91cf\u4f7f\u7528\u4e86dropout\u5c42\u6765\u907f\u514d\u8fc7\u5ea6\u62df\u5408\u3002<\/li>\n\n\n\n<li>PyTorch \u6709\u4e00\u4e2a\u540d\u4e3a nn.Transformer \u7684\u6a21\u5757\u3002\u5b83\u5b9e\u73b0\u4e86\u5b8c\u6574\u7684 Transformer \u67b6\u6784\uff08\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\uff09\uff0c\u800c\u6211\u4eec\u5c06\u4ec5\u5b9e\u73b0\u6a21\u578b\u7684\u4e00\u90e8\u5206\uff08\u4ec5\u7f16\u7801\u5668\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p>Einops \u5c42\uff1a&nbsp;Einops \u662f\u4e00\u4e2a\u51fa\u8272\u7684\u5f20\u91cf\u64cd\u4f5c\u3001\u91cd\u5851\u548c\u8c03\u6574\u5927\u5c0f\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 einops \u4e2d\u7684 Rearrange \u5c42\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 torch.reshape\uff0c\u5b83\u5177\u6709\u66f4\u51fa\u8272\u7684 API\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e PyTorch\u3001TensorFlow\u3001JAX \u548c numpy \u65e0\u7f1d\u96c6\u6210\u3002\u6211\u4eec\u6ca1\u6709\u8db3\u591f\u7684\u7bc7\u5e45\u5728\u8fd9\u91cc\u4ecb\u7ecd einops\uff0c\u4f46\u5982\u679c\u60a8\u4ee5\u524d\u4ece\u672a\u89c1\u8fc7 einops \u4ee3\u7801\uff0c\u8bf7\u4e0d\u8981\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5b83\u975e\u5e38\u6613\u8bfb\uff0c\u5e76\u4e14\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u60a8\u76f4\u89c2\u731c\u6d4b\u7684\u5b8c\u5168\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u4ee3\u7801&nbsp;\u4e3a\u4e86\u8f7b\u677e\u5b66\u4e60\u672c\u6559\u7a0b\uff0c\u8bf7\u70b9\u51fb\u4e0b\u9762\u7684\u6309\u94ae\u4e0b\u8f7d\u4ee3\u7801\u3002\u514d\u8d39\uff01<br><\/p>\n\n\n\n<p>\u4e0b\u8f7d\u4ee3\u7801<\/p>\n\n\n\n<p>\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u5c06 MHSA \u5c42\u5b9e\u73b0\u4e3a \u7684\u5b50\u7c7b<code>nn.Module<\/code>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>123456789101112\u5341\u4e091415161718192021222324\u4e8c\u5341\u4e94\u4e8c\u5341\u516d\u4e8c\u5341\u4e03\u4e8c\u5341\u516b\u4e8c\u5341\u4e5d\u4e09\u534131\u4e09\u5341\u4e8c33\u4e09\u5341\u56db\u4e09\u5341\u4e94\u4e09\u5341\u516d\u4e09\u5341\u4e03\u4e09\u5341\u516b\u4e09\u5341\u4e5d4041\u56db\u5341\u4e8c43\u56db\u5341\u56db\u56db\u5341\u4e94\u56db\u5341\u516d\u56db\u5341\u4e03\u56db\u5341\u516b4950515253545556<\/td><td><code>from<\/code>&nbsp;<code>torch&nbsp;<\/code><code>import<\/code>&nbsp;<code>nn<\/code><code>from<\/code>&nbsp;<code>einops.layers.torch&nbsp;<\/code><code>import<\/code>&nbsp;<code>Rearrange<\/code>&nbsp;<code>class<\/code>&nbsp;<code>MultiHeadedSelfAttention(nn.Module):<\/code><code>&nbsp;&nbsp;<\/code><code>def<\/code>&nbsp;<code>__init__(<\/code><code>self<\/code><code>, indim, adim, nheads, drop):<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>'''<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>indim: (int) dimension of input vector<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>adim: (int) dimensionality of each attention head<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>nheads: (int) number of heads in MHA layer<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>drop: (float 0~1) probability of dropping a node<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>Implements QKV MSA layer<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>output = softmax(Q*K\/sqrt(d))*V<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>scale= 1\/sqrt(d), here, d = adim<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>'''<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>super<\/code><code>(MultiHeadedSelfAttention,&nbsp;<\/code><code>self<\/code><code>).__init__()<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>hdim<\/code><code>=<\/code><code>adim<\/code><code>*<\/code><code>nheads<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.scale<\/code><code>=<\/code>&nbsp;<code>hdim<\/code><code>*<\/code><code>*<\/code>&nbsp;<code>-<\/code><code>0.5<\/code>&nbsp;<code>#scale in softmax(Q*K*scale)*V<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.key_lyr&nbsp;<\/code><code>=<\/code>&nbsp;<code>self<\/code><code>.get_qkv_layer(indim, hdim, nheads)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>#nn.Linear(indim, hdim, bias=False)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>#there should be nheads layers<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.query_lyr<\/code><code>=<\/code><code>self<\/code><code>.get_qkv_layer(indim, hdim, nheads)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.value_lyr<\/code><code>=<\/code><code>self<\/code><code>.get_qkv_layer(indim, hdim, nheads)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.attention_scores<\/code><code>=<\/code><code>nn.Softmax(dim<\/code><code>=<\/code><code>-<\/code><code>1<\/code><code>)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.dropout<\/code><code>=<\/code><code>nn.Dropout(drop)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>self<\/code><code>.out_layer<\/code><code>=<\/code><code>nn.Sequential(Rearrange(<\/code><code>'bsize nheads indim hdim -&gt; bsize indim (nheads hdim)'<\/code><code>),<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>nn.Linear(hdim, indim),<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>nn.Dropout(drop))<\/code><code>&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;<\/code><code>def<\/code>&nbsp;<code>get_qkv_layer(<\/code><code>self<\/code><code>, indim, hdim, nheads):<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>'''<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>returns query, key, value layer (call this function thrice to get all of q, k &amp; v layers)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>'''<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>layer<\/code><code>=<\/code><code>nn.Sequential(nn.Linear(indim, hdim, bias<\/code><code>=<\/code><code>False<\/code><code>),<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>Rearrange(<\/code><code>'bsize indim (nheads hdim) -&gt; bsize nheads indim hdim'<\/code><code>, nheads<\/code><code>=<\/code><code>nheads))<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>return<\/code>&nbsp;<code>layer<\/code>&nbsp;<code>&nbsp;&nbsp;<\/code><code>def<\/code>&nbsp;<code>forward(<\/code><code>self<\/code><code>, x):<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>query<\/code><code>=<\/code><code>self<\/code><code>.key_lyr(x)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>key<\/code><code>=<\/code><code>self<\/code><code>.query_lyr(x)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>value<\/code><code>=<\/code><code>self<\/code><code>.value_lyr(x)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>dotp<\/code><code>=<\/code><code>torch.matmul(query, key.transpose(<\/code><code>-<\/code><code>1<\/code><code>,&nbsp;<\/code><code>-<\/code><code>2<\/code><code>))<\/code><code>*<\/code><code>self<\/code><code>.scale<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>scores<\/code><code>=<\/code><code>self<\/code><code>.attention_scores(dotp)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>scores<\/code><code>=<\/code><code>self<\/code><code>.dropout(scores)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>weighted<\/code><code>=<\/code><code>torch.matmul(scores, value)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>out<\/code><code>=<\/code><code>self<\/code><code>.out_layer(weighted)<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>&nbsp;<code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code>return<\/code>&nbsp;<code>out<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6784\u9020\u51fd\u6570\u91c7\u7528\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u8f93\u5165\u7684\u5927\u5c0f\u3002\u00a0<\/li>\n\n\n\n<li>\u6ce8\u610f\u529b\u5c42\u7684\u5927\u5c0f<\/li>\n\n\n\n<li>\u6ce8\u610f\u529b\u5934\u7684\u6570\u91cf\uff0c\u4ee5\u53ca<\/li>\n\n\n\n<li>\u8f8d\u5b66\u6982\u7387<\/li>\n<\/ul>\n\n\n\n<p>\u6b65\u9aa4 1\uff1a\u8be5\u51fd\u6570<code>get_qkv_layer<\/code>\u7528\u4e8e\u540c\u65f6\u83b7\u53d6\u6240\u6709\u6ce8\u610f\u529b\u5934\u7684\u67e5\u8be2\u3001\u952e\u548c\u503c\u5411\u91cf\u7684\u5bc6\u96c6\u5c42\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u5c06\u5728 for \u5faa\u73af\u4e2d\u5b9e\u73b0\uff0c\u6bcf\u4e2a\u6ce8\u610f\u529b\u5934\u8fdb\u884c\u4e00\u6b21\u8fed\u4ee3\u3002\u4f46\u662f\uff0c\u5c06\u591a\u4e2a\u77e9\u9635\u4e58\u6cd5\u8fd0\u7b97\u5408\u5e76\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u51cf\u5c11\u5ef6\u8fdf\u5e76\u63d0\u9ad8 CUDA \u5360\u7528\u7387\u3002einops \u5e93\u4e2d\u7684 Rearrange \u5c42\u91cd\u5851 QKV \u77e9\u9635\uff0c\u4f7f\u5934\u7ef4\u5ea6\u7d27\u6328\u7740\u6279\u5904\u7406\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<p>\u7b2c 2 \u6b65\uff1a\u7136\u540e\u5c06\u6ce8\u610f\u529b\u77e9\u9635\u6cbf\u5176\u6700\u540e\u4e24\u4e2a\u7ef4\u5ea6\u8f6c\u7f6e\uff0c\u8ba1\u7b97\u4e3a<code>matul<\/code>\u67e5\u8be2\u548c\u5bc6\u94a5\u7684\u6ce8\u610f\u529b\u77e9\u9635\uff0c\u6b63\u5982\u7b2c 2 \u8282\u548c\u7b2c 3 \u8282\u4e2d\u89e3\u91ca\u7684\u90a3\u6837\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa4 3\uff1a\u5c06\u6ce8\u610f\u529b\u77e9\u9635\u6309\u6ce8\u610f\u529b\u7ef4\u5ea6\u7684\u5e73\u65b9\u6839\u7f29\u653e\uff0c\u7136\u540e\u91c7\u7528 softmax \u5f97\u5230\u6ce8\u610f\u529b\u5f97\u5206\u77e9\u9635\u3002\u5f97\u5206\u77e9\u9635\u540e\u9762\u8ddf\u7740\u4e00\u4e2a dropout \u5c42\uff0c\u4ee5\u9632\u6b62\u8fc7\u5ea6\u62df\u5408\u3002<\/p>\n\n\n\n<p>\u6b65\u9aa44\uff1a\u5229\u7528\u5f97\u5206\u77e9\u9635\u8ba1\u7b97\u503c\u77e9\u9635\u7684\u52a0\u6743\u548c\u3002\u6700\u540e\uff0ceinops Rearrange \u5c42\u5c06\u6240\u6709\u6ce8\u610f\u529b\u5934\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u5e76\u4f7f\u7528\u7ebf\u6027\u5c42\u5c06\u6ce8\u610f\u529b\u5c42\u7684\u8f93\u51fa\u8f6c\u6362\u4e3a\u4e0e\u8f93\u5165\u76f8\u540c\u7684\u5f62\u72b6\u3002\u518d\u6b21\u5e94\u7528 Dropout \u4ee5\u9632\u6b62\u8fc7\u62df\u5408\u3002<\/p>\n\n\n\n<p>4.1 \u57fa\u672c\u7ed3\u679c<\/p>\n\n\n\n<p>\u6211\u4eec\u5f15\u5165\u7684 MHSA \u5c42\u53ea\u662f Transformer \u6a21\u578b\u4e2d\u4f7f\u7528\u7684\u6784\u5efa\u5757\u3002\u9664\u4e86\u9a8c\u8bc1\u5176\u81ea\u8eab\u7684\u5de5\u4f5c\u4e4b\u5916\uff0c\u6211\u4eec\u65e0\u6cd5\u7528\u5b83\u505a\u5f88\u591a\u4e8b\u60c5\u3002\u6211\u4eec\u5c06\u8f93\u5165 x \u4f20\u9012\u5230 MHSA \u5c42\uff0c\u5e76\u9a8c\u8bc1\u8f93\u51fa\u662f\u5426\u5177\u6709\u4e0e\u8f93\u5165\u76f8\u540c\u7684\u5f62\u72b6\u3002\u6211\u4eec\u5c06\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u4e2d\u4f7f\u7528\u6b64\u5c42\u6765\u6784\u5efa\u89c6\u89c9 Transformer \u6a21\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/ciaOAxzQ9MXIZ0iciaSaENUFfpnIc9mBMiahicCrgqcBertN9wibwH0dibKLng99re5QLu3dkd69CbsCg7JpopgBhE2ZA\/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" alt=\"\u56fe\u7247\"\/><figcaption class=\"wp-element-caption\">\u56fe5.\u9a8c\u8bc1MHSA\u5c42\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u6982\u62ec<\/h2>\n\n\n\n<p>\u8fd9\u7bc7\u535a\u6587\u5960\u5b9a\u4e86 transformer \u6a21\u578b\u7684\u57fa\u7840\uff0c\u5c24\u5176\u662f\u89c6\u89c9 transformer\u3002\u673a\u5668\u5b66\u4e60\u7684\u6700\u65b0\u8fdb\u5c55\u4ee5 transformer \u4e3a\u6838\u5fc3\uff0c\u800c transformer \u7684\u6838\u5fc3\u662f\u591a\u5934\u81ea\u6ce8\u610f\u529b\u5c42\u3002\u56e0\u6b64\uff0c\u76f4\u89c2\u548c\u6570\u5b66\u5730\u7406\u89e3\u6ce8\u610f\u529b\u673a\u5236\u5bf9\u4e8e\u4f60\u672a\u6765\u7684\u9879\u76ee\u548c\u804c\u4e1a\u751f\u6daf\u81f3\u5173\u91cd\u8981\u3002&nbsp;<\/p>\n\n\n\n<p>\u6211\u4eec\u9996\u5148\u56de\u987e\u4e86\u5faa\u73af\u7f51\u7edc\u548c LSTM \u80cc\u540e\u7684\u6838\u5fc3\u6982\u5ff5\u3002\u5b83\u4eec\u7684\u6210\u529f\u548c\u4e0d\u8db3\u8ba9\u6211\u4eec\u4e86\u89e3\u4e86\u6ce8\u610f\u529b\u673a\u5236\u7684\u9002\u7528\u8303\u56f4\u4ee5\u53ca\u5b83\u4e0e\u65e9\u671f\u6a21\u578b\u7684\u4e0d\u540c\u4e4b\u5904\u3002&nbsp;<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u76f4\u89c2\u5730\u4e86\u89e3\u4e86\u6ce8\u610f\u529b\u5c42\u80cc\u540e\u7684\u673a\u5236\u3002\u6211\u4eec\u4e00\u6b65\u6b65\u5730\u8ba1\u7b97\u67e5\u8be2\u3001\u952e\u3001\u503c\u3001\u6ce8\u610f\u529b\u77e9\u9635\u3001\u5206\u6570\uff0c\u6700\u540e\u662f\u6ce8\u610f\u529b\u5c42\u7684\u8f93\u51fa\u3002\u5728\u83b7\u5f97\u76f4\u89c2\u7406\u89e3\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u8fd9\u4e9b\u6982\u5ff5\u5f62\u5f0f\u5316\u4e3a\u5177\u4f53\u7684\u6570\u5b66\u5f62\u5f0f\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u6570\u5b66\u548c\u76f4\u89c2\u7684\u7406\u89e3\u8f6c\u5316\u4e3a\u5b9e\u73b0\u591a\u5934\u81ea\u6ce8\u610f\u529b\u5c42\u7684 PyTorch \u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u6253\u4e0b\u4e86\u57fa\u7840\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u4e2d\u8f7b\u677e\u7406\u89e3\u89c6\u89c9\u8f6c\u6362\u5668\u3002\u6211\u4eec\u5e0c\u671b\u5728\u90a3\u91cc\u89c1\u5230\u4f60\u3002<\/p>\n\n\n\n<p>\u6e90\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>http:\/\/www.gitpp.com\/datasets\/learnopencv-cn\/tree\/master\/Attention_Mechanism_Introduction<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u521a\u521a\u7ed3\u675f\u7684 2022 \u5e74\u662f\u4eba\u5de5\u667a\u80fd\u9886\u57df\u53d6\u5f97\u8bf8\u591a\u8fdb\u6b65\u7684\u4e0d\u53ef\u601d\u8bae\u7684\u4e00\u5e74\u3002\u4eba\u5de5\u667a\u80fd\u9886\u57df\u6700\u8fd1\u53d6\u5f97\u7684\u5927\u591a\u6570\u91cc\u7a0b\u7891\u5f0f\u6210\u5c31\u90fd [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-transformer"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":1,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":784,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/783\/revisions\/784"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}