{"id":934,"date":"2021-12-29T18:44:06","date_gmt":"2021-12-29T10:44:06","guid":{"rendered":"http:\/\/139.9.1.231\/?p=934"},"modified":"2021-12-29T18:51:33","modified_gmt":"2021-12-29T10:51:33","slug":"nametorch","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/29\/nametorch\/","title":{"rendered":"\u547d\u540d\u5f20\u91cf"},"content":{"rendered":"\n<p>        \u5f20\u91cf\u547d\u540d\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u7ef4\u5ea6\u7684\u540d\u5b57\u6765\u505a\u7d22\u5f15\u6216\u5176\u4ed6\u64cd\u4f5c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3001\u6613\u7528\u6027\uff0c\u9632\u6b62\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u547d\u540d\u7684\u5bf9\u8c61\u662f\u5f20\u91cf\u7684\u7ef4\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NCHW = &#91;\u2018N\u2019, \u2018C\u2019, \u2018H\u2019, \u2018W\u2019]\n#\u5f20\u91cf\u547d\u540d\nimages = torch.randn(32, 3, 56, 56, names=NCHW)\nimages.sum('C')\nimages.select('C', index=0)\n<em># \u4e5f\u53ef\u4ee5\u8fd9\u4e48\u8bbe\u7f6e<\/em>\ntensor = torch.rand(3,4,1,2,names=('C', 'N', 'H', 'W'))\n<em># \u4f7f\u7528align_to\u53ef\u4ee5\u5bf9\u7ef4\u5ea6\u65b9\u4fbf\u5730\u6392\u5e8f<\/em>\ntensor = tensor.align_to('N', 'C', 'H', 'W')<\/code><\/pre>\n\n\n\n<p>\u547d\u540d\u5f20\u91cf\u5141\u8bb8\u7528\u6237\u4e3a\u5f20\u91cf\u7ef4\u5ea6\u6307\u5b9a\u660e\u786e\u7684\u540d\u79f0\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u91c7\u7528\u7ef4\u5ea6\u53c2\u6570\u7684\u64cd\u4f5c\u5c06\u63a5\u53d7\u7ef4\u5ea6\u540d\u79f0\uff0c\u4ece\u800c\u65e0\u9700\u6309\u4f4d\u7f6e\u8ddf\u8e2a\u7ef4\u5ea6\u3002\u6b64\u5916\uff0c\u547d\u540d\u5f20\u91cf\u4f7f\u7528\u540d\u79f0\u6765\u81ea\u52a8\u68c0\u67e5 API \u5728\u8fd0\u884c\u65f6\u662f\u5426\u88ab\u6b63\u786e\u4f7f\u7528\uff0c\u4ece\u800c\u63d0\u4f9b\u989d\u5916\u7684\u5b89\u5168\u6027\u3002\u540d\u79f0\u8fd8\u53ef\u7528\u4e8e\u91cd\u65b0\u6392\u5217\u7ef4\u5ea6\uff0c\u4f8b\u5982\uff0c\u652f\u6301\u201c\u6309\u540d\u79f0\u5e7f\u64ad\u201d\u800c\u4e0d\u662f\u201c\u6309\u4f4d\u7f6e\u5e7f\u64ad\u201d\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a\u5bf9\u4e8e\u4efb\u610f\u5f20\u91cf\uff0c\u91c7\u7528\u4e00\u4e2a <code><strong>names<\/strong><\/code> \u53c2\u6570\uff0c<strong>\u53c2\u6570\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u7c7b\u578b\u5217\u8868or\u5143\u7ec4\uff0c\u503c\u8868\u793a<strong>\u7ef4\u5ea6\u540d\u79f0<\/strong><\/strong>\uff0c\u5c06\u540d\u79f0\u4e0e\u6bcf\u4e2a\u7ef4\u5ea6\u76f8\u5173\u8054\u3002<\/p>\n\n\n\n<pre id=\"codecell0\" class=\"wp-block-preformatted\">&gt;&gt;&gt; <strong>torch.zeros(<\/strong>2<strong>,<\/strong> 3<strong>,<\/strong> <strong>names=(<\/strong>'N'<strong>,<\/strong> 'C'<strong>))<\/strong>\ntensor([[0., 0., 0.],\n        [0., 0., 0.]], names=('N', 'C'))\n\n\n&lt;!-- wp:preformatted --&gt;\n&lt;pre id=\"codecell0\" class=\"wp-block-preformatted\"&gt;&gt;&gt;&gt; &lt;strong&gt;torch.zeros(&lt;\/strong&gt;2&lt;strong&gt;,&lt;\/strong&gt; 3&lt;strong&gt;,&lt;\/strong&gt; &lt;strong&gt;names=(&lt;\/strong&gt;'N'&lt;strong&gt;,&lt;\/strong&gt; 'C'&lt;strong&gt;))&lt;\/strong&gt;\ntensor([[0., 0., 0.],\n        [0., 0., 0.]], names=('N', 'C'))\n\n\n#\u83b7\u5f97\u7ef4\u5ea6\u7684\u540d\u79f0\u4e3ai\u7684tensor\ntensor.names[i]#\u83b7\u5f97\u4e3a\u7ef4\u5ea6\u7684\u540d\u79f0i\u7684tensor\n<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u51fd\u6570\u652f\u6301\u547d\u540d\u5f20\u91cf\uff1a<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.empty.html#torch.empty\"><code>torch.empty()<\/code><\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.rand.html#torch.rand\"><code>torch.rand()<\/code><\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.randn.html#torch.randn\"><code>torch.randn()<\/code><\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.ones.html#torch.ones\"><code>torch.ones()<\/code><\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.tensor.html#torch.tensor\"><code>torch.tensor()<\/code><\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.zeros.html#torch.zeros\"><code>torch.zeros()<\/code><\/a><\/li><\/ul>\n\n\n\n<p><strong>\u4f7f\u7528tensor.<a href=\"https:\/\/pytorch.org\/docs\/stable\/named_tensor.html#torch.Tensor.names\"><code>names<\/code><\/a>\u8bbf\u95ee\u5f20\u91cf\u7684\u7ef4\u5ea6\u540d\u79f0\u548c&nbsp;tensor.<a href=\"https:\/\/pytorch.org\/docs\/stable\/named_tensor.html#torch.Tensor.rename\"><code>rename()<\/code><\/a>\u91cd\u547d\u540d\u547d\u540d\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"codecell1\" class=\"wp-block-preformatted\">&gt;&gt;&gt; <strong>imgs<\/strong> <strong>=<\/strong> <strong>torch<\/strong><strong>.<\/strong><strong>randn<\/strong><strong>(<\/strong>1<strong>,<\/strong> 2<strong>,<\/strong> 2<strong>,<\/strong> 3 <strong>,<\/strong> <strong>names<\/strong><strong>=<\/strong><strong>(<\/strong>'N'<strong>,<\/strong> 'C'<strong>,<\/strong> 'H'<strong>,<\/strong> 'W'<strong>))<\/strong>\n&gt;&gt;&gt; <strong>imgs<\/strong><strong>.<\/strong><strong>names<\/strong>\n('N', 'C', 'H', 'W')\n\n&gt;&gt;&gt; <strong>renamed_imgs<\/strong> <strong>=<\/strong> <strong>imgs<\/strong><strong>.<\/strong><strong>rename<\/strong><strong>(<\/strong><strong>H<\/strong><strong>=<\/strong>'height'<strong>,<\/strong> <strong>W<\/strong><strong>=<\/strong>'width'<strong>)<\/strong>\n&gt;&gt;&gt; <strong>renamed_imgs<\/strong><strong>.<\/strong><strong>names<\/strong>\n('N', 'C', 'height', 'width)<\/pre>\n\n\n\n<p>\u547d\u540d\u5f20\u91cf\u53ef\u4ee5\u4e0e\u672a\u547d\u540d\u5f20\u91cf\u5171\u5b58\uff1b\u547d\u540d\u5f20\u91cf\u662f \u7684\u5b9e\u4f8b&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/tensors.html#torch.Tensor\"><code>torch.Tensor<\/code><\/a>\u3002\u672a\u547d\u540d\u5f20\u91cf\u5177\u6709\u547d\u540d<code>None<\/code>\u7ef4\u5ea6\u3002\u547d\u540d\u5f20\u91cf\u4e0d\u9700\u8981\u547d\u540d\u6240\u6709\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<pre id=\"codecell2\" class=\"wp-block-preformatted\">&gt;&gt;&gt; <strong>imgs<\/strong> <strong>=<\/strong> <strong>torch<\/strong><strong>.<\/strong><strong>randn<\/strong><strong>(<\/strong>1<strong>,<\/strong> 2<strong>,<\/strong> 2<strong>,<\/strong> 3 <strong>,<\/strong> <strong>names<\/strong><strong>=<\/strong><strong>(<\/strong><strong>None<\/strong><strong>,<\/strong> 'C'<strong>,<\/strong> 'H'<strong>,<\/strong> 'W'<strong>))<\/strong>\n&gt;&gt;&gt; <strong>imgs<\/strong><strong>.<\/strong><strong>names<\/strong>\n(None, 'C', 'H', 'W')<\/pre>\n\n\n\n<h2>\u540d\u79f0\u4f20\u64ad\u8bed\u4e49<\/h2>\n\n\n\n<p>\u547d\u540d\u5f20\u91cf\u4f7f\u7528\u540d\u79f0\u6765\u81ea\u52a8\u68c0\u67e5 API \u5728\u8fd0\u884c\u65f6\u662f\u5426\u88ab\u6b63\u786e\u8c03\u7528\u3002\u8fd9\u53d1\u751f\u5728\u4e00\u4e2a\u540d\u4e3a<em>name inference<\/em>\u7684\u8fc7\u7a0b\u4e2d\u3002\u66f4\u6b63\u5f0f\u5730\u8bf4\uff0c\u540d\u79f0\u63a8\u65ad\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ul><li><strong>\u68c0\u67e5\u540d\u79f0<\/strong>\uff1a\u64cd\u4f5c\u5458\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6267\u884c\u81ea\u52a8\u68c0\u67e5\uff0c\u4ee5\u68c0\u67e5\u67d0\u4e9b\u7ef4\u5ea6\u540d\u79f0\u662f\u5426\u5fc5\u987b\u5339\u914d\u3002<\/li><li><strong>\u4f20\u64ad\u540d\u79f0<\/strong>\uff1a\u540d\u79f0\u63a8\u65ad\u5c06\u540d\u79f0\u4f20\u64ad\u5230\u8f93\u51fa\u5f20\u91cf\u3002<\/li><\/ul>\n\n\n\n<p>\u6240\u6709\u652f\u6301\u547d\u540d\u5f20\u91cf\u7684\u64cd\u4f5c\u90fd\u4f1a\u4f20\u64ad\u540d\u79f0\u3002<\/p>\n\n\n\n<pre id=\"codecell3\" class=\"wp-block-preformatted\">&gt;&gt;&gt; <strong>x<\/strong> <strong>=<\/strong> <strong>torch<\/strong><strong>.<\/strong><strong>randn<\/strong><strong>(<\/strong>3<strong>,<\/strong> 3<strong>,<\/strong> <strong>names<\/strong><strong>=<\/strong><strong>(<\/strong>'N'<strong>,<\/strong> 'C'<strong>))<\/strong>\n&gt;&gt;&gt; <strong>x<\/strong><strong>.<\/strong><strong>abs<\/strong><strong>()<\/strong><strong>.<\/strong><strong>names<\/strong>\n('N', 'C')<\/pre>\n\n\n\n<p>\u5b98\u65b9\u6587\u6863\u5730\u5740\uff1a <a href=\"https:\/\/pytorch.org\/docs\/stable\/named_tensor.html\">https:\/\/pytorch.org\/docs\/stable\/named_tensor.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f20\u91cf\u547d\u540d\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u7ef4\u5ea6\u7684\u540d\u5b57\u6765\u505a\u7d22\u5f15\u6216\u5176\u4ed6\u64cd\u4f5c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3001\u6613\u7528\u6027\uff0c\u9632\u6b62\u51fa &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/29\/nametorch\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u547d\u540d\u5f20\u91cf<\/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\/934"}],"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=934"}],"version-history":[{"count":17,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/934\/revisions"}],"predecessor-version":[{"id":952,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/934\/revisions\/952"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=934"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}