{"id":8745,"date":"2022-10-04T10:03:22","date_gmt":"2022-10-04T02:03:22","guid":{"rendered":"http:\/\/139.9.1.231\/?p=8745"},"modified":"2022-10-04T10:03:23","modified_gmt":"2022-10-04T02:03:23","slug":"torch-roll","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/10\/04\/torch-roll\/","title":{"rendered":"torch.roll \u51fd\u6570"},"content":{"rendered":"\n<p>The Question about the mask of window attention\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/microsoft\/Swin-Transformer\/issues\/38\">https:\/\/github.com\/microsoft\/Swin-Transformer\/issues\/38<\/a><\/p>\n\n\n\n<p><code>torch.roll<\/code>(<em>input<\/em>,\u00a0<em>shifts<\/em>,\u00a0<em>dims=None<\/em>)\u00a0\u2192\u00a0<a href=\"https:\/\/pytorch.org\/docs\/stable\/tensors.html#torch.Tensor\">Tensor<\/a><\/p>\n\n\n\n<p>Roll the tensor&nbsp;<code>input<\/code>&nbsp;along the given dimension(s). Elements that are shifted beyond the last position are re-introduced at the first position. If&nbsp;<code>dims<\/code>&nbsp;is&nbsp;None, the tensor will be flattened before rolling and then restored to the original shape.Parameters<\/p>\n\n\n\n<ul><li><strong>input<\/strong>\u00a0(<a href=\"https:\/\/pytorch.org\/docs\/stable\/tensors.html#torch.Tensor\"><em>Tensor<\/em><\/a>) \u2013 the input tensor.<\/li><li><strong>shifts<\/strong>\u00a0(<a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#int\"><em>int<\/em><\/a><em>\u00a0or\u00a0tuple of python:ints<\/em>) \u2013 The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value<\/li><li><strong>dims<\/strong>\u00a0(<a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#int\"><em>int<\/em><\/a><em>\u00a0or\u00a0tuple of python:ints<\/em>) \u2013 Axis along which to roll<\/li><\/ul>\n\n\n\n<p>\u6cbf\u7ed9\u5b9a\u7ef4\u6570\u6eda\u52a8\u5f20\u91cf\uff0c\u79fb\u52a8\u5230\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\u4ee5\u5916\u7684\u5143\u7d20\u5c06\u5728\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u91cd\u65b0\u5f15\u5165\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5c3a\u5bf8\uff0c\u5f20\u91cf\u5c06\u5728\u8f67\u5236\u524d\u88ab\u538b\u5e73\uff0c\u7136\u540e\u6062\u590d\u5230\u539f\u59cb\u5f62\u72b6\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u7406\u89e3\uff1ashifts\u7684\u503c\u4e3a\u6b63\u6570\u76f8\u5f53\u4e8e\u5411\u4e0b\u6324\u7259\u818f\uff0c\u6324\u51fa\u7684\u7259\u818f\u53c8\u4ece\u9876\u90e8\u585e\u56de\u7259\u818f\u91cc\u9762\uff1bshifts\u7684\u503c\u4e3a\u8d1f\u6570\u76f8\u5f53\u4e8e\u5411\u4e0a\u6324\u7259\u818f\uff0c\u6324\u51fa\u7684\u7259\u818f\u53c8\u4ece\u5e95\u90e8\u585e\u56de\u7259\u818f\u91cc\u9762<\/p>\n\n\n\n<ul><li>input (Tensor) \u2014\u2014 \u8f93\u5165\u5f20\u91cf\u3002<\/li><li>shifts (python:int \u6216 tuple of python:int) \u2014\u2014 \u5f20\u91cf\u5143\u7d20\u79fb\u4f4d\u7684\u4f4d\u6570\u3002\u5982\u679c\u8be5\u53c2\u6570\u662f\u4e00\u4e2a\u5143\u7ec4\uff08\u4f8b\u5982shifts=(x,y)\uff09\uff0cdims\u5fc5\u987b\u662f\u4e00\u4e2a\u76f8\u540c\u5927\u5c0f\u7684\u5143\u7ec4\uff08\u4f8b\u5982dims=(a,b)\uff09\uff0c\u76f8\u5f53\u4e8e\u5728\u7b2ca\u7ef4\u5ea6\u79fbx\u4f4d\uff0c\u5728b\u7ef4\u5ea6\u79fby\u4f4d<\/li><li>dims (int \u6216 tuple of python:int) \u786e\u5b9a\u7684\u7ef4\u5ea6\u3002<\/li><\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre id=\"codecell0\" class=\"wp-block-preformatted\">>>> <strong>x<\/strong> <strong>=<\/strong> <strong>torch.tensor([<\/strong>1<strong>,<\/strong> 2<strong>,<\/strong> 3<strong>,<\/strong> 4<strong>,<\/strong> 5<strong>,<\/strong> 6<strong>,<\/strong> 7<strong>,<\/strong> 8<strong>]).view(<\/strong>4<strong>,<\/strong> 2<strong>)<\/strong>\n>>> <strong>x<\/strong>\ntensor([[1, 2],\n        [3, 4],\n        [5, 6],\n        [7, 8]])\n>>> <strong>torch.roll(x,<\/strong> 1<strong>)<\/strong>\ntensor([[8, 1],\n        [2, 3],\n        [4, 5],\n        [6, 7]])\n\n'''\u7b2c0\u7ef4\u5ea6\u5411\u4e0b\u79fb1\u4f4d\uff0c\u591a\u51fa\u7684[7,8]\u8865\u5145\u5230\u9876\u90e8'''\n>>> <strong>torch.roll(x,<\/strong> 1<strong>,<\/strong> 0<strong>)<\/strong>\ntensor([[7, 8],\n        [1, 2],\n        [3, 4],\n        [5, 6]])\n\n'''\u7b2c0\u7ef4\u5ea6\u5411\u4e0a\u79fb1\u4f4d\uff0c\u591a\u51fa\u7684[1,2]\u8865\u5145\u5230\u5e95\u90e8'''\n>>> <strong>torch.roll(x,<\/strong> <strong>-<\/strong>1<strong>,<\/strong> 0<strong>)<\/strong>\ntensor([[3, 4],\n        [5, 6],\n        [7, 8],\n        [1, 2]])\n\n'''tuple\u5143\u7956,\u7ef4\u5ea6\u4e00\u4e00\u5bf9\u5e94\uff1a\r\n\u7b2c0\u7ef4\u5ea6\u5411\u4e0b\u79fb2\u4f4d\uff0c\u591a\u51fa\u7684[5,6][7,8]\u8865\u5145\u5230\u9876\u90e8\uff0c\r\n\u7b2c1\u7ef4\u5411\u53f3\u79fb1\u4f4d\uff0c\u591a\u51fa\u7684[6,8,2,4]\u8865\u5145\u5230\u6700\u5de6\u8fb9'''\n>>> <strong>torch.roll(x,<\/strong> <strong>shifts=(<\/strong>2<strong>,<\/strong> 1<strong>),<\/strong> <strong>dims=(<\/strong>0<strong>,<\/strong> 1<strong>))<\/strong>\ntensor([[6, 5],\n        [8, 7],\n        [2, 1],\n        [4, 3]])\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Question about the mask of window attention\uff1a https: &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/10\/04\/torch-roll\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">torch.roll \u51fd\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8745"}],"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=8745"}],"version-history":[{"count":13,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8745\/revisions"}],"predecessor-version":[{"id":8758,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8745\/revisions\/8758"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=8745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=8745"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=8745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}