{"id":1972,"date":"2022-01-13T19:16:49","date_gmt":"2022-01-13T11:16:49","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1972"},"modified":"2022-01-13T19:16:51","modified_gmt":"2022-01-13T11:16:51","slug":"maemasked-autoencoders-are-scalable-vision-learners","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/13\/maemasked-autoencoders-are-scalable-vision-learners\/","title":{"rendered":"MAE:Masked Autoencoders Are Scalable Vision Learners"},"content":{"rendered":"\n<p>\u6458\u81ea Jack Cui<\/p>\n\n\n\n<p>\u9a6c\u8d5b\u514b\uff0c\u514b\u661f\uff0c\u771f\u6765\u4e86\uff01<\/p>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">\u4f55\u607a\u660e\u5927\u795e\u7684\u65b0\u4f5c \u8bba\u6587\uff1ahttps:\/\/arxiv.org\/abs\/2111.06377<\/p>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\"><strong>\u9879\u76ee\u5730\u5740\uff1ahttps:\/\/github.com\/facebookresearch\/mae<\/strong><\/p>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">\u7b80\u5355\u8bb2\uff1a\u5c06\u56fe\u7247<strong>\u968f\u673a\u906e\u6321<\/strong>\uff0c<strong>\u7136\u540e\u590d\u539f<\/strong>\u3002\u5e76\u4e14\u906e\u6321\u7684\u6bd4\u4f8b\uff0c<strong>\u975e\u5e38\u5927<\/strong>\uff01<strong>\u8d85\u8fc7\u6574\u5f20\u56fe\u7684 80%<\/strong> \uff0c\u6211\u4eec\u76f4\u63a5\u770b\u6548\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"935\" height=\"724\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-201.png\" alt=\"\" class=\"wp-image-1979\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-201.png 935w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-201-300x232.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-201-768x595.png 768w\" sizes=\"(max-width: 935px) 100vw, 935px\" \/><\/figure>\n\n\n\n<p>\u7b2c\u4e00\u5217\u662f\u906e\u6321\u56fe\uff0c\u7b2c\u4e8c\u5217\u662f\u4fee\u590d\u7ed3\u679c\uff0c\u7b2c\u4e09\u5217\u662f\u539f\u56fe\u3002\u56fe\u7247\u592a\u591a\uff0c\u53ef\u80fd\u770b\u4e0d\u6e05\uff0c\u6211\u4eec\u5355\u770b\u4e00\u4e2a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"262\" height=\"263\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-203.png\" alt=\"\" class=\"wp-image-1982\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-203.png 262w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-203-150x150.png 150w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-203-120x120.png 120w\" sizes=\"(max-width: 262px) 100vw, 262px\" \/><\/figure>\n\n\n\n<p>\u770b\u8fd9\u4e2a\u906e\u6321\u7684\u7a0b\u5ea6\uff0c\u8868\u9488\u3001\u8868\u76d8\u51e0\u4e4e\u90fd\u770b\u4e0d\u89c1\u4e86\u3002\u4f46\u662f MAE \u4f9d\u7136\u80fd\u591f\u4fee\u590d\u51fa\u6765\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"523\" height=\"263\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-204.png\" alt=\"\" class=\"wp-image-1983\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-204.png 523w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-204-300x151.png 300w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure>\n\n\n\n<p><strong>\u8fd9\u4e2a\u6548\u679c\u771f\u7684\u5f88\u60ca\u8273\uff01<\/strong>\u751a\u81f3\u5bf9\u4e8e\u906e\u6321 <strong>95%<\/strong> \u7684\u9762\u79ef\u7684\u56fe\u7247\u4f9d\u7136 work\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"426\" height=\"214\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-202.png\" alt=\"\" class=\"wp-image-1981\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-202.png 426w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-202-300x151.png 300w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/figure>\n\n\n\n<p><strong>\u770b\u5de6\u56fe\uff0c\u4f60\u80fd\u770b\u51fa\u6765\u88ab\u906e\u6321\u7684\u662f\u8611\u83c7\u5417\uff1f\uff1f<\/strong>MAE \u5374\u80fd\u8f7b\u677e\u4fee\u590d\u51fa\u6765\u3002\u63a5\u4e0b\u6765\uff0c\u8ddf\u5927\u5bb6\u804a\u804a MAE\u3002<\/p>\n\n\n\n<h3 class=\"has-light-pink-background-color has-background\"><strong>Vit<\/strong><\/h3>\n\n\n\n<p>\u8bb2\u89e3 MAE \u4e4b\u524d\u4e0d\u5f97\u4e0d\u5148\u8bf4\u4e0b Vit\u3002\u7ea2\u904d\u5927\u6c5f\u5357\u5317\u7684 Vision Transformer\uff0cViT\u3002\u9886\u57df\u5185\u7684\u5c0f\u4f19\u4f34\uff0c\u6216\u591a\u6216\u5c11\u90fd\u5e94\u8be5\u542c\u8bf4\u8fc7\u3002\u5b83\u5c06 Transformer \u5e94\u7528\u5230\u4e86 CV \u4e0a\u9762\uff0c\u5c06\u6574\u4e2a\u56fe\u5206\u4e3a 16 * 16 \u7684\u5c0f\u65b9\u5757\uff0c\u6bcf\u4e2a\u65b9\u5757\u505a\u6210\u4e00\u4e2a\u8bcd\uff0c\u7136\u540e\u653e\u8fdb Transformer \u8fdb\u884c\u8bad\u7ec3\u3002\u89c6\u89c9transformer \u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406 \u4e2d\u7684transformer\u53ef\u4ee5\u8fdb\u884c\u7c7b\u6bd4\uff0c<strong>\u53ef\u4ee5\u628a\u4e00\u4e2a\u56fe\u50cf\u5757\u7406\u89e3\u6210\u4e00\u4e2a\u5355\u8bcd\u3002<\/strong><\/p>\n\n\n\n<h3><strong>MAE<\/strong><\/h3>\n\n\n\n<p>MAE \u7ed3\u6784\u8bbe\u8ba1\u7684\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"658\" height=\"625\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-205.png\" alt=\"\" class=\"wp-image-1986\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-205.png 658w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-205-300x285.png 300w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<p>\u5c06\u4e00\u5f20\u56fe\u968f\u673a\u6253 Mask\uff0c\u672a Mask \u90e8\u5206\u8f93\u5165\u7ed9 Encoder \u8fdb\u884c\u7f16\u7801\u5b66\u4e60\uff0c\u8fd9\u4e2a Encoder \u5c31\u662f Vit\uff0c\u7136\u540e\u5f97\u5230\u6bcf\u4e2a\u5757\u7684\u7279\u5f81\u3002\u518d\u5c06\u672a Mask \u90e8\u5206\u4ee5\u53ca Mask \u90e8\u5206\u5168\u90e8\u8f93\u5165\u7ed9 Decoder \u8fdb\u884c\u89e3\u7801\u5b66\u4e60\uff0c\u6700\u7ec8\u76ee\u6807\u662f\u4fee\u590d\u56fe\u7247\u3002\u800c Decoder \u5c31\u662f\u4e00\u4e2a\u8f7b\u91cf\u5316\u7684 Transformer\u3002\u5b83\u7684\u635f\u5931\u51fd\u6570\u5c31\u662f\u666e\u901a\u7684 MSE\u3002\u6240\u4ee5\u8bf4\uff0c MAE \u7684 Encoder \u548c Decoder \u7ed3\u6784\u4e0d\u540c\uff0c\u662f\u975e\u5bf9\u79f0\u5f0f\u7684\u3002Encoder \u5c06\u8f93\u5165\u7f16\u7801\u4e3a latent representation\uff0c\u800c Decoder \u5c06\u4ece latent representation \u91cd\u5efa\u539f\u59cb\u4fe1\u53f7\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u9879\u76ee\u63d0\u4f9b\u4e86 Colab\uff0c\u5982\u679c\u4f60\u80fd\u767b\u9646\uff0c\u90a3\u4e48\u53ef\u4ee5\u76f4\u63a5\u4f53\u9a8c\uff1a<a href=\"https:\/\/colab.research.google.com\/github\/facebookresearch\/mae\/blob\/main\/demo\/mae_visualize.ipynb\">https:\/\/colab.research.google.com\/github\/facebookresearch\/mae\/blob\/main\/demo\/mae_visualize.ipynb<\/a><\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u80fd\u767b\u9646\uff0c\u53ef\u4ee5\u76f4\u63a5\u672c\u5730\u90e8\u7f72\uff0c\u4f5c\u8005\u63d0\u4f9b\u4e86\u9884\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"799\" height=\"207\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-215.png\" alt=\"\" class=\"wp-image-2022\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-215.png 799w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-215-300x78.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-215-768x199.png 768w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<p>MAE \u53ef\u4ee5\u7528\u6765\u751f\u6210\u4e0d\u5b58\u5728\u7684\u5185\u5bb9\uff0c\u5c31\u50cf GAN \u4e00\u6837\u3002<\/p>\n\n\n\n<h2 class=\"has-light-pink-background-color has-background\">\u9996\u5148\u6765\u770b\u770b\u795e\u9b54\u662f Transformer \uff1a<\/h2>\n\n\n\n<p>Transformer \u6700\u521d\u4e3b\u8981\u5e94\u7528\u4e8e\u4e00\u4e9b\u81ea\u7136\u8bed\u8a00\u5904\u7406\u573a\u666f\uff0c\u6bd4\u5982\u7ffb\u8bd1\u3001\u6587\u672c\u5206\u7c7b\u3001\u5199\u5c0f\u8bf4\u3001\u5199\u6b4c\u7b49\u3002\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\uff0cTransformer \u5f00\u59cb\u5f81\u6218\u89c6\u89c9\u9886\u57df\uff0c\u5206\u7c7b\u3001\u68c0\u6d4b\u7b49\u4efb\u52a1\u5747\u4e0d\u5728\u8bdd\u4e0b\uff0c\u9010\u6e10\u8d70\u4e0a\u4e86<strong>\u591a\u6a21\u6001<\/strong>\u7684\u9053\u8def\u3002<\/p>\n\n\n\n<p>Transformer \u662f Google \u5728 2017 \u5e74\u63d0\u51fa\u7684\u7528\u4e8e\u673a\u5668\u7ffb\u8bd1\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"923\" height=\"218\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-207.png\" alt=\"\" class=\"wp-image-1990\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-207.png 923w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-207-300x71.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-207-768x181.png 768w\" sizes=\"(max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<p>Transformer \u7684\u5185\u90e8\uff0c\u5728\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Encoder-Decoder \u7684\u7ed3\u6784\uff0c\u5373 \u7f16\u7801\u5668-\u89e3\u7801\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"756\" height=\"474\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-206.png\" alt=\"\" class=\"wp-image-1989\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-206.png 756w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-206-300x188.png 300w\" sizes=\"(max-width: 756px) 100vw, 756px\" \/><\/figure>\n\n\n\n<p class=\"has-light-blue-background-color has-background\">Transformer \u4e2d\u629b\u5f03\u4e86\u4f20\u7edf\u7684 CNN \u548c RNN\uff0c\u6574\u4e2a\u7f51\u7edc\u7ed3\u6784\u5b8c\u5168\u7531 Attention \u673a\u5236\u7ec4\u6210\uff0c\u5e76\u4e14\u91c7\u7528\u4e86 6 \u5c42 Encoder-Decoder \u7ed3\u6784\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"667\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-208-1024x667.png\" alt=\"\" class=\"wp-image-1991\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-208-1024x667.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-208-300x195.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-208-768x500.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-208.png 1080w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u663e\u7136\uff0cTransformer \u4e3b\u8981\u5206\u4e3a<strong>\u4e24\u5927\u90e8\u5206<\/strong>\uff0c\u5206\u522b\u662f<strong>\u7f16\u7801\u5668<\/strong>\u548c<strong>\u89e3\u7801\u5668<\/strong>\u3002\u6574\u4e2a Transformer \u662f\u7531 6 \u4e2a\u8fd9\u6837\u7684\u7ed3\u6784\u7ec4\u6210\uff0c\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\uff0c\u6211\u4eec\u53ea\u770b\u5176\u4e2d\u4e00\u4e2aEncoder-Decoder \u7ed3\u6784\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"1000\" height=\"843\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-211.png\" alt=\"\" class=\"wp-image-1994\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-211.png 1000w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-211-300x253.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-211-768x647.png 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Why do we work?\uff0c\u6211\u4eec\u4e3a\u4ec0\u4e48\u5de5\u4f5c\uff1f\u5de6\u4fa7\u7ea2\u6846\u662f<strong>\u7f16\u7801\u5668<\/strong>\uff0c\u53f3\u4fa7\u7ea2\u6846\u662f<strong>\u89e3\u7801\u5668<\/strong>\uff0c<strong>\u7f16\u7801\u5668<\/strong>\u8d1f\u8d23\u628a\u81ea\u7136\u8bed\u8a00\u5e8f\u5217\u6620\u5c04\u6210\u4e3a\u9690\u85cf\u5c42\uff08\u4e0a\u56fe\u7b2c2\u6b65\uff09\uff0c\u5373\u542b\u6709\u81ea\u7136\u8bed\u8a00\u5e8f\u5217\u7684\u6570\u5b66\u8868\u8fbe\u3002<strong>\u89e3\u7801\u5668<\/strong>\u628a\u9690\u85cf\u5c42\u518d\u6620\u5c04\u4e3a\u81ea\u7136\u8bed\u8a00\u5e8f\u5217\uff0c\u4ece\u800c\u4f7f\u6211\u4eec\u53ef\u4ee5\u89e3\u51b3\u5404\u79cd\u95ee\u9898\uff0c\u5982\u60c5\u611f\u5206\u6790\u3001\u673a\u5668\u7ffb\u8bd1\u3001\u6458\u8981\u751f\u6210\u3001\u8bed\u4e49\u5173\u7cfb\u62bd\u53d6\u7b49\u3002\u7b80\u5355\u8bf4\u4e0b\uff0c\u4e0a\u56fe\u6bcf\u4e00\u6b65\u90fd\u505a\u4e86\u4ec0\u4e48\uff1a<\/p>\n\n\n\n<ul><li>\u8f93\u5165\u81ea\u7136\u8bed\u8a00\u5e8f\u5217\u5230\u7f16\u7801\u5668: Why do we work?(\u4e3a\u4ec0\u4e48\u8981\u5de5\u4f5c)\uff1b<\/li><li>\u7f16\u7801\u5668\u8f93\u51fa\u7684\u9690\u85cf\u5c42\uff0c\u518d\u8f93\u5165\u5230\u89e3\u7801\u5668\uff1b<\/li><li>\u8f93\u5165 &lt;\ud835\udc60\ud835\udc61\ud835\udc4e\ud835\udc5f\ud835\udc61&gt; (\u8d77\u59cb)\u7b26\u53f7\u5230\u89e3\u7801\u5668\uff1b<\/li><li>\u89e3\u7801\u5668\u5f97\u5230\u7b2c\u4e00\u4e2a\u5b57&#8221;\u4e3a&#8221;\uff1b<\/li><li>\u5c06\u5f97\u5230\u7684\u7b2c\u4e00\u4e2a\u5b57&#8221;\u4e3a&#8221;\u843d\u4e0b\u6765\u518d\u8f93\u5165\u5230\u89e3\u7801\u5668\uff1b<\/li><li>\u89e3\u7801\u5668\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5b57&#8221;\u4ec0&#8221;\uff1b<\/li><li>\u5c06\u5f97\u5230\u7684\u7b2c\u4e8c\u5b57\u518d\u843d\u4e0b\u6765\uff0c\u76f4\u5230\u89e3\u7801\u5668\u8f93\u51fa &lt;\ud835\udc52\ud835\udc5b\ud835\udc51&gt; (\u7ec8\u6b62\u7b26)\uff0c\u5373\u5e8f\u5217\u751f\u6210\u5b8c\u6210\u3002<\/li><\/ul>\n\n\n\n<p>\u89e3\u7801\u5668\u548c\u7f16\u7801\u5668\u7684\u7ed3\u6784\u7c7b\u4f3c\uff0c\u672c\u6587\u4ee5\u7f16\u7801\u5668\u90e8\u5206\u8fdb\u884c\u8bb2\u89e3\u3002\u5373<strong>\u628a\u81ea\u7136\u8bed\u8a00\u5e8f\u5217\u6620\u5c04\u4e3a\u9690\u85cf\u5c42\u7684\u6570\u5b66\u8868\u8fbe<\/strong>\u7684\u8fc7\u7a0b\uff0c\u56e0\u4e3a\u7406\u89e3\u4e86\u7f16\u7801\u5668\u4e2d\u7684\u7ed3\u6784\uff0c\u7406\u89e3\u89e3\u7801\u5668\u5c31\u975e\u5e38\u7b80\u5355\u4e86\u3002\u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u6211\u5c06\u7f16\u7801\u5668\u5206\u4e3a 4 \u4e2a\u90e8\u5206\uff0c\u4f9d\u6b21\u8bb2\u89e3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"1000\" height=\"728\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-210.png\" alt=\"\" class=\"wp-image-1993\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-210.png 1000w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-210-300x218.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-210-768x559.png 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h3><strong>1\u3001\u4f4d\u7f6e\u5d4c\u5165\uff08\ud835\udc5d\ud835\udc5c\ud835\udc60\ud835\udc56\ud835\udc61\ud835\udc56\ud835\udc5c\ud835\udc5b\ud835\udc4e\ud835\udc59 \ud835\udc52\ud835\udc5b\ud835\udc50\ud835\udc5c\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54\uff09<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u8f93\u5165\u6570\u636e X \u7ef4\u5ea6\u4e3a[batch size, sequence length]\u7684\u6570\u636e\uff0c\u6bd4\u5982\u6211\u4eec\u4e3a\u4ec0\u4e48\u5de5\u4f5c\u3002batch size \u5c31\u662f batch \u7684\u5927\u5c0f\uff0c\u8fd9\u91cc\u53ea\u6709\u4e00\u53e5\u8bdd\uff0c\u6240\u4ee5 batch size \u4e3a 1\uff0csequence length \u662f\u53e5\u5b50\u7684\u957f\u5ea6\uff0c\u4e00\u5171 7 \u4e2a\u5b57\uff0c\u6240\u4ee5\u8f93\u5165\u7684\u6570\u636e\u7ef4\u5ea6\u662f [1, 7]\u3002\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u5c06\u8fd9\u53e5\u8bdd\u8f93\u5165\u5230<strong>\u7f16\u7801\u5668<\/strong>\u4e2d\uff0c\u56e0\u4e3a Tranformer \u4e0d\u8ba4\u8bc6\uff0c\u6211\u4eec\u9700\u8981\u5148\u8fdb\u884c<strong>\u5b57\u5d4c\u5165<\/strong>\uff0c\u5373\u5f97\u5230\u56fe\u4e2d\u7684 \u3002\u7b80\u5355\u70b9\u8bf4\uff0c\u5c31\u662f\u6587\u5b57->\u5b57\u5411\u91cf\u7684\u8f6c\u6362\uff0c\u8fd9\u79cd\u8f6c\u6362\u662f\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u8ba4\u8bc6\u7684\u6570\u5b66\u8868\u793a\uff0c\u7528\u5230\u7684\u65b9\u6cd5\u5c31\u662f Word2Vec\uff0cWord2Vec \u7684\u5177\u4f53\u7ec6\u8282\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u6682\u4e14\u4e0d\u7528\u4e86\u89e3\uff0c\u8fd9\u4e2a\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u3002\u5f97\u5230\u7ef4\u5ea6\u662f [batch size, sequence length, embedding dimension]\uff0cembedding dimension \u7684\u5927\u5c0f\u7531 Word2Vec \u7b97\u6cd5\u51b3\u5b9a\uff0cTranformer \u91c7\u7528 512 \u957f\u5ea6\u7684\u5b57\u5411\u91cf\u3002\u6240\u4ee5 \u7684\u7ef4\u5ea6\u662f [1, 7, 512]\u3002\u81f3\u6b64\uff0c\u8f93\u5165\u7684\u6211\u4eec\u4e3a\u4ec0\u4e48\u5de5\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u77e9\u9635\u6765\u7b80\u5316\u8868\u793a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"692\" height=\"231\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-209.png\" alt=\"\" class=\"wp-image-1992\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-209.png 692w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-209-300x100.png 300w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u6587\u5b57\u7684\u5148\u540e\u987a\u5e8f\uff0c\u5f88\u91cd\u8981\u3002\u6bd4\u5982\u5403\u996d\u6ca1\u3001\u6ca1\u5403\u996d\u3001\u6ca1\u996d\u5403\u3001\u996d\u5403\u6ca1\u3001\u996d\u6ca1\u5403\uff0c\u540c\u6837\u4e09\u4e2a\u5b57\uff0c\u987a\u5e8f\u98a0\u5012\uff0c\u6240\u8868\u8fbe\u7684\u542b\u4e49\u5c31\u4e0d\u540c\u4e86\u3002\u6587\u5b57\u7684\u4f4d\u7f6e\u4fe1\u606f\u5f88\u91cd\u8981\uff0cTranformer \u6ca1\u6709\u7c7b\u4f3c RNN \u7684\u5faa\u73af\u7ed3\u6784\uff0c\u6ca1\u6709\u6355\u6349\u987a\u5e8f\u5e8f\u5217\u7684\u80fd\u529b\u3002\u4e3a<strong>\u4e86\u4fdd\u7559\u8fd9\u79cd\u4f4d\u7f6e\u4fe1\u606f\u4ea4\u7ed9 Tranformer \u5b66\u4e60\uff0c\u6211\u4eec\u9700\u8981\u7528\u5230\u4f4d\u7f6e\u5d4c\u5165\u3002<\/strong>\u52a0\u5165\u4f4d\u7f6e\u4fe1\u606f\u7684\u65b9\u5f0f\u975e\u5e38\u591a\uff0c\u6700\u7b80\u5355\u7684\u53ef\u4ee5\u662f\u76f4\u63a5\u5c06\u7edd\u5bf9\u5750\u6807 0,1,2 \u7f16\u7801\u3002Tranformer \u91c7\u7528\u7684\u662f sin-cos \u89c4\u5219\uff0c\u4f7f\u7528\u4e86 sin \u548c cos \u51fd\u6570\u7684\u7ebf\u6027\u53d8\u6362\u6765\u63d0\u4f9b\u7ed9\u6a21\u578b\u4f4d\u7f6e\u4fe1\u606f\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-213.png\" alt=\"\" class=\"wp-image-1998\" width=\"312\" height=\"79\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-213.png 457w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-213-300x75.png 300w\" sizes=\"(max-width: 312px) 100vw, 312px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e0a\u5f0f\u4e2d pos \u6307\u7684\u662f\u53e5\u4e2d\u5b57\u7684\u4f4d\u7f6e\uff0c\u53d6\u503c\u8303\u56f4\u662f [0, \ud835\udc5a\ud835\udc4e\ud835\udc65 \ud835\udc60\ud835\udc52\ud835\udc5e\ud835\udc62\ud835\udc52\ud835\udc5b\ud835\udc50\ud835\udc52 \ud835\udc59\ud835\udc52\ud835\udc5b\ud835\udc54\ud835\udc61\u210e)\uff0ci \u6307\u7684\u662f\u5b57\u5d4c\u5165\u7684\u7ef4\u5ea6, \u53d6\u503c\u8303\u56f4\u662f [0, \ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54 \ud835\udc51\ud835\udc56\ud835\udc5a\ud835\udc52\ud835\udc5b\ud835\udc60\ud835\udc56\ud835\udc5c\ud835\udc5b)\u3002 \u5c31\u662f \ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54 \ud835\udc51\ud835\udc56\ud835\udc5a\ud835\udc52\ud835\udc5b\ud835\udc60\ud835\udc56\ud835\udc5c\ud835\udc5b \u7684\u5927\u5c0f\u3002\u4e0a\u9762\u6709 sin \u548c cos \u4e00\u7ec4\u516c\u5f0f\uff0c\u4e5f\u5c31\u662f\u5bf9\u5e94\u7740 \ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54 \ud835\udc51\ud835\udc56\ud835\udc5a\ud835\udc52\ud835\udc5b\ud835\udc60\ud835\udc56\ud835\udc5c\ud835\udc5b \u7ef4\u5ea6\u7684\u4e00\u7ec4\u5947\u6570\u548c\u5076\u6570\u7684\u5e8f\u53f7\u7684\u7ef4\u5ea6\uff0c\u4ece\u800c\u4ea7\u751f\u4e0d\u540c\u7684\u5468\u671f\u6027\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5bfc\u5165\u4f9d\u8d56\u5e93\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport seaborn as sns\r\nimport math\r\n\r\ndef get_positional_encoding(max_seq_len, embed_dim):\r\n    # \u521d\u59cb\u5316\u4e00\u4e2apositional encoding\r\n    # embed_dim: \u5b57\u5d4c\u5165\u7684\u7ef4\u5ea6\r\n    # max_seq_len: \u6700\u5927\u7684\u5e8f\u5217\u957f\u5ea6\r\n    positional_encoding = np.array(&#91;\r\n        &#91;pos \/ np.power(10000, 2 * i \/ embed_dim) for i in range(embed_dim)]\r\n        if pos != 0 else np.zeros(embed_dim) for pos in range(max_seq_len)])\r\n    positional_encoding&#91;1:, 0::2] = np.sin(positional_encoding&#91;1:, 0::2])  # dim 2i \u5076\u6570\r\n    positional_encoding&#91;1:, 1::2] = np.cos(positional_encoding&#91;1:, 1::2])  # dim 2i+1 \u5947\u6570\r\n    # \u5f52\u4e00\u5316, \u7528\u4f4d\u7f6e\u5d4c\u5165\u7684\u6bcf\u4e00\u884c\u9664\u4ee5\u5b83\u7684\u6a21\u957f\r\n    # denominator = np.sqrt(np.sum(position_enc**2, axis=1, keepdims=True))\r\n    # position_enc = position_enc \/ (denominator + 1e-8)\r\n    return positional_encoding\r\n    \r\npositional_encoding = get_positional_encoding(max_seq_len=100, embed_dim=16)\r\nplt.figure(figsize=(10,10))\r\nsns.heatmap(positional_encoding)\r\nplt.title(\"Sinusoidal Function\")\r\nplt.xlabel(\"hidden dimension\")\r\nplt.ylabel(\"sequence length\")<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53ef\u4ee5\u770b\u5230\uff0c\u4f4d\u7f6e\u5d4c\u5165\u5728 \ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54 \ud835\udc51\ud835\udc56\ud835\udc5a\ud835\udc52\ud835\udc5b\ud835\udc60\ud835\udc56\ud835\udc5c\ud835\udc5b \uff08\u4e5f\u662fhidden dimension \uff09\u7ef4\u5ea6\u4e0a\u968f\u7740\u7ef4\u5ea6\u5e8f\u53f7\u589e\u5927\uff0c\u5468\u671f\u53d8\u5316\u4f1a\u8d8a\u6765\u8d8a\u6162\uff0c\u800c\u4ea7\u751f\u4e00\u79cd\u5305\u542b\u4f4d\u7f6e\u4fe1\u606f\u7684\u7eb9\u7406\u3002\n\n\u5c31\u8fd9\u6837\uff0c\u4ea7\u751f\u72ec\u4e00\u7684\u7eb9\u7406\u4f4d\u7f6e\u4fe1\u606f\uff0c\u6a21\u578b\u4ece\u800c\u5b66\u5230\u4f4d\u7f6e\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u81ea\u7136\u8bed\u8a00\u7684\u65f6\u5e8f\u7279\u6027\u3002\n<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-212.png\" alt=\"\" class=\"wp-image-1996\" width=\"387\" height=\"393\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-212.png 871w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-212-295x300.png 295w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-212-768x782.png 768w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><\/figure><\/div>\n\n\n\n<p>\u6700\u540e\uff0c\u5c06Xembedding\u00a0\u00a0\u548c\u00a0<code>\u4f4d\u7f6e\u5d4c\u5165<\/code>\u00a0\u76f8\u52a0\uff0c\u9001\u7ed9\u4e0b\u4e00\u5c42\u3002<\/p>\n\n\n\n<h3>2\u3001\u81ea\u6ce8\u610f\u529b\u5c42\uff08\ud835\udc60\ud835\udc52\ud835\udc59\ud835\udc53 \ud835\udc4e\ud835\udc61\ud835\udc61\ud835\udc52\ud835\udc5b\ud835\udc61\ud835\udc56\ud835\udc5c\ud835\udc5b \ud835\udc5a\ud835\udc52\ud835\udc50\u210e\ud835\udc4e\ud835\udc5b\ud835\udc56\ud835\udc60\ud835\udc5a\uff09<\/h3>\n\n\n\n<p>\u76f4\u63a5\u770b\u4e0b\u56fe\u7b14\u8bb0\uff0c\u8bb2\u89e3\u7684\u975e\u5e38\u8be6\u7ec6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"852\" height=\"1024\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/281a5b914d85d455a5a5dc3fc655c1dc-852x1024.png\" alt=\"\" class=\"wp-image-2000\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/281a5b914d85d455a5a5dc3fc655c1dc-852x1024.png 852w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/281a5b914d85d455a5a5dc3fc655c1dc-250x300.png 250w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/281a5b914d85d455a5a5dc3fc655c1dc-768x923.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/281a5b914d85d455a5a5dc3fc655c1dc.png 1000w\" sizes=\"(max-width: 852px) 100vw, 852px\" \/><\/figure>\n\n\n\n<p>\u591a\u5934\u7684\u610f\u4e49\u5728\u4e8e\uff0c\\(QK^{T}\\)\u00a0\u5f97\u5230\u7684\u77e9\u9635\u5c31\u53eb\u6ce8\u610f\u529b\u77e9\u9635\uff0c\u5b83\u53ef\u4ee5\u8868\u793a\u6bcf\u4e2a\u5b57\u4e0e\u5176\u4ed6\u5b57\u7684\u76f8\u4f3c\u7a0b\u5ea6\u3002\u56e0\u4e3a\uff0c\u5411\u91cf\u7684\u70b9\u79ef\u503c\u8d8a\u5927\uff0c\u8bf4\u660e\u4e24\u4e2a\u5411\u91cf\u8d8a\u63a5\u8fd1\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"801\" height=\"1024\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/4edabcd1931f8fb40360d410758a55ce-801x1024.png\" alt=\"\" class=\"wp-image-2002\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/4edabcd1931f8fb40360d410758a55ce-801x1024.png 801w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/4edabcd1931f8fb40360d410758a55ce-235x300.png 235w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/4edabcd1931f8fb40360d410758a55ce-768x982.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/4edabcd1931f8fb40360d410758a55ce.png 1000w\" sizes=\"(max-width: 801px) 100vw, 801px\" \/><\/figure>\n\n\n\n<p>\u6211\u4eec\u7684\u76ee\u7684\u662f\uff0c\u8ba9\u6bcf\u4e2a\u5b57\u90fd\u542b\u6709\u5f53\u524d\u8fd9\u4e2a\u53e5\u5b50\u4e2d\u7684\u6240\u6709\u5b57\u7684\u4fe1\u606f\uff0c\u7528\u6ce8\u610f\u529b\u5c42\uff0c\u6211\u4eec\u505a\u5230\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u662f<\/strong>\uff0c\u5728\u4e0a\u9762&nbsp;<code>\ud835\udc60\ud835\udc52\ud835\udc59\ud835\udc53 \ud835\udc4e\ud835\udc61\ud835\udc61\ud835\udc52\ud835\udc5b\ud835\udc61\ud835\udc56\ud835\udc5c\ud835\udc5b<\/code>&nbsp;\u7684\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528&nbsp;<code>\ud835\udc5a\ud835\udc56\ud835\udc5b\ud835\udc56 \ud835\udc4f\ud835\udc4e\ud835\udc61\ud835\udc50\u210e<\/code>\uff0c\u4e5f\u5c31\u662f\u4e00\u6b21\u8ba1\u7b97\u591a\u53e5\u8bdd\uff0c\u4e0a\u6587\u4e3e\u4f8b\u53ea\u7528\u4e86\u4e00\u4e2a\u53e5\u5b50\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u53e5\u5b50\u7684\u957f\u5ea6\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u9700\u8981\u6309\u7167\u6700\u957f\u7684\u53e5\u5b50\u7684\u957f\u5ea6\u7edf\u4e00\u5904\u7406\u3002\u5bf9\u4e8e\u77ed\u7684\u53e5\u5b50\uff0c\u8fdb\u884c\u00a0<code>Padding<\/code>\u00a0\u64cd\u4f5c\uff0c\u4e00\u822c\u6211\u4eec\u7528\u00a0<code>0<\/code>\u00a0\u6765\u8fdb\u884c\u586b\u5145\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"1000\" height=\"284\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/42e99d42f1010db6b4fad2d52a20b8aa.png\" alt=\"\" class=\"wp-image-2004\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/42e99d42f1010db6b4fad2d52a20b8aa.png 1000w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/42e99d42f1010db6b4fad2d52a20b8aa-300x85.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/42e99d42f1010db6b4fad2d52a20b8aa-768x218.png 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<h3>3\u3001\u6b8b\u5dee\u94fe\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/h3>\n\n\n\n<p>\u52a0\u5165\u4e86\u6b8b\u5dee\u8bbe\u8ba1\u548c\u5c42\u5f52\u4e00\u5316\u64cd\u4f5c\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u9632\u6b62\u68af\u5ea6\u6d88\u5931\uff0c\u52a0\u5feb\u6536\u655b\u3002<\/p>\n\n\n\n<h4>1) \u6b8b\u5dee\u8bbe\u8ba1<\/h4>\n\n\n\n<p>\u6211\u4eec\u5728\u4e0a\u4e00\u6b65\u5f97\u5230\u4e86\u7ecf\u8fc7\u6ce8\u610f\u529b\u77e9\u9635\u52a0\u6743\u4e4b\u540e\u7684&nbsp;<code>\ud835\udc49<\/code>\uff0c \u4e5f\u5c31\u662f&nbsp;<code>\ud835\udc34\ud835\udc61\ud835\udc61\ud835\udc52\ud835\udc5b\ud835\udc61\ud835\udc56\ud835\udc5c\ud835\udc5b(\ud835\udc44, \ud835\udc3e, \ud835\udc49)<\/code>\uff0c\u6211\u4eec\u5bf9\u5b83\u8fdb\u884c\u4e00\u4e0b\u8f6c\u7f6e\uff0c\u4f7f\u5176\u548c&nbsp;<code>\ud835\udc4b\ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54<\/code>&nbsp;\u7684\u7ef4\u5ea6\u4e00\u81f4, \u4e5f\u5c31\u662f&nbsp;<code>[\ud835\udc4f\ud835\udc4e\ud835\udc61\ud835\udc50\u210e \ud835\udc60\ud835\udc56\ud835\udc67\ud835\udc52, \ud835\udc60\ud835\udc52\ud835\udc5e\ud835\udc62\ud835\udc52\ud835\udc5b\ud835\udc50\ud835\udc52 \ud835\udc59\ud835\udc52\ud835\udc5b\ud835\udc54\ud835\udc61\u210e, \ud835\udc52\ud835\udc5a\ud835\udc4f\ud835\udc52\ud835\udc51\ud835\udc51\ud835\udc56\ud835\udc5b\ud835\udc54 \ud835\udc51\ud835\udc56\ud835\udc5a\ud835\udc52\ud835\udc5b\ud835\udc60\ud835\udc56\ud835\udc5c\ud835\udc5b]<\/code>&nbsp;\uff0c\u7136\u540e\u628a\u4ed6\u4eec\u52a0\u8d77\u6765\u505a\u6b8b\u5dee\u8fde\u63a5\uff0c\u76f4\u63a5\u8fdb\u884c\u5143\u7d20\u76f8\u52a0\uff0c\u56e0\u4e3a\u4ed6\u4eec\u7684\u7ef4\u5ea6\u4e00\u81f4:<\/p>\n\n\n\n<p>$$<br>X_{\\text {embedding }}+\\text { Attention }(Q, K, V)<br>$$<\/p>\n\n\n\n<p>\u5728\u4e4b\u540e\u7684\u8fd0\u7b97\u91cc\uff0c\u6bcf\u7ecf\u8fc7\u4e00\u4e2a\u6a21\u5757\u7684\u8fd0\u7b97\uff0c\u90fd\u8981\u628a\u8fd0\u7b97\u4e4b\u524d\u7684\u503c\u548c\u8fd0\u7b97\u4e4b\u540e\u7684\u503c\u76f8\u52a0\uff0c\u4ece\u800c\u5f97\u5230\u6b8b\u5dee\u8fde\u63a5\uff0c\u8bad\u7ec3\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u68af\u5ea6\u76f4\u63a5\u8d70\u6377\u5f84\u53cd\u4f20\u5230\u6700\u521d\u59cb\u5c42\uff1a<\/p>\n\n\n\n<p>$$<br>X+\\text { SubLayer }(X)<br>$$<\/p>\n\n\n\n<h4>2) \u5c42\u5f52\u4e00\u5316<\/h4>\n\n\n\n<p>\u4f5c\u7528\u662f\u628a\u795e\u7ecf\u7f51\u7edc\u4e2d\u9690\u85cf\u5c42\u5f52\u4e00\u4e3a\u6807\u51c6\u6b63\u6001\u5206\u5e03\uff0c\u4e5f\u5c31\u662f&nbsp;<code>\ud835\udc56.\ud835\udc56.\ud835\udc51<\/code>&nbsp;\u72ec\u7acb\u540c\u5206\u5e03\uff0c \u4ee5\u8d77\u5230\u52a0\u5feb\u8bad\u7ec3\u901f\u5ea6\uff0c \u52a0\u901f\u6536\u655b\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>$$<br>\\mu_{i}=\\frac{1}{m} \\sum_{i=1}^{m} x_{i j}<br>$$<\/p>\n\n\n\n<p>\u4e0a\u5f0f\u4e2d\u4ee5\u77e9\u9635\u7684\u884c (\ud835\udc5f\ud835\udc5c\ud835\udc64) \u4e3a\u5355\u4f4d\u6c42\u5747\u503c\uff1a<\/p>\n\n\n\n<p>$$<br>\\sigma_{j}^{2}=\\frac{1}{m} \\sum_{i=1}^{m}\\left(x_{i j}-\\mu_{j}\\right)^{2}<br>$$<\/p>\n\n\n\n<p>\u4e0a\u5f0f\u4e2d\u4ee5\u77e9\u9635\u7684\u884c (\ud835\udc5f\ud835\udc5c\ud835\udc64) \u4e3a\u5355\u4f4d\u6c42\u65b9\u5dee\uff1a<\/p>\n\n\n\n<p>$$<br>\\operatorname{LayerNorm}(x)=\\alpha \\odot \\frac{x_{i j}-\\mu_{i}}{\\sqrt{\\sigma_{i}^{2}+\\epsilon}}+\\beta<br>$$<\/p>\n\n\n\n<p>\u7136\u540e\u7528\u6bcf\u4e00\u884c\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u51cf\u53bb\u8fd9\u884c\u7684\u5747\u503c\uff0c\u518d\u9664\u4ee5\u8fd9\u884c\u7684\u6807\u51c6\u5dee\uff0c\u4ece\u800c\u5f97\u5230\u5f52 \u4e00\u5316\u540e\u7684\u6570\u503c\uff0c \\(\\epsilon\\) \u662f\u4e3a\u4e86\u9632\u6b62\u9664 0 \uff1b\u4e4b\u540e\u5f15\u5165\u4e24\u4e2a\u53ef\u8bad\u7ec3\u53c2\u6570 \\(\\alpha, \\beta\\) \u6765\u5f25\u8865\u5f52\u4e00\u5316\u7684\u8fc7\u7a0b\u4e2d\u635f\u5931\u6389\u7684\u4fe1\u606f\uff0c\u6ce8\u610f \\(\\odot\\) \u8868\u793a \u5143\u7d20\u76f8\u4e58\u800c\u4e0d\u662f\u70b9\u79ef\uff0c\u6211\u4eec\u4e00\u822c\u521d\u59cb\u5316 \\(\\alpha\\) \u4e3a\u5168 1 \uff0c\u800c\\(\\beta\\) \u4e3a\u5168 0 \u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5c42\u9762\u975e\u5e38\u7b80\u5355\uff0c\u5355\u5934\u00a0<code>attention<\/code>\u00a0\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ScaledDotProductAttention(nn.Module):\r\n    ''' Scaled Dot-Product Attention '''\r\n\r\n    def __init__(self, temperature, attn_dropout=0.1):\r\n        super().__init__()\r\n        self.temperature = temperature\r\n        self.dropout = nn.Dropout(attn_dropout)\r\n\r\n    def forward(self, q, k, v, mask=None):\r\n        # self.temperature\u662f\u8bba\u6587\u4e2d\u7684d_k ** 0.5\uff0c\u9632\u6b62\u68af\u5ea6\u8fc7\u5927\r\n        # QxK\/sqrt(dk)\r\n        attn = torch.matmul(q \/ self.temperature, k.transpose(2, 3))\r\n\r\n        if mask is not None:\r\n            # \u5c4f\u853d\u4e0d\u60f3\u8981\u7684\u8f93\u51fa\r\n            attn = attn.masked_fill(mask == 0, -1e9)\r\n        # softmax+dropout\r\n        attn = self.dropout(F.softmax(attn, dim=-1))\r\n        # \u6982\u7387\u5206\u5e03xV\r\n        output = torch.matmul(attn, v)\r\n\r\n        return output, attn<\/code><\/pre>\n\n\n\n<p><code>Multi-Head Attention<\/code>\u00a0\u5b9e\u73b0\u5728\u00a0<code>ScaledDotProductAttention<\/code>\u00a0\u57fa\u7840\u4e0a\u6784\u5efa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MultiHeadAttention(nn.Module):\r\n    ''' Multi-Head Attention module '''\r\n\r\n    # n_head\u5934\u7684\u4e2a\u6570\uff0c\u9ed8\u8ba4\u662f8\r\n    # d_model\u7f16\u7801\u5411\u91cf\u957f\u5ea6\uff0c\u4f8b\u5982\u672c\u6587\u8bf4\u7684512\r\n    # d_k, d_v\u7684\u503c\u4e00\u822c\u4f1a\u8bbe\u7f6e\u4e3a n_head * d_k=d_model\uff0c\r\n    # \u6b64\u65f6concat\u540e\u6b63\u597d\u548c\u539f\u59cb\u8f93\u5165\u4e00\u6837\uff0c\u5f53\u7136\u4e0d\u76f8\u540c\u4e5f\u53ef\u4ee5\uff0c\u56e0\u4e3a\u540e\u9762\u6709fc\u5c42\r\n    # \u76f8\u5f53\u4e8e\u5c06\u53ef\u5b66\u4e60\u77e9\u9635\u5206\u6210\u72ec\u7acb\u7684n_head\u4efd\r\n    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):\r\n        super().__init__()\r\n        # \u5047\u8bben_head=8\uff0cd_k=64\r\n        self.n_head = n_head\r\n        self.d_k = d_k\r\n        self.d_v = d_v\r\n        # d_model\u8f93\u5165\u5411\u91cf\uff0cn_head * d_k\u8f93\u51fa\u5411\u91cf\r\n        # \u53ef\u5b66\u4e60W^Q\uff0cW^K,W^V\u77e9\u9635\u53c2\u6570\u521d\u59cb\u5316\r\n        self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False)\r\n        self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False)\r\n        self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False)\r\n        # \u6700\u540e\u7684\u8f93\u51fa\u7ef4\u5ea6\u53d8\u6362\u64cd\u4f5c\r\n        self.fc = nn.Linear(n_head * d_v, d_model, bias=False)\r\n        # \u5355\u5934\u81ea\u6ce8\u610f\u529b\r\n        self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5)\r\n        self.dropout = nn.Dropout(dropout)\r\n        # \u5c42\u5f52\u4e00\u5316\r\n        self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)\r\n\r\n    def forward(self, q, k, v, mask=None):\r\n        # \u5047\u8bbeqkv\u8f93\u5165\u662f(b,100,512),100\u662f\u8bad\u7ec3\u6bcf\u4e2a\u6837\u672c\u6700\u5927\u5355\u8bcd\u4e2a\u6570\r\n        # \u4e00\u822cqkv\u76f8\u7b49\uff0c\u5373\u81ea\u6ce8\u610f\u529b\r\n        residual = q\r\n        # \u5c06\u8f93\u5165x\u548c\u53ef\u5b66\u4e60\u77e9\u9635\u76f8\u4e58\uff0c\u5f97\u5230(b,100,512)\u8f93\u51fa\r\n        # \u5176\u4e2d512\u7684\u542b\u4e49\u5176\u5b9e\u662f8x64\uff0c8\u4e2ahead\uff0c\u6bcf\u4e2ahead\u7684\u53ef\u5b66\u4e60\u77e9\u9635\u4e3a64\u7ef4\u5ea6\r\n        # q\u7684\u8f93\u51fa\u662f(b,100,8,64),kv\u4e5f\u662f\u4e00\u6837\r\n        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)\r\n        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)\r\n        v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)\r\n\r\n        # \u53d8\u6210(b,8,100,64)\uff0c\u65b9\u4fbf\u540e\u9762\u8ba1\u7b97\uff0c\u4e5f\u5c31\u662f8\u4e2a\u5934\u5355\u72ec\u8ba1\u7b97\r\n        q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)\r\n\r\n        if mask is not None:\r\n            mask = mask.unsqueeze(1)   # For head axis broadcasting.\r\n        # \u8f93\u51faq\u662f(b,8,100,64),\u7ef4\u6301\u4e0d\u53d8,\u5185\u90e8\u8ba1\u7b97\u6d41\u7a0b\u662f\uff1a\r\n        # q*k\u8f6c\u7f6e\uff0c\u9664\u4ee5d_k ** 0.5\uff0c\u8f93\u51fa\u7ef4\u5ea6\u662fb,8,100,100\u5373\u5355\u8bcd\u548c\u5355\u8bcd\u76f4\u63a5\u7684\u76f8\u4f3c\u6027\r\n        # \u5bf9\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u8fdb\u884csoftmax\u64cd\u4f5c\u5f97\u5230b,8,100,100\r\n        # \u6700\u540e\u4e58\u4e0aV\uff0c\u5f97\u5230b,8,100,64\u8f93\u51fa\r\n        q, attn = self.attention(q, k, v, mask=mask)\r\n\r\n        # b,100,8,64-->b,100,512\r\n        q = q.transpose(1, 2).contiguous().view(sz_b, len_q, -1)\r\n        q = self.dropout(self.fc(q))\r\n        # \u6b8b\u5dee\u8ba1\u7b97\r\n        q += residual\r\n        # \u5c42\u5f52\u4e00\u5316\uff0c\u5728512\u7ef4\u5ea6\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\uff0c\u8fdb\u884c\u5c42\u5f52\u4e00\u5316\r\n        q = self.layer_norm(q)\r\n\r\n        return q, attn<\/code><\/pre>\n\n\n\n<h3>4\u3001\u524d\u9988\u7f51\u7edc<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u5c42\u5c31\u6ca1\u5565\u8bf4\u7684\u4e86\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u770b\u4ee3\u7801\u5427\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PositionwiseFeedForward(nn.Module):\r\n    ''' A two-feed-forward-layer module '''\r\n\r\n    def __init__(self, d_in, d_hid, dropout=0.1):\r\n        super().__init__()\r\n        # \u4e24\u4e2afc\u5c42\uff0c\u5bf9\u6700\u540e\u7684512\u7ef4\u5ea6\u8fdb\u884c\u53d8\u6362\r\n        self.w_1 = nn.Linear(d_in, d_hid) # position-wise\r\n        self.w_2 = nn.Linear(d_hid, d_in) # position-wise\r\n        self.layer_norm = nn.LayerNorm(d_in, eps=1e-6)\r\n        self.dropout = nn.Dropout(dropout)\r\n\r\n    def forward(self, x):\r\n        residual = x\r\n\r\n        x = self.w_2(F.relu(self.w_1(x)))\r\n        x = self.dropout(x)\r\n        x += residual\r\n\r\n        x = self.layer_norm(x)\r\n\r\n        return x<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea Jack Cui \u9a6c\u8d5b\u514b\uff0c\u514b\u661f\uff0c\u771f\u6765\u4e86\uff01 \u4f55\u607a\u660e\u5927\u795e\u7684\u65b0\u4f5c \u8bba\u6587\uff1ahttps:\/\/arxiv.org\/a &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/13\/maemasked-autoencoders-are-scalable-vision-learners\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">MAE:Masked Autoencoders Are Scalable Vision Learners<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,12,9],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1972"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1972"}],"version-history":[{"count":23,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1972\/revisions"}],"predecessor-version":[{"id":2025,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1972\/revisions\/2025"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1972"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}