{"id":4703,"date":"2022-07-11T15:36:37","date_gmt":"2022-07-11T07:36:37","guid":{"rendered":"http:\/\/139.9.1.231\/?p=4703"},"modified":"2022-09-20T10:48:09","modified_gmt":"2022-09-20T02:48:09","slug":"patches-are-all-you-need","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/07\/11\/patches-are-all-you-need\/","title":{"rendered":"Patches Are All You Need?"},"content":{"rendered":"\n<p>                                                                                  &#8212;&#8212;&#8212;&#8211; ConvMixer \u7f51\u7edc<\/p>\n\n\n\n<p>\u8bba\u6587\u5730\u5740\uff1a<a href=\"https:\/\/openreview.net\/pdf?id=TVHS5Y4dNvM\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/openreview.net\/pdf?id=TVHS5Y4dNvM<\/a><br><a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Github&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Github<\/a>\u00a0\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/tmp-iclr\/convmixer\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/tmp-iclr\/convmixer<\/a><\/p>\n\n\n\n<p>ConvMixer is now integrated into the&nbsp;<a href=\"https:\/\/github.com\/rwightman\/pytorch-image-models\"><code>timm<\/code>&nbsp;framework itself<\/a>. You can see the PR&nbsp;<a href=\"https:\/\/github.com\/rwightman\/pytorch-image-models\/pull\/910\">here<\/a>.<\/p>\n\n\n\n<p>Conv Mixer \u8fd9\u7bc7\u6587\u7ae0\u63d0\u51fa\u7684\u521d\u8877\u662f\u60f3\u53bb\u5f04\u6e05\u695a\uff0cViT\u7cfb\u5217\u6a21\u578b\u8868\u73b0\u4f18\u8d8a\uff0c\u5230\u5e95\u662f\u56fe\u7247\u5206\u5757\u7684\u529f\u52b3 \u8fd8\u662f\u7f51\u7edc\u4e2dAttention\u7684\u529f\u52b3\u3002\u4e8e\u662f\u4f5c\u8005\u5c31\u6839\u636e\u6df1\u5ea6\u53ef\u5206\u79bb\u5377\u79ef\uff0c\u5728ViT \u548c&nbsp;<a href=\"https:\/\/blog.csdn.net\/qq_38676487\/article\/details\/118640096?spm=1001.2014.3001.5501\" target=\"_blank\" rel=\"noreferrer noopener\">MLP Mixer<\/a>&nbsp;\u7684\u542f\u53d1\u4e2d \u8bbe\u8ba1\u4e86Conv Mixer\u3002\u5e76\u4e14\u5728\u8868\u73b0\u4e0a\u8d85\u8d8a\u4e86\u4e00\u4e9bViT \uff08\u67d0\u4e9bViT\u7ed3\u6784\uff09\uff0cMLP Mixer \u548c ResNet\u3002\u6587\u7ae0\u672c\u8eab\u5e76\u6ca1\u53bb\u8ffd\u6c42\u6a21\u578b\u7684\u901f\u5ea6\uff0c\u548c\u8868\u73b0\u80fd\u529b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/img-blog.csdnimg.cn\/89d3ad297afa446b905903d1147c8d81.png?x-oss-process=image\/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAX1_kuI3mg7Plhpnku6PnoIFfXw==,size_8,color_FFFFFF,t_70,g_se,x_16\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"898\" height=\"358\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-3.png\" alt=\"\" class=\"wp-image-4708\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-3.png 898w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-3-300x120.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-3-768x306.png 768w\" sizes=\"(max-width: 898px) 100vw, 898px\" \/><figcaption>\u7f51\u7edc\u7ed3\u6784<\/figcaption><\/figure>\n\n\n\n<p>\u7f51\u7edc\u7ed3\u6784\u8be6\u89e3\uff1a<\/p>\n\n\n\n<p>1\u3001 Patch embedding<\/p>\n\n\n\n<p>\u8fd9\u91cc\u7684Patch embedding\u5b9e\u9645\u4e0a\u662f\u4f7f\u7528\u4e00\u4e2a\u5377\u79ef\u5c42 \u5b9e\u73b0\u7684<\/p>\n\n\n\n<p>nn.Conv2d(3,dim,kernel_size=patch_size,stride=patch_size)<\/p>\n\n\n\n<p>\u5176\u4e2d kernel_size \u5c31\u662fpatch\u7684\u5927\u5c0f<\/p>\n\n\n\n<p>2\u3001GELU\u6fc0\u6d3b\u51fd\u6570\uff08\u9ad8\u65af\u8bef\u5dee\u7ebf\u6027\u5355\u5143\uff09<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u662f\u6700\u8fd1\u5f88\u591a\u6a21\u578b\u90fd\u5728\u7528\u7684\u51fd\u6570\uff08dert\u3001\u9ad8\u65af\u8bef\u5dee\u7ebf\u6027\u5355\u5143\u6fc0\u6d3b\u51fd\u6570\u5728\u6700\u8fd1\u7684 Transformer \u6a21\u578b\uff09GELUs\u6b63\u662f\u5728\u6fc0\u6d3b\u4e2d\u5f15\u5165\u4e86\u968f\u673a\u6b63\u5219\u7684\u601d\u60f3\uff0c\u662f\u4e00\u79cd\u5bf9\u795e\u7ecf\u5143\u8f93\u5165\u7684\u6982\u7387\u63cf\u8ff0\uff0c\u76f4\u89c2\u4e0a\u66f4\u7b26\u5408\u81ea\u7136\u7684\u8ba4\u8bc6\uff0c\u540c\u65f6\u5b9e\u9a8c\u6548\u679c\u8981\u6bd4Relus\u4e0eELUs\u90fd\u8981\u597d\u3002<\/p>\n\n\n\n<p>GELUs\u5176\u5b9e\u662f dropout\u3001zoneout\u3001Relus\u7684\u7efc\u5408\uff0cGELUs\u5bf9\u4e8e\u8f93\u5165\u4e58\u4ee5\u4e00\u4e2a0,1\u7ec4\u6210\u7684mask\uff0c\u800c\u8be5mask\u7684\u751f\u6210\u5219\u662f\u4f9d\u6982\u7387\u968f\u673a\u7684\u4f9d\u8d56\u4e8e\u8f93\u5165\u3002\u5047\u8bbe\u8f93\u5165\u4e3aX, mask\u4e3am\uff0c\u5219m\u670d\u4ece\u4e00\u4e2a\u4f2f\u52aa\u5229\u5206\u5e03(\u03a6 ( x ) \\Phi(x)\u03a6(<em>x<\/em>),&nbsp;\u03a6 ( x ) = P ( X &lt; = x ) , X \u670d \u4ece \u6807 \u51c6 \u6b63 \u592a \u5206 \u5e03 \\Phi(x)=P(X&lt;=x), X\u670d\u4ece\u6807\u51c6\u6b63\u592a\u5206\u5e03\u03a6(<em>x<\/em>)=<em>P<\/em>(<em>X<\/em>&lt;=<em>x<\/em>),<em>X<\/em>\u670d\u4ece\u6807\u51c6\u6b63\u592a\u5206\u5e03)\uff0c\u8fd9\u4e48\u9009\u62e9\u662f\u56e0\u4e3a\u795e\u7ecf\u5143\u7684\u8f93\u5165\u8d8b\u5411\u4e8e\u6b63\u592a\u5206\u5e03\uff0c\u8fd9\u4e48\u8bbe\u5b9a\u4f7f\u5f97\u5f53\u8f93\u5165x\u51cf\u5c0f\u7684\u65f6\u5019\uff0c\u8f93\u5165\u4f1a\u6709\u4e00\u4e2a\u66f4\u9ad8\u7684\u6982\u7387\u88abdropout\u6389\uff0c\u8fd9\u6837\u7684\u6fc0\u6d3b\u53d8\u6362\u5c31\u4f1a\u968f\u673a\u4f9d\u8d56\u4e8e\u8f93\u5165\u4e86\u3002<\/p>\n\n\n\n<p>\u770b\u5f97\u51fa\u6765\uff0c\u8fd9\u5c31\u662f\u67d0\u4e9b\u51fd\u6570\uff08\u6bd4\u5982\u53cc\u66f2\u6b63\u5207\u51fd\u6570 tanh\uff09\u4e0e\u8fd1\u4f3c\u6570\u503c\u7684\u7ec4\u5408\u3002\u6ca1\u4ec0\u4e48\u8fc7\u591a\u53ef\u8bf4\u7684\u3002\u6709\u610f\u601d\u7684\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u56fe\u5f62\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/pic4.zhimg.com\/v2-149a8bc6ee90f65205a8d408f79a0017_r.jpg\" alt=\"\"\/><figcaption>GELU \u6fc0\u6d3b\u51fd\u6570\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53 x \u5927\u4e8e 0 \u65f6\uff0c\u8f93\u51fa\u4e3a x\uff1b\u4f46 x=0 \u5230 x=1 \u7684\u533a\u95f4\u9664\u5916\uff0c\u8fd9\u65f6\u66f2\u7ebf\u66f4\u504f\u5411\u4e8e y \u8f74\u3002<\/p>\n\n\n\n<p>\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u4f3c\u4e4e\u662f NLP \u9886\u57df\u7684\u5f53\u524d\u6700\u4f73\uff1b\u5c24\u5176\u5728 Transformer \u6a21\u578b\u4e2d\u8868\u73b0\u6700\u597d\uff1b<\/li><li>\u80fd\u907f\u514d\u68af\u5ea6\u6d88\u5931\u95ee\u9898\u3002<\/li><\/ul>\n\n\n\n<p>3\u3001ConvMixerLayer<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ConvMixerLayer(nn.Module):\n    def __init__(self,dim,kernel_size = 9):\n        super().__init__()\n        #\u6b8b\u5dee\u7ed3\u6784\n        self.Resnet =  nn.Sequential(\n            nn.Conv2d(dim,dim,kernel_size=kernel_size,groups=dim,padding='same'),\n            nn.GELU(),\n            nn.BatchNorm2d(dim)\n        )\n        #\u9010\u70b9\u5377\u79ef\n        self.Conv_1x1 = nn.Sequential(\n            nn.Conv2d(dim,dim,kernel_size=1),\n            nn.GELU(),\n            nn.BatchNorm2d(dim)\n        )\n    def forward(self,x):\n        x = x +self.Resnet(x)\n        x = self.Conv_1x1(x)\n        return <\/code><\/pre>\n\n\n\n<p>\u5728ConvMixer Layer \u4e2d, \u4f7f\u7528\u4e86\u6df1\u5ea6\u53ef\u5206\u79bb\u5377\u79ef\uff0cGELU \u6fc0\u6d3b\u51fd\u6570\uff0c\u9010\u70b9\u5377\u79ef\u3002<br><strong>\u8bba\u6587\u4e2d\u5c06\u56fe\u4e2d\u7ea2\u8272\u90e8 \u79f0\u4e3a \u201cchannel wise mixing\u201d \u84dd\u8272\u90e8\u5206\u79f0\u4e3a &#8220;spatial mixing&#8221;<\/strong><br><strong>\u8bba\u6587\u5f97\u5230\u7684\u7ed3\u8bba\u662f\u5f53\u6df1\u5ea6\u53ef\u5206\u79bb\u5377\u79ef\u90e8\u5206\u7684\u5377\u79ef\u6838\u8d8a\u5927\uff0c\u6a21\u578b\u7684\u6027\u80fd\u8d8a\u597d\u3002<\/strong>\u6587\u7ae0\u4e2d\u7684\u4f7f\u7528\u7684\u662f9&#215;9\u7684\u5377\u79ef\u6838\uff0c<strong>\u56e0\u4e3a\u5377\u79ef\u6838\u8d8a\u5927\u8868\u73b0\u8d8a\u597d<\/strong>\u3002<\/p>\n\n\n\n<p>\u6587\u7ae0\u6700\u540e\u4e5f\u8ba4\u4e3a\uff0cViT \u8868\u73b0\u5982\u6b64\u4f18\u8d8a \u662f\u56e0\u4e3apatch embedding \uff08\u56fe\u7247\u5206\u5757\uff09\u7684\u539f\u56e0\u3002<br><strong>\u4f5c\u8005\u8ba4\u4e3a patch embedding \u64cd\u4f5c\u5c31\u80fd\u5b8c\u6210\u795e\u7ecf\u7f51\u7edc\u7684\u6240\u6709\u4e0b\u91c7\u6837\u8fc7\u7a0b\uff0c\u964d\u4f4e\u4e86\u56fe\u7247\u7684\u5206\u8fa8\u7387\uff0c\u589e\u52a0\u4e86\u611f\u53d7\u91ce\uff0c\u66f4\u5bb9\u6613\u627e\u5230\u8fdc\u5904\u7684\u7a7a\u95f4\u4fe1\u606f\u3002\u4ece\u800c\u6a21\u578b\u8868\u73b0\u826f\u597d<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8212;&#8212;&#8212;&#8211; ConvMixer \u7f51\u7edc \u8bba\u6587\u5730\u5740\uff1ahttps:\/\/ &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/07\/11\/patches-are-all-you-need\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Patches Are All You Need?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4703"}],"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=4703"}],"version-history":[{"count":15,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4703\/revisions"}],"predecessor-version":[{"id":7952,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4703\/revisions\/7952"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=4703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=4703"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=4703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}