{"id":128,"date":"2021-09-27T00:40:07","date_gmt":"2021-09-26T16:40:07","guid":{"rendered":"http:\/\/139.224.63.49\/?p=128"},"modified":"2021-09-27T00:40:07","modified_gmt":"2021-09-26T16:40:07","slug":"pytorch%e6%8a%a5%e9%94%99%ef%bc%9aone-of-the-variables-needed-for-gradient-computation-has-been-modified-by%e3%80%82%e3%80%82%e3%80%82","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=128","title":{"rendered":"pytorch\u62a5\u9519\uff1aone of the variables needed for gradient computation has been modified by\u3002\u3002\u3002"},"content":{"rendered":"\n<p>\u4eca\u5929\u72af\u4e86\u4e00\u4e2a\u4f4e\u7ea7\u9519\u8bef\uff0c\u8bb0\u5f55\u5728\u6b64\uff1a<\/p>\n\n\n\n<p>pytorch\u4e2d\uff0c\u5bf9\u4e8etensor\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u9700\u8981\u68af\u5ea6\u4f20\u64ad\uff0c\u90a3\u5c31\u4e0d\u80fd\u662finplace\u7684\u3002\u8981\u6ce8\u610f\u7684\u662f\uff1a+=\uff0c*=\u4e4b\u7c7b\u7684\u8fd0\u7b97\u7b26\u662finplace\u7684\u64cd\u4f5c\uff01\uff01<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> b += a<\/code><\/pre>\n\n\n\n<p>\u4f1a\u62a5\u9519\uff0c\u4e0d\u8fc7\u5982\u679c\u6539\u6210<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b = b+a<\/code><\/pre>\n\n\n\n<p>\u5c31\u4e0d\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u6b64\u65f6b\u6240\u5f15\u7528\u7684\u5185\u5b58\u4f4d\u7f6e\u5df2\u7ecf\u6539\u53d8\u4e86\uff0c\u5373id(b)\u53d1\u751f\u4e86\u6539\u53d8<\/p>\n\n\n\n<p>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> import torch\n>>> a = torch.randn(3,4)\n>>> a.shape\ntorch.Size(&#91;3, 4])\n>>> b = torch.randn(3,4)\n>>> id(a)\n2230921339008\n>>> a = a+b\n>>> id(a)\n2230921340032\n>>> a += b\n>>> id(a)\n2230921340032\n>>><\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u6211\u4ee5\u524d\u6ca1\u6ce8\u610f\u8fc7\u7684\u70b9\uff0c\u90a3\u5c31\u662fpython\u4e2d\u5bf9\u4e8e\u7b26\u53f7\u7684\u91cd\u8f7d\uff0c\u8fd8\u633a\u590d\u6742\u7684\uff0c\u9700\u8981\u8fa8\u6790__add__\u548c__radd__ \u548c __iadd__\u7684\u533a\u522b<\/p>\n\n\n\n<p>\u53ef\u90e8\u5206\u53c2\u8003<a href=\"https:\/\/blog.csdn.net\/sinat_38682860\/article\/details\/89026764?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link\">Python\uff1a\u8868\u8fbe\u5f0f i += x \u4e0e i = i + x \u7b49\u4ef7\u5417\uff1f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u72af\u4e86\u4e00\u4e2a\u4f4e\u7ea7\u9519\u8bef\uff0c\u8bb0\u5f55\u5728\u6b64\uff1a pytorch\u4e2d\uff0c\u5bf9\u4e8etensor\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u9700\u8981\u68af\u5ea6\u4f20\u64ad\uff0c\u90a3\u5c31\u4e0d&hellip; <a href=\"http:\/\/iamnear.top\/?p=128\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">pytorch\u62a5\u9519\uff1aone of the variables needed for gradient computation has been modified by\u3002\u3002\u3002<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-128","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":1,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":130,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions\/130"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}