{"id":63,"date":"2024-01-08T16:08:11","date_gmt":"2024-01-08T08:08:11","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=63"},"modified":"2024-01-08T16:08:11","modified_gmt":"2024-01-08T08:08:11","slug":"pytorch%e4%b8%ad%e7%9a%84%e5%bc%a0%e9%87%8f%e5%ad%98%e5%82%a8%e6%96%b9%e5%bc%8f%e4%bb%a5%e5%8f%8aview%e5%92%8creshape%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2024\/01\/08\/pytorch%e4%b8%ad%e7%9a%84%e5%bc%a0%e9%87%8f%e5%ad%98%e5%82%a8%e6%96%b9%e5%bc%8f%e4%bb%a5%e5%8f%8aview%e5%92%8creshape%e6%93%8d%e4%bd%9c\/","title":{"rendered":"PyTorch\u4e2d\u7684\u5f20\u91cf\u5b58\u50a8\u65b9\u5f0f\u4ee5\u53caview()\u548creshape()\u64cd\u4f5c"},"content":{"rendered":"<h3>1. PyTorch\u5f20\u91cf\u7684\u5e95\u5c42\u5b58\u50a8\u65b9\u5f0f<\/h3>\n<p>\u5728PyTorch\u4e2d\uff0c\u5f20\u91cf\u7684\u5934\u4fe1\u606f(\u5305\u62ec\u5f20\u91cf\u540d\u3001\u5f62\u72b6\u7b49\u4fe1\u606f)\u548c\u5b9e\u9645\u7684\u6570\u636e\u662f\u5206\u5f00\u5b58\u50a8\u7684\u3002\u5728\u6570\u636e\u533a\uff0c\u65e0\u8bba\u5f20\u91cf\u7684\u7ef4\u5ea6\u3001\u5f62\u72b6\u5982\u4f55\uff0c\u90fd\u662f\u6309\u7167\u4e00\u7ef4\u3001\u8fde\u7eed\u7684\u65b9\u5f0f\u8fdb\u884c\u5b58\u50a8\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f20\u91cf\u7684<code>storage()<\/code>\u6765\u67e5\u770b\u5f20\u91cf\u7684\u6570\u636e\u533a\uff1a<\/p>\n<pre><code class=\"language-python\">import torch\na = torch.arange(9).view(3, 3)\nprint(f&quot;storage of a: {a.storage()}&quot;)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/tensor.storage1.png\" alt=\"\" \/><\/p>\n<h4>1.1 \u5f20\u91cf\u7684size\u5c5e\u6027\u548cstride\u5c5e\u6027<\/h4>\n<p>\u5f20\u91cf\u7684<code>size<\/code>\u6307\u7684\u662f\u5f20\u91cf\u6bcf\u4e00\u7ef4\u5ea6\u7684\u5927\u5c0f\uff0c\u5f20\u91cf\u7684\u6b65\u957f\u5c5e\u6027<code>stride<\/code>\u53ef\u4ee5\u7406\u89e3\u4e3a<strong>\u4ece\u7d22\u5f15\u4e2d\u7684\u4e00\u4e2a\u7ef4\u5ea6\u8de8\u5230\u4e0b\u4e00\u4e2a\u7ef4\u5ea6\u4e2d\u95f4\u7684\u8de8\u5ea6<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/tensor_stride.png\" alt=\"\" \/><\/p>\n<p>\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u7ef4\u5ea6(\u5373\u884c)\uff0c\u4ece\u4e0a\u4e00\u884c\u8de8\u8d8a\u5230\u4e0b\u4e00\u884c\u9700\u8981\u8de8\u8d8a\u4e09\u4e2a\u5143\u7d20\uff0c\u800c\u7b2c\u4e8c\u4e2a\u7ef4\u5ea6(\u5373\u5217)\uff0c\u4ece\u524d\u4e00\u5217\u8de8\u8d8a\u5230(\u540c\u4e00\u884c\u7684)\u4e0b\u4e00\u5217\u53ea\u9700\u8de8\u8d8a\u4e00\u4e2a\u5143\u7d20\uff0c\u6545<code>a<\/code>\u7684<code>stride<\/code>\u4e3a<code>(3,1)<\/code><\/p>\n<h4>1.2 \u8fde\u7eed\u6027\u6761\u4ef6<\/h4>\n<p>\u5f20\u91cf\u662f\u8fde\u7eed\u7684\u662f\u6307\u5f20\u91cf\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<p><code>stride[i] = stride[i + 1] * size[i + 1]<\/code><\/p>\n<h3>2. view()\u548creshape()<\/h3>\n<p>\u5f20\u91cf\u7684<code>view()<\/code>\u548c`reshape()\u7684\u4f5c\u7528\u90fd\u662f\u5c06\u5f20\u91cf\u8f6c\u6362\u4e3a\u6307\u5b9a\u5f62\u72b6\uff0c\u7136\u800c\uff0cview()\u8981\u6c42\u5f20\u91cf\u5728\u88c5\u6362\u524d\u540e\u6ee1\u8db3\u8fde\u7eed\u578b\u6761\u4ef6<\/p>\n<pre><code class=\"language-python\">b = a.permute(1, 0) # b\u662fa\u7684\u8f6c\u7f6e\uff0c\u6b64\u65f6a\u548cb\u5171\u4eab\u5b58\u50a8\u533a\nprint(f&quot;size of a: {a.size()}, size of b: {b.size()}&quot;)\nprint(f&quot;stride of a: {a.stride()}, stride of b: {b.stride()}&quot;)    # b\u4e0d\u6ee1\u8db3\u8fde\u7eed\u578b\u6761\u4ef6\nprint(f&quot;storage_ptr of a: {a.storage().data_ptr()}, storage_ptr of b: {b.storage().data_ptr()}&quot;)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/tensor_stride2.png\" alt=\"\" \/><\/p>\n<p>\u6b64\u65f6\uff0c\u82e5\u5bf9b\u4f7f\u7528<code>view()<\/code>\uff0c\u5219\u4f1a\u56e0\u4e0d\u6ee1\u8db3\u8fde\u7eed\u6027\u6761\u4ef6\u800c\u62a5\u9519\uff1a<\/p>\n<pre><code class=\"language-python\">b.view(1, 9)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/view_error.png\" alt=\"\" \/><\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\u5148\u4f7f\u7528<code>contiguous()<\/code>\u5c06\u5176\u8f6c\u6362\u5230\u6ee1\u8db3\u8fde\u7eed\u6027\u6761\u4ef6\uff0c\u8be5\u51fd\u6570\u4f1a\u5f00\u8f9f\u65b0\u7684\u5185\u5b58\u7a7a\u95f4\uff0c<strong>\u5bf9\u6570\u636e\u8fdb\u884c\u6df1\u62f7\u8d1d<\/strong>:<\/p>\n<pre><code class=\"language-python\">b = b.contiguous()\nprint(f&quot;storage_ptr of a: {a.storage().data_ptr()}, storage_ptr of b: {b.storage().data_ptr()}&quot;)  # a, b\u7684data_ptr\u4e0d\u540c\nb = b.view(1, 9)\nprint(b)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/contiguous_view.png\" alt=\"\" \/><\/p>\n<p><code>reshape()<\/code>\u4e5f\u53ef\u4ee5\u8fbe\u5230\u4e0a\u8ff0\u7684\u6548\u679c\uff1a<\/p>\n<ul>\n<li>\u5f53\u5f20\u91cf\u6ee1\u8db3\u8fde\u7eed\u6027\u6761\u4ef6\u65f6\uff0c\u7b49\u4ef7\u4e8e<code>b.view()<\/code><\/li>\n<li>\u5f53\u5f20\u91cf\u4e0d\u6ee1\u8db3\u8fde\u7eed\u6027\u6761\u4ef6\u65f6\uff0c\u7b49\u4ef7\u4e8e<code>b.contiguous().view()<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-python\">c = a.permute(1, 0)\nc = c.reshape(1, 9)\nprint(c)<\/code><\/pre>\n<p><strong>\u672c\u6587\u4ee3\u7801\u8be6\u89c1<\/strong><a href=\"http:\/\/code.3body.fun:10000\/notebooks\/Python\/torch_test\/PyTorch%E4%B8%AD%E7%9A%84view%E5%92%8Creshape.ipynb\">\u4ee3\u7801<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. PyTorch\u5f20\u91cf\u7684\u5e95\u5c42\u5b58\u50a8\u65b9\u5f0f \u5728PyTorch\u4e2d\uff0c\u5f20\u91cf\u7684\u5934\u4fe1\u606f(\u5305\u62ec\u5f20\u91cf\u540d\u3001\u5f62\u72b6\u7b49\u4fe1\u606f)\u548c\u5b9e\u9645\u7684\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":64,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,31,24],"tags":[26,34,32,33],"class_list":["post-63","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-pytorch","category-deep_learning","tag-pytorch","tag-reshape","tag-tensor","tag-view"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":1,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":65,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/65"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/64"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}