{"id":50387,"date":"2024-12-03T16:21:16","date_gmt":"2024-12-03T08:21:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50387\/"},"modified":"2024-12-03T16:21:16","modified_gmt":"2024-12-03T08:21:16","slug":"%e5%9c%a8-pytorch-%e4%b8%ad%e5%b1%95%e5%bc%80","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50387\/","title":{"rendered":"\u5728 PyTorch \u4e2d\u5c55\u5f00"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5728 PyTorch \u4e2d\u5c55\u5f00<\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 PyTorch \u4e2d\u5c55\u5f00\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731936444673b40bca6f4d.jpg\" class=\"aligncenter\" title=\"\u5728 PyTorch \u4e2d\u5c55\u5f00\u63d2\u56fe\" alt=\"\u5728 PyTorch \u4e2d\u5c55\u5f00\u63d2\u56fe\" \/><\/p>\n<p>\u8bf7\u6211\u559d\u676f\u5496\u5561<\/p>\n<p>*\u5907\u5fd8\u5f55\uff1a<\/p>\n<ul>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 unflatten()\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 flatten() \u548c ravel()\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 flatten()\u3002<\/li>\n<\/ul>\n<p>unflatten() \u53ef\u4ee5\u5411\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6216\u591a\u4e2a d \u5f20\u91cf\u6dfb\u52a0\u96f6\u4e2a\u6216\u591a\u4e2a\u7ef4\u5ea6\uff0c\u5f97\u5230\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6216\u591a\u4e2a d \u5f20\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>*\u5907\u5fd8\u5f55\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fdim(required-type:int)\u3002<\/li>\n<li>\u521d\u59cb\u5316\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f unflattened_size\uff08\u5fc5\u9700\u7c7b\u578b\uff1a\u5143\u7ec4\u6216 int \u5217\u8868\uff09\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8f93\u5165\uff08\u5fc5\u9700\u7c7b\u578b\uff1aint\u3001float\u3001complex \u6216 bool \u7684\u5f20\u91cf\uff09\u3002 *-1 \u63a8\u65ad\u5e76\u8c03\u6574\u5927\u5c0f\u3002<\/li>\n<li>unflatten() \u548c unflatten() \u7684\u533a\u522b\u662f\uff1a\n<ul>\n<li> unflatten() \u5177\u6709 unflattened_size \u53c2\u6570\uff0c\u8be5\u53c2\u6570\u4e0e unflatten() \u7684 size \u53c2\u6570\u76f8\u540c\u3002<\/li>\n<li>\u57fa\u672c\u4e0a\uff0cunflatten() \u7528\u4e8e\u5b9a\u4e49\u6a21\u578b\uff0c\u800c unflatten() \u4e0d\u7528\u4e8e\u5b9a\u4e49\u6a21\u578b\u3002 <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>import torch\nfrom torch import nn\n\nunflatten = nn.Unflatten()\nunflatten\n# Unflatten(dim=0, unflattened_size=(6,))\n\nunflatten.dim\n# 0\n\nunflatten.unflattened_size\n# (6,)\n\nmy_tensor = torch.tensor([7, 1, -8, 3, -6, 0])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(6,))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1,))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(6,))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,))\nunflatten(input=my_tensor)\n# tensor([7, 1, -8, 3, -6, 0])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 6))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 6))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 6))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 6))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1))\nunflatten(input=my_tensor)\n# tensor([[7, 1, -8, 3, -6, 0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2, 3))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(2, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(2, -1))\nunflatten(input=my_tensor)\n# tensor([[7, 1, -8], [3, -6, 0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(3, 2))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(3, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 2))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1))\nunflatten(input=my_tensor)\n# tensor([[7, 1], [-8, 3], [-6, 0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(6, 1))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(6, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(6, 1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(6, -1))\nunflatten(input=my_tensor)\n# tensor([[7], [1], [-8], [3], [-6], [0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, 3))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2, 3))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 3))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 2, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, -1))\nunflatten(input=my_tensor)\n# tensor([[[7, 1, -8], [3, -6, 0]]])\netc\n\nmy_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2,))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1,))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(3,))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(-1,))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(3,))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(2,))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(-1,))\nunflatten(input=my_tensor)\n# tensor([[7, 1, -8], [3, -6, 0]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 2))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 2))\nunflatten(input=my_tensor)\n# tensor([[[7, 1, -8], [3, -6, 0]]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2, 1))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(1, 3))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 3))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(2, 1))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(2, -1))\nunflatten(input=my_tensor)\n# tensor([[[7, 1, -8]], [[3, -6, 0]]])\n\nunflatten = nn.Unflatten(dim=1, unflattened_size=(3, 1))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(3, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1))\nunflatten(input=my_tensor)\n# tensor([[[7], [1], [-8]], [[3], [-6], [0]]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, 2))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 1, 2))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 2))\nunflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, -1))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, 2))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 1, 2))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(1, -1, 2))\nunflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, -1))\nunflatten(input=my_tensor)\n# tensor([[[[7, 1, -8], [3, -6, 0]]]])\n\nunflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, 3))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 1, 3))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(1, -1, 3))\nunflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, -1))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3))\nunflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, -1))\nunflatten(input=my_tensor)\n# tensor([[[[7, 1, -8]]], [[[3, -6, 0]]]])\n\nmy_tensor = torch.tensor([[7., 1., -8.], [3., -6., 0.]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2,))\nunflatten(input=my_tensor)\n# tensor([[7., 1., -8.], [3., -6., 0.]])\n\nmy_tensor = torch.tensor([[7.+0.j, 1.+0.j, -8.+0.j],\n                          [3.+0.j, -6.+0.j, 0.+0.j]])\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2,))\nunflatten(input=my_tensor)\n# tensor([[7.+0.j, 1.+0.j, -8.+0.j],\n#         [3.+0.j, -6.+0.j, 0.+0.j]])\n\nmy_tensor = torch.tensor([[True, False, True], [False, True, False]])\n\nunflatten = nn.Unflatten(dim=0, unflattened_size=(2,))\nunflatten(input=my_tensor)\n# tensor([[True, False, True], [False, True, False]])\n<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5728 PyTorch \u4e2d\u5c55\u5f00\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PyTorch \u4e2d\u5c55\u5f00 \u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300a\u5728 PyTorch \u4e2d\u5c55\u5f00\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01 \u8bf7\u6211\u559d\u676f\u5496\u5561 *\u5907\u5fd8\u5f55\uff1a \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 unflatten()\u3002 \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 flatten() \u548c ravel()\u3002 \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 flatten()\u3002 unflatten() \u53ef\u4ee5\u5411\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6216\u591a\u4e2a d \u5f20\u91cf\u6dfb\u52a0\u96f6\u4e2a\u6216\u591a\u4e2a\u7ef4\u5ea6\uff0c\u5f97\u5230\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6216\u591a\u4e2a d \u5f20\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a *\u5907\u5fd8\u5f55\uff1a \u521d\u59cb\u5316\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fdim(required-type:int)\u3002 \u521d\u59cb\u5316\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f unflattened_size\uff08\u5fc5\u9700\u7c7b\u578b\uff1a\u5143\u7ec4\u6216 int \u5217\u8868\uff09\u3002 \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8f93\u5165\uff08\u5fc5\u9700\u7c7b\u578b\uff1aint\u3001float\u3001complex \u6216 bool \u7684\u5f20\u91cf\uff09\u3002 *-1 \u63a8\u65ad\u5e76\u8c03\u6574\u5927\u5c0f\u3002 unflatten() \u548c unflatten() \u7684\u533a\u522b\u662f\uff1a unflatten() \u5177\u6709 unflattened_size \u53c2\u6570\uff0c\u8be5\u53c2\u6570\u4e0e unflatten() \u7684 size \u53c2\u6570\u76f8\u540c\u3002 \u57fa\u672c\u4e0a\uff0cunflatten() \u7528\u4e8e\u5b9a\u4e49\u6a21\u578b\uff0c\u800c unflatten() \u4e0d\u7528\u4e8e\u5b9a\u4e49\u6a21\u578b\u3002 import torch from torch import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50387","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50387"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50387\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}