\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u6d45\u8c08PyTorch\u4e2din-place operation\u7684\u542b\u4e49\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u4e00\u8d77\u8ddf\u968f\u5c0f\u7f16\u8fc7\u6765\u770b\u770b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\nin-place operation\u5728pytorch\u4e2d\u662f\u6307\u6539\u53d8\u4e00\u4e2atensor\u7684\u503c\u7684\u65f6\u5019\uff0c\u4e0d\u7ecf\u8fc7\u590d\u5236\u64cd\u4f5c\uff0c\u800c\u662f\u76f4\u63a5\u5728\u539f\u6765\u7684\u5185\u5b58\u4e0a\u6539\u53d8\u5b83\u7684\u503c\u3002\u53ef\u4ee5\u628a\u5b83\u6210\u4e3a\u539f\u5730\u64cd\u4f5c\u7b26\u3002<\/p>\n
\u5728pytorch\u4e2d\u7ecf\u5e38\u52a0\u540e\u7f00\u201c_\u201d\u6765\u4ee3\u8868\u539f\u5730in-place operation\uff0c\u6bd4\u5982\u8bf4.add_() \u6216\u8005.scatter()\u3002python\u91cc\u9762\u7684+=\uff0c*=\u4e5f\u662fin-place operation\u3002<\/p>\n
\u4e0b\u9762\u662f\u6b63\u5e38\u7684\u52a0\u64cd\u4f5c,\u6267\u884c\u7ed3\u675f\u52a0\u64cd\u4f5c\u4e4b\u540ex\u7684\u503c\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff1a<\/p>\n
\r\nimport torch\r\nx=torch.rand(2) #tensor([0.8284, 0.5539])\r\nprint(x)\r\ny=torch.rand(2)\r\nprint(x+y) #tensor([1.0250, 0.7891])\r\nprint(x) #tensor([0.8284, 0.5539])<\/pre>\n\u4e0b\u9762\u662f\u539f\u5730\u64cd\u4f5c\uff0c\u6267\u884c\u4e4b\u540e\u6539\u53d8\u4e86\u539f\u6765\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n
\r\nimport torch\r\nx=torch.rand(2) #tensor([0.8284, 0.5539])\r\nprint(x)\r\ny=torch.rand(2)\r\nx.add_(y)\r\nprint(x) #tensor([1.1610, 1.3789])<\/pre>\n\u5728\u5b98\u65b9\u95ee\u6587\u6863\u4e2d\u7531\u8fd9\u4e00\u6bb5\u8bdd\uff1a<\/p>\n
\u5982\u679c\u4f60\u4f7f\u7528\u4e86in-place operation\u800c\u6ca1\u6709\u62a5\u9519\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u786e\u5b9a\u4f60\u7684\u68af\u5ea6\u8ba1\u7b97\u662f\u6b63\u786e\u7684\u3002<\/p>\n
\u8865\u5145\u77e5\u8bc6\uff1aPyTorch\u4e2dnn.ReLU(inplace=True)\u4e2dinplace\u7684\u4f5c\u7528<\/strong><\/span><\/div>\n\u6211\u4eec\u7528PyTorch\u642d\u5efa\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u4f1a\u9047\u5230nn.ReLU(inplace=True),inplace=True\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f<\/p>\n
nn.Conv2d(64,192,kernel_size=3,stride=1,padding=1),\r\nnn.ReLu(inpalce=True),# inplace\u4e3aTrue\uff0c\u9ed8\u8ba4\u4e3aFalse<\/pre>\n\u610f\u601d\u662f\uff1a\u662f\u5426\u5c06\u8ba1\u7b97\u5f97\u5230\u7684\u503c\u76f4\u63a5\u8986\u76d6\u4e4b\u524d\u7684\u503c<\/p>\n
\u4f8b\u5982\uff1ax = x+1<\/p>\n
\u5373\u5bf9\u539f\u503cx\u8fdb\u884c+1\u64cd\u4f5c\u540e\u5f97\u5230\u7684\u503c\uff0c\u76f4\u63a5\u8d4b\u503c\u7ed9x<\/p>\n
\u800c\u4e0d\u662f\u5982\u4e0b\u627e\u4e00\u4e2a\u4e2d\u95f4\u53d8\u91cfy\uff1a<\/p>\n
y=x+1\r\nx=y<\/pre>\n\u5148\u5c06x\u8fdb\u884c+1\u64cd\u4f5c\u540e\u8d4b\u503c\u7ed9\u4e2d\u95f4\u53d8\u91cfy\uff0c\u7136\u540e\u5c06y\u503c\u8d4b\u7ed9x<\/p>\n
\u8fd9\u6837\u5c31\u9700\u8981\u5185\u5b58\u5b58\u50a8\u53d8\u91cfy<\/p>\n
\u56e0\u6b64\u5f53inplace=True\u65f6\uff1a<\/p>\n
\u5c31\u662f\u5bf9\u4ece\u4e0a\u5c42\u7f51\u7edcnn.Conv2d\u4e2d\u4f20\u9012\u4e0b\u6765\u7684tensor\u76f4\u63a5\u8fdb\u884c\u4fee\u6539\uff0c\u8fd9\u6837\u80fd\u591f\u8282\u7701\u8fd0\u7b97\u5185\u5b58\uff0c\u4e0d\u7528\u591a\u5b58\u50a8\u5176\u4ed6\u53d8\u91cf\u3002<\/p>\n
\u4ee5\u4e0a\u8fd9\u7bc7\u6d45\u8c08PyTorch\u4e2din-place operation\u7684\u542b\u4e49\u5c31\u662f\u5c0f\u7f16\u5206\u4eab\u7ed9\u5927\u5bb6\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
in-place operation\u5728pytorch\u4e2d\u662f\u6307\u6539\u53d8\u4e00\u4e2atensor\u7684\u503c\u7684\u65f6\u5019\uff0c\u4e0d\u7ecf\u8fc7\u590d\u5236\u64cd\u4f5c\uff0c\u800c\u662f […]<\/p>\n","protected":false},"author":1482,"featured_media":170268,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195070","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=195070"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195070\/revisions"}],"predecessor-version":[{"id":195072,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195070\/revisions\/195072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/170268"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=195070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=195070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=195070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}