-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPytorch detach 和 retain_graph - 知乎.html
115 lines (113 loc) · 348 KB
/
Pytorch detach 和 retain_graph - 知乎.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html data-theme="light" data-react-helmet="data-theme" lang="zh"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="utf-8"><title>Pytorch: detach 和 retain_graph - 知乎</title><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="renderer" content="webkit"><meta name="force-rendering" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="google-site-verification" content="FTeR0c8arOPKh8c5DYh_9uu98_zJbaWw53J-Sch9MTg"><meta data-react-helmet="true" name="keywords" content="神经网络,机器学习,生成对抗网络(GAN)"><meta data-react-helmet="true" name="description" content="本人观察 Pytorch 下的生成对抗网络(GAN)的实现代码,发现不同人的实现细节略有不同,其中用到了 detach 和 retain_graph,本文通过两个 gan 的代码,介绍它们的作用,并分析,不同的更新策略对程序效率的影响。…"><meta data-react-helmet="true" property="og:title" content="Pytorch: detach 和 retain_graph"><meta data-react-helmet="true" property="og:url" content="https://zhuanlan.zhihu.com/p/43843694"><meta data-react-helmet="true" property="og:description" content="本人观察 Pytorch 下的生成对抗网络(GAN)的实现代码,发现不同人的实现细节略有不同,其中用到了 detach 和 retain_graph,本文通过两个 gan 的代码,介绍它们的作用,并分析,不同的更新策略对程序效率的影响。…"><meta data-react-helmet="true" property="og:image" content=""><meta data-react-helmet="true" property="og:type" content="article"><meta data-react-helmet="true" property="og:site_name" content="知乎专栏"><link data-react-helmet="true" rel="apple-touch-icon" href="https://static.zhihu.com/heifetz/assets/apple-touch-icon-152.a53ae37b.png"><link data-react-helmet="true" rel="apple-touch-icon" href="https://static.zhihu.com/heifetz/assets/apple-touch-icon-152.a53ae37b.png" sizes="152x152"><link data-react-helmet="true" rel="apple-touch-icon" href="https://static.zhihu.com/heifetz/assets/apple-touch-icon-120.bbce8f18.png" sizes="120x120"><link data-react-helmet="true" rel="apple-touch-icon" href="https://static.zhihu.com/heifetz/assets/apple-touch-icon-76.cbade8f9.png" sizes="76x76"><link data-react-helmet="true" rel="apple-touch-icon" href="https://static.zhihu.com/heifetz/assets/apple-touch-icon-60.8f6c52aa.png" sizes="60x60"><link rel="shortcut icon" type="image/x-icon" href="https://static.zhihu.com/heifetz/favicon.ico"><link rel="search" type="application/opensearchdescription+xml" href="https://static.zhihu.com/heifetz/search.xml" title="知乎"><link rel="dns-prefetch" href="https://static.zhimg.com/"><link rel="dns-prefetch" href="https://pic1.zhimg.com/"><link rel="dns-prefetch" href="https://pic2.zhimg.com/"><link rel="dns-prefetch" href="https://pic3.zhimg.com/"><link rel="dns-prefetch" href="https://pic4.zhimg.com/"><style>
.u-safeAreaInset-top {
height: constant(safe-area-inset-top) !important;
height: env(safe-area-inset-top) !important;
}
.u-safeAreaInset-bottom {
height: constant(safe-area-inset-bottom) !important;
height: env(safe-area-inset-bottom) !important;
}
</style><link href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_005.css" rel="stylesheet"><script defer="defer" crossorigin="anonymous" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/init.js" data-sentry-config="{"dsn":"https://[email protected]/1224","sampleRate":0.1,"release":"1710-1a380e91","ignoreErrorNames":["NetworkError","SecurityError"],"ignoreErrors":["origin message","Network request failed","Loading chunk","这个系统不支持该功能。","Can't find variable: webkit","Can't find variable: $","内存不足","out of memory","DOM Exception 18","The operation is insecure","[object Event]","[object FileError]","[object DOMError]","[object Object]","拒绝访问。","Maximum call stack size exceeded","UploadError","无法 fetch","draft-js","缺少 JavaScript 对象","componentWillEnter","componentWillLeave","componentWillAppear","getInlineStyleAt","getCharacterList"],"whitelistUrls":["static.zhihu.com"]}"></script><style data-emotion-css="1cd9gw4">.css-1cd9gw4{margin-left:.3em;}</style><style data-emotion-css="qbubgm">.css-qbubgm{margin-left:0;}</style><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_002.js"></script><link rel="stylesheet" type="text/css" href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_006.css"><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_007.js"></script><link rel="stylesheet" type="text/css" href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_002.css"><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_003.js"></script><link rel="stylesheet" type="text/css" href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_004.css"><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_005.js"></script><link rel="stylesheet" type="text/css" href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_003.css"><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column.js"></script><style data-emotion="css"></style><link rel="stylesheet" type="text/css" href="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column.css"><script charset="utf-8" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_004.js"></script></head><body class="WhiteBg-body" data-react-helmet="class" data-new-gr-c-s-check-loaded="8.869.0" data-gr-ext-installed="" data-gr-ext-disabled="forever"><div id="root"><div class="App"><div class="LoadingBar"></div><main role="main" class="App-main"><div class="Post-content" data-zop-usertoken="{"userToken":"yin-xiang-nan"}" data-zop="{"authorName":"杨指北","itemId":43843694,"title":"Pytorch: detach 和 retain_graph","type":"article"}" data-za-detail-view-path-module="PostItem" data-za-extra-module="{"card":{"content":{"type":"Post","token":"43843694"}}}"><div class="ColumnPageHeader-Wrapper"><div><div class="Sticky ColumnPageHeader is-fixed" style="width: 1068px; top: 0px; left: 0px;"><div class="ColumnPageHeader-content"><a class="ZhihuLogoLink" href="https://www.zhihu.com/" aria-label="知乎"><svg viewBox="0 0 64 30" fill="#0066FF" width="64" height="30"><path d="M29.05 4.582H16.733V25.94h3.018l.403 2.572 4.081-2.572h4.815V4.582zm-5.207 18.69l-2.396 1.509-.235-1.508h-1.724V7.233h6.78v16.04h-2.425zM14.46 14.191H9.982c0-.471.033-.954.039-1.458v-5.5h5.106V5.935a1.352 1.352 0 0 0-.404-.957 1.378 1.378 0 0 0-.968-.396H5.783c.028-.088.056-.177.084-.255.274-.82 1.153-3.326 1.153-3.326a4.262 4.262 0 0 0-2.413.698c-.57.4-.912.682-1.371 1.946-.532 1.453-.997 2.856-1.31 3.693C1.444 8.674.28 11.025.28 11.025a5.85 5.85 0 0 0 2.52-.61c1.119-.593 1.679-1.502 2.054-2.883l.09-.3h2.334v5.5c0 .5-.045.982-.073 1.46h-4.12c-.71 0-1.39.278-1.893.775a2.638 2.638 0 0 0-.783 1.874h6.527a17.717 17.717 0 0 1-.778 3.649 16.796 16.796 0 0 1-3.012 5.273A33.104 33.104 0 0 1 0 28.74s3.13 1.175 5.425-.954c1.388-1.292 2.631-3.814 3.23-5.727a28.09 28.09 0 0 0 1.12-5.229h5.967v-1.37a1.254 1.254 0 0 0-.373-.899 1.279 1.279 0 0 0-.909-.37z"></path><path d="M11.27 19.675l-2.312 1.491 5.038 7.458a6.905 6.905 0 0 0 .672-2.218 3.15 3.15 0 0 0-.28-2.168l-3.118-4.563zM51.449 15.195V5.842c4.181-.205 7.988-.405 9.438-.483l.851-.05c.387-.399.885-2.395.689-3.021-.073-.25-.213-.666-.638-.555a33.279 33.279 0 0 1-4.277.727c-2.766.321-3.97.404-7.804.682-6.718.487-12.709.72-12.709.72a2.518 2.518 0 0 0 .788 1.834 2.567 2.567 0 0 0 1.883.706c2.278-.095 5.598-.25 8.996-.41v9.203h-12.78c0 .703.281 1.377.783 1.874a2.69 2.69 0 0 0 1.892.777h10.105v7.075c0 .887-.464 1.192-1.231 1.214h-3.92a4.15 4.15 0 0 0 .837 1.544 4.2 4.2 0 0 0 1.403 1.067 6.215 6.215 0 0 0 2.71.277c1.36-.066 2.967-.826 2.967-3.57v-7.607h11.28c.342 0 .67-.135.91-.374.242-.239.378-.563.378-.902v-1.375H51.449z"></path><path d="M42.614 8.873a2.304 2.304 0 0 0-1.508-.926 2.334 2.334 0 0 0-1.727.405l-.376.272 4.255 5.85 2.24-1.62-2.884-3.98zM57.35 8.68l-3.125 4.097 2.24 1.663 4.517-5.927-.375-.277a2.32 2.32 0 0 0-1.722-.452 2.327 2.327 0 0 0-1.536.896z"></path></svg></a><i class="ColumnPageHeader-Line"></i><div class="ColumnPageHeader-Title"><div class="ColumnPageHeader-TitleName"><span class="ColumnPageHeader-TitleMeta">首发于</span><a class="ColumnLink ColumnPageHeader-TitleColumn" href="https://www.zhihu.com/column/c_167341220">轮子工厂</a></div></div><div class="ColumnPageHeader-Button"><button type="button" class="Button ColumnPageHeader-WriteButton Button--blue"><svg class="Zi Zi--EditSurround" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M18.453 7.992l-1.833-1.65.964-.978a1.223 1.223 0 0 1 1.73-.012l.005.006a1.24 1.24 0 0 1 .007 1.748l-.873.886zm-1.178 1.194l-5.578 5.66-1.935.697a.393.393 0 0 1-.504-.504l.697-1.935 5.488-5.567 1.832 1.65zM7.58 5.848l5.654.006-1.539 1.991-3.666.012A1.02 1.02 0 0 0 7 8.868v7.993c0 .558.46 1.01 1.029 1.01l7.941-.01c.568 0 1.03-.453 1.03-1.012v-4.061l2-1.442v6.002c0 1.397-1.2 2.501-2.62 2.501H7.574C6.153 19.85 5 18.717 5 17.32V8.35c0-1.397 1.16-2.502 2.58-2.502z"></path></svg>写文章</button><div class="Popover"><button title="更多" id="Popover1-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover1-content" type="button" class="Button ColumnPageHeader-MenuToggler Button--plain"><svg class="Zi Zi--Dots" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M5 14a2 2 0 1 1 0-4 2 2 0 0 1 0 4zm7 0a2 2 0 1 1 0-4 2 2 0 0 1 0 4zm7 0a2 2 0 1 1 0-4 2 2 0 0 1 0 4z" fill-rule="evenodd"></path></svg></button></div></div></div></div><div class="Sticky--holder" style="position: relative; inset: 0px; display: block; float: none; margin: 0px; height: 52px;"></div></div></div><article class="Post-Main Post-NormalMain" tabindex="-1"><header class="Post-Header"><h1 class="Post-Title">Pytorch: detach 和 retain_graph</h1><div class="Post-Author"><div class="AuthorInfo" itemprop="author" itemscope="" itemtype="http://schema.org/Person"><meta itemprop="name" content="杨指北"><meta itemprop="image" content="https://pic4.zhimg.com/bf28449bb_l.jpg"><meta itemprop="url" content="https://www.zhihu.com/people/yin-xiang-nan"><meta itemprop="zhihu:followerCount"><span class="UserLink AuthorInfo-avatarWrapper"><div class="Popover"><div id="Popover15-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover15-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar Avatar--round AuthorInfo-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_xs.jpg" srcset="https://pic4.zhimg.com/bf28449bb_l.jpg 2x" alt="杨指北" width="38" height="38"></a></div></div></span><div class="AuthorInfo-content"><div class="AuthorInfo-head"><span class="UserLink AuthorInfo-name"><div class="Popover"><div id="Popover16-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover16-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></div></div></span></div><div class="AuthorInfo-detail"><div class="AuthorInfo-badge"><div class="ztext AuthorInfo-badgeText">无业游民</div></div></div></div></div></div><div class="LabelContainer-wrapper"></div><div><span class="Voters"><span><span class="UserLink"><div class="Popover"><div id="Popover20-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover20-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/li-lu-jun-39">李路军</a></div></div></span></span><span>、<span class="UserLink"><div class="Popover"><div id="Popover21-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover21-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/MerleCahng">Merle磊磊</a></div></div></span></span><span>、<span class="UserLink"><div class="Popover"><div id="Popover22-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover22-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-dong-53">王大东</a></div></div></span></span><span>、<span class="UserLink"><div class="Popover"><div id="Popover23-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover23-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/llcing">Mayouji</a></div></div></span></span> 等 <button type="button" class="Button Button--plain">163 人赞同了该文章</button></span></div></header><div class="Post-RichTextContainer"><div class="RichText ztext Post-RichText"><p>本人观察 Pytorch 下的生成对抗网络(GAN)的实现代码,发现不同人的实现细节略有不同,其中用到了 detach 和 retain_graph,本文通过两个 gan 的代码,介绍它们的作用,并分析,不同的更新策略对程序效率的影响。</p><p>这两个 GAN 的实现中,其更新策略不同,前者是先更新判别器(discriminator)参数,再更新生成器(generator)参数,这正是原始论文<a href="https://link.zhihu.com/?target=https%3A//arxiv.org/abs/1406.2661" class=" wrap external" target="_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">Generative Adversarial Networks</a> 中的算法(下图所示);后者是先更新 generator 参数,再更新 discriminator 参数,它们的实现孰优孰劣呢?</p><p class="ztext-empty-paragraph"><br></p><figure data-size="normal"><noscript><img src="https://pic2.zhimg.com/v2-1c7add429e5c48bb783bc50e8af3fddd_b.jpg" data-caption="" data-size="normal" data-rawwidth="697" data-rawheight="456" class="origin_image zh-lightbox-thumb" width="697" data-original="https://pic2.zhimg.com/v2-1c7add429e5c48bb783bc50e8af3fddd_r.jpg"/></noscript><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-1c7add429e5c48bb783bc50e8af3fddd_720w.jpg" data-caption="" data-size="normal" data-rawwidth="697" data-rawheight="456" class="origin_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-1c7add429e5c48bb783bc50e8af3fddd_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-1c7add429e5c48bb783bc50e8af3fddd_b.jpg" data-lazy-status="ok" width="697"></figure><h2><b>GAN 的基本原理</b></h2><p>首先回顾一下生成对抗网络算法的基本原理,对于熟悉的同学,可以跳过这部分。限于篇幅,只介绍最原始的两种 GAN 损失函数,不失一般性。</p><p>原始 GAN 的损失函数,主要来源于 binary cross-entropy loss:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_004.svg" alt="[公式]" eeimg="1" data-formula="L(x^{(i)},y^{(i)})=-y^{(i)}\cdot\mathrm{log}P(x^{(i)})-(1-y^{(i)})\cdot\mathrm{log}(1-P(x^{(i)}))\tag{1}"> </p><p>其中,y 为真实标签,取 1 (正样本) 或者 0 (负样本),P(x) 为 x 属于正样本的概率。</p><p><b>判别器</b>有两种输入,一种是真实的样本 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_013.svg" alt="[公式]" eeimg="1" data-formula="(x\in P_r,y=1)">;一种是生成器通过噪音 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_012.svg" alt="[公式]" eeimg="1" data-formula="z"> 生成的假样本 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_009.svg" alt="[公式]" eeimg="1" data-formula="(G(z)\in P_g, y=0)"> 。判别器的工作是将这两种来源的样本区分开,故采用公式 (1) 计算其损失函数 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_019.svg" alt="[公式]" eeimg="1" data-formula="L_d"> 。</p><p>分别把真实样本和生成的假样本代入公式 (1),对于每个正样本,其损失函数只剩下:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_008.svg" alt="[公式]" eeimg="1" data-formula="L_{d_{real}}(x^{(i)}\in P_r,y^{(i)}=1)=-\mathrm{log}D(x^{(i)})\tag{2}"> </p><p>而对于每个负样本,损失函数为:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_003.svg" alt="[公式]" eeimg="1" data-formula="L_{d_{fake}}(G(z^{(i)}),y^{(i)}=0)=-\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{3}"> </p><p>(这里把公式 (1) 的 P(x) 改为 D(x),表示是 discriminator 的输出,和上图中的算法保持一致)。把这两个损失函数加起来,算一下平均值:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_007.svg" alt="[公式]" eeimg="1" data-formula="L_{d_{total}}=-\frac{1}{m}\sum_{i=1}^{m}\mathrm{log}D(x^{(i)})+\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{4}"> </p><p>然后,将这个损失函数对 discriminator 的参数 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_021.svg" alt="[公式]" eeimg="1" data-formula="\theta_d"> 求导,即得到上图算法中的梯度:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_011.svg" alt="[公式]" eeimg="1" data-formula="\frac{\mathrm{d}L_{d_{total}}}{\mathrm{d}\theta_d}=-\nabla_{\theta_d}\frac{1}{m}\sum_{i=1}^{m}\mathrm{log}D(x^{(i)})+\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{5}"> </p><p>(<i><b>注意算法截图中用梯度上升优化,所以和上式差一个负号</b></i>)</p><p><b>生成器</b>输入噪声 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_002.svg" alt="[公式]" eeimg="1" data-formula="z^{(i)}"> ,输出一个假数据 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_016.svg" alt="[公式]" eeimg="1" data-formula="G(z^{(i)})"> 。它希望这个假数据能骗过判别器。而上文判别器对假数据的损失函数为:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_003.svg" alt="[公式]" eeimg="1" data-formula="L_{d_{fake}}(G(z^{(i)}),y^{(i)}=0)=-\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{3}"> </p><p>这个损失越大,意味着判别器的性能越差,同时也意味着生成器的性能越好。也就是说,我们希望朝着增大该损失的方向来调整生成器的参数。如果采用梯度下降优化生成器,也就是要减小它的相反数,即生成器的损失函数:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_006.svg" alt="[公式]" eeimg="1" data-formula="L_g(z^{(i)})=\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{6}"> </p><p>这就是原始 GAN 生成器的第一种损失函数,对其取平均再对生成器参数 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation.svg" alt="[公式]" eeimg="1" data-formula="\theta_g"> 求梯度,得到算法截图里面的公式:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_015.svg" alt="[公式]" eeimg="1" data-formula="\frac{\mathrm{d}L_{g_{total}}}{\mathrm{d}\theta_g}=\nabla_{\theta_g}\frac{1}{m}\sum_{i=1}^m\mathrm{log}\left(1-D(G(z^{(i)}))\right)\tag{7}"> </p><p>(<i><b>算法截图中用梯度下降优化,所以和上式相同</b></i>)</p><p>但是,公式 (6) 的损失函数在训练初期梯度会特别小。因为刚开始训练时,生成器生成的数据十分不真实,导致判别器很容易判断出来它是假数据。因此 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_010.svg" alt="[公式]" eeimg="1" data-formula="D(G(z^{(i)}))"> 是个接近于0 的常数。而损失函数 (6) 对 generator 参数 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation.svg" alt="[公式]" eeimg="1" data-formula="\theta_g"> 的导数为:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_014.svg" alt="[公式]" eeimg="1" data-formula="\frac{\mathrm{d}L_g(z^{(i)})}{\mathrm{d}\theta_g}=-\frac{1}{1-D(G(z^{(i)}))}\cdot\frac{\mathrm{d}D(G(z^{(i)}))}{\mathrm{d}G(z^{(i)})}\cdot\frac{\mathrm{d}G(z^{(i)})}{\mathrm{d}\theta_g}\tag{8}"> </p><p>由于 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_010.svg" alt="[公式]" eeimg="1" data-formula="D(G(z^{(i)}))"> 是接近于0的常数,所以 (8) 中等号右边第一项近似为 -1,而中间项为接近 0 的数(因为 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_010.svg" alt="[公式]" eeimg="1" data-formula="D(G(z^{(i)}))"> 是接近 0 的常函数,而常函数的梯度为 0)。</p><p>因此,GAN 的作者又给出了第二种生成器损失函数:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_005.svg" alt="[公式]" eeimg="1" data-formula="L_g(z^{(i)})=-\mathrm{log}(D(G(z^{(i)})))\tag{9}"> </p><p>这个损失函数很好理解,就是要生成这样的数据:使得输入 discriminator 后,输出一个较大的概率,即对生成器来说 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_017.svg" alt="[公式]" eeimg="1" data-formula="\mathrm{log}(D(G(z^{(i)})))"> 越大越好,取它的相反数,也就是公式 (9) 越小越好。我们计算公式 (9) 的损失函数对生成器参数 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation.svg" alt="[公式]" eeimg="1" data-formula="\theta_g"> 的导数:</p><p><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_020.svg" alt="[公式]" eeimg="1" data-formula="\frac{\mathrm{d}L_g(z^{(i)})}{\mathrm{d}\theta_g}=-\frac{1}{D(G(z^{(i)}))}\cdot\frac{\mathrm{d}D(G(z^{(i)}))}{\mathrm{d}G(z^{(i)})}\cdot\frac{\mathrm{d}G(z^{(i)})}{\mathrm{d}\theta_g}\tag{10}"> </p><p>和 (8) 比只有等号右边第一项不同,当 <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_010.svg" alt="[公式]" eeimg="1" data-formula="D(G(z^{(i)}))"> 接近 0 时, <img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/equation_018.svg" alt="[公式]" eeimg="1" data-formula="1/D(G(z^{(i)}))"> 接近无穷大,和中间那项相乘,一定程度抵消了中间项接近 0 的问题,使计算出来的梯度增大了。</p><p>在实现过程中,基本上都是采用公式 (9) 作为生成器损失函数,因为这十分方便,只要在计算生成器损失函数时,把虚假数据的标签标记为真,代入 Binary cross-entropy loss 的公式即可。</p><h2><b>更新策略</b></h2><p>下面进入本文正题,即,在 pytorch 中,detach 和 retain_graph 是干什么用的?本文将借助两段 GAN 的实现代码,来举例介绍它们的作用。</p><p><b>第一段代码先更新判别器,再更新生成器。</b>我们分析循环中一个 step 的代码:</p><div class="highlight"><pre><code class="language-python3"><span class="n">valid</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">fill_</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span> <span class="c1"># 真实标签,都是1</span>
<span class="n">fake</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">fill_</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span> <span class="c1"># 假标签,都是0</span>
<span class="c1">#----------</span>
<span class="c1"># 训练判别器</span>
<span class="c1">#----------</span>
<span class="n">real_imgs</span> <span class="o">=</span> <span class="n">imgs</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">((</span><span class="n">imgs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">100</span><span class="p">))</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span> <span class="c1"># 噪声</span>
<span class="n">gen_imgs</span> <span class="o">=</span> <span class="n">generator</span><span class="p">(</span><span class="n">z</span><span class="p">)</span> <span class="c1"># 从噪声中生成假数据</span>
<span class="n">pred_gen</span> <span class="o">=</span> <span class="n">discriminator</span><span class="p">(</span><span class="n">gen_imgs</span><span class="p">)</span> <span class="c1"># 判别器对假数据的输出</span>
<span class="n">pred_real</span> <span class="o">=</span> <span class="n">discriminator</span><span class="p">(</span><span class="n">real_imgs</span><span class="p">)</span> <span class="c1"># 判别器对真数据的输出</span>
<span class="n">optimizer_D</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span> <span class="c1"># 把判别器中所有参数的梯度归零</span>
<span class="n">real_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">pred_real</span><span class="p">,</span> <span class="n">valid</span><span class="p">)</span> <span class="c1"># 判别器对真实样本的损失</span>
<span class="n">fake_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">pred_gen</span><span class="p">,</span> <span class="n">fake</span><span class="p">)</span> <span class="c1"># 判别器对假样本的损失</span>
<span class="n">d_loss</span> <span class="o">=</span> <span class="p">(</span><span class="n">real_loss</span> <span class="o">+</span> <span class="n">fake_loss</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1"># 两项损失相加取平均</span>
<span class="c1"># 下面这行代码十分重要,将在正文着重讲解</span>
<span class="n">d_loss</span><span class="o">.</span><span class="n">backward</span><span class="p">(</span><span class="n">retain_graph</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="c1"># retain_graph 十分重要,否则计算图内存将会被释放</span>
<span class="n">optimizer_D</span><span class="o">.</span><span class="n">step</span><span class="p">()</span> <span class="c1"># 判别器参数更新</span>
<span class="c1">#---------</span>
<span class="c1">#训练生成器</span>
<span class="c1">#---------</span>
<span class="n">g_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">pred_gen</span><span class="p">,</span> <span class="n">valid</span><span class="p">)</span> <span class="c1"># 生成器的损失函数</span>
<span class="n">optimizer_G</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span> <span class="c1"># 生成器参数梯度归零</span>
<span class="n">g_loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span> <span class="c1"># 生成器的损失函数梯度反向传播</span>
<span class="n">optimizer_G</span><span class="o">.</span><span class="n">step</span><span class="p">()</span> <span class="c1"># 生成器参数更新</span></code></pre></div><p>上面的代码中 d_loss.backward(retain_graph=True) 十分关键,它用于反向传播 discriminator loss 的梯度。那么,具体传播到什么地方去呢?</p><p>这
要看 d_loss 是由哪几部分构成的:real_loss 和 fake_loss,而 fake_loss 又是从 noise 经过
generator 来的。因此,d_loss 反向传播,将一传到底,不但计算了 discriminator 的梯度,同时还计算了
generator 的梯度,虽然这一步optimizer_D.step()只更新 discriminator 的参数。</p><p>也正是这个原因,下面在更新 generator 参数时,要先将生成器参数的梯度重新归零,避免受到 discriminator loss 回传过来的梯度影响。注意:它在反向传播时,设置了<b> retain graph = 0 </b>这个参数。它的作用是<b>保持计算图</b>,因为 pytorch 默认<b>一个计算图只计算一次反向传播,反向传播后,这个计算图的内存就被释放了</b>。而后面的 generator 算梯度时还要用到这个计算图,所以用这个参数控制计算图不被释放。</p><p>generator
的 损失在回传时,同样要经过 discriminator 网络才能传递回自身(系统从输入噪声到 Discriminator
输出,从头到尾只有一次前向传播,而有两次反向传播,故在第一次反向传播时,要保持计算图不被释放)。因此,在回传梯度时,同样也计算了一遍
discriminator 的参数梯度,只不过这次 discriminator 的参数不更新,只更新 generator 的参数,即
optimizer_G.step()。同时,我们看到,下一个 step 首先将 discriminator 的梯度重置为 0,就是为了防止
generator loss 反向传播时顺带计算的梯度对其造成影响(还有上一步 discriminator loss 回传时累积的梯度)。</p><p>综
上,我们看到,为了完成一步参数更新,我们进行了两次反向传播,第一次反向传播为了更新 discriminator 的参数,但多余计算了
generator 的梯度。第二次反向传播为了更新 generator 的参数,但是不得不多计算 discriminator 的梯度。</p><p class="ztext-empty-paragraph"><br></p><p><b>对于先更新生成器参数的情况</b>,我们也分析其循环中一个 step 的代码:</p><div class="highlight"><pre><code class="language-python3"><span class="n">valid</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">fill_</span><span class="p">(</span><span class="mf">1.0</span><span class="p">),</span> <span class="n">requires_grad</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="c1"># 真实样本的标签,都是 1</span>
<span class="n">fake</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">fill_</span><span class="p">(</span><span class="mf">0.0</span><span class="p">),</span> <span class="n">requires_grad</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="c1"># 生成样本的标签,都是 0</span>
<span class="n">real_imgs</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">type</span><span class="p">(</span><span class="n">Tensor</span><span class="p">))</span>
<span class="c1">#-----------</span>
<span class="c1"># 训练生成器</span>
<span class="c1">#-----------</span>
<span class="n">optimizer_G</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span> <span class="c1"># 生成器参数梯度归零</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">Variable</span><span class="p">(</span><span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="n">imgs</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">opt</span><span class="o">.</span><span class="n">latent_dim</span><span class="p">))))</span> <span class="c1"># 噪声</span>
<span class="n">gen_imgs</span> <span class="o">=</span> <span class="n">generator</span><span class="p">(</span><span class="n">z</span><span class="p">)</span> <span class="c1"># 根据噪声生成虚假样本</span>
<span class="n">g_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">discriminator</span><span class="p">(</span><span class="n">gen_imgs</span><span class="p">),</span> <span class="n">valid</span><span class="p">)</span> <span class="c1"># 用真实的标签+假样本,计算生成器损失</span>
<span class="n">g_loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span> <span class="c1"># 生成器梯度反向传播,反向传播经过了判别器,故此时判别器参数也有梯度</span>
<span class="n">optimizer_G</span><span class="o">.</span><span class="n">step</span><span class="p">()</span> <span class="c1"># 生成器参数更新,判别器参数虽然有梯度,但是这一步不能更新判别器</span>
<span class="c1">#----------</span>
<span class="c1"># 训练判别器</span>
<span class="c1">#----------</span>
<span class="n">optimizer_D</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span> <span class="c1"># 把生成器损失函数梯度反向传播时,顺带计算的判别器参数梯度清空</span>
<span class="n">real_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">discriminator</span><span class="p">(</span><span class="n">real_imgs</span><span class="p">),</span> <span class="n">valid</span><span class="p">)</span> <span class="c1"># 真样本+真标签:判别器损失</span>
<span class="n">fake_loss</span> <span class="o">=</span> <span class="n">adversarial_loss</span><span class="p">(</span><span class="n">discriminator</span><span class="p">(</span><span class="n">gen_imgs</span><span class="o">.</span><span class="n">detach</span><span class="p">()),</span> <span class="n">fake</span><span class="p">)</span> <span class="c1"># 假样本+假标签:判别器损失</span>
<span class="n">d_loss</span> <span class="o">=</span> <span class="p">(</span><span class="n">real_loss</span> <span class="o">+</span> <span class="n">fake_loss</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1"># 判别器总的损失函数</span>
<span class="n">d_loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span> <span class="c1"># 判别器损失回传</span>
<span class="n">optimizer_D</span><span class="o">.</span><span class="n">step</span><span class="p">()</span> <span class="c1"># 判别器参数更新</span></code></pre></div><p>上述代码先更新生成器参数,再更新判别器参数。那么除了顺序上的区别,和先更新判别器,再更新生成器,还有什么不同?答案是,计算图的遍历次数不同。</p><p>为
了更新生成器参数,用生成器的损失函数计算梯度,然后反向传播,传播图中经过了discriminator,根据链式法则,不得不顺带计算一下判别器的参
数梯度,虽然在这一步不会更新判别器参数。反向传播过后,noise 到 fake image 再到 discriminator
的输出这个前向传播的计算图就被释放掉了,后面也不会再用到。</p><p>接着更新判别器参数,此时注意到,我们输入判别器的是两部分,一部分是真实数
据,另一部分是生成器的输出,也就是假数据。注意观察细节,在判别器前向传播过程,输入的假数据被 detach
了:discriminator(gen_imgs.detach()),detach
的意思是,这个数据和生成它的计算图“脱钩”了,即梯度传到它那个地方就停了,不再继续往前传播(实际上也不会再往前传播了,因为 generator
的计算图在第一次反向传播过后就被释放了)。因此,判别器梯度反向传播,就到它自己身上为止。</p><p>因此,比起第一种策略,要少计算一次 generator 的所有参数的梯度,同时,也不必刻意保存一次计算图,占用不必要的内存。</p><p>但
需要注意的是,在第一种策略中,noise 从 generator 输入,到 discriminator
输出,只经历了一次前向传播,discriminator 端的输出,被用了两次,一次是计算 discriminator 的损失函数,另一次是计算
generator 的损失函数。</p><p>而在第二种策略中,noise 从 generator 输入,到discriminator
输出,计算 generator 损失,回传,这一步更新了 generator 的参数,并释放了计算图。下一步更新 discriminator
的参数时,generator 的输出经过 detach 后,又通过了一遍 discriminator,相当于,generator
的输出前后两次通过了 discriminator ,得到相同的输出。显然,这也是冗余的。</p><p class="ztext-empty-paragraph"><br></p><p>综上,这两段代码各有利弊:</p><p>第
一段代码,好处是 noise 只进行了一次前向传播,缺点是,更新 discriminator 参数时,多计算了一次 generator
的梯度,同时,第一次更新 discriminator 需要保留计算图,保证算 generator loss 时计算图不被销毁。</p><p>第二
段代码,好处是通过先更新 generator ,使更新后的前向传播计算图可以放心被销毁,因此不用保留计算图占用内存。同时,在更新
discriminator 的时候,也不会像上面的那段代码,计算冗余的 generator 的梯度。缺点是,在 discriminator
上,对 generator 的输出算了两次前向传播,第二次又产生了新的计算图(但比第一次的小)。</p><p>一个多计算了一次
generator 梯度,一个多计算一次 discriminator 前向传播。因此,两者差别不大。如果 discriminator
比generator 复杂,那么应该采取第一种策略,如果 discriminator 比 generator
简单,那么应该采取第二种策略,通常情况下,discriminator 要比 generator 简单,故应该采取第二种策略居多。</p><p class="ztext-empty-paragraph"><br></p><p>但是第二种先更新generator,再更新 discriminator 总是给人感觉怪怪得,因为 generator 的更新需要 discriminator 提供准确的 loss 和 gradient,否则岂不是在瞎更新?</p><p class="ztext-empty-paragraph"><br></p><p>还
有一种没提到的策略,noise 从 generator 输入,输出 fake data,然后 detach 一下,随着 true data
一起输入 discriminator,计算 discriminator 损失,并更新 discriminator 参数。接下来,再把没经过
detach 的 fake data 输入到discriminator 中,计算 generator loss,再反向传播梯度,更新
generator 的参数。这种策略,计算了两次 discriminator 梯度,一次 generator 梯度。感觉这种比较符合先更新
discriminator 的习惯。缺点是,之前的 generator 生成的计算图得保留着,直到 discriminator
更新完,再释放。不像策略二,马上用完马上释放。综合来说,还是策略二最好,策略三其次,策略一最差(差在多计算一次 generator
gradient 上,而通常多计算一次 generator gradient 的运算量比多计算一次 discriminator
前向传播的运算量大),因此,detach 还是很有必要的。</p></div></div><div class="ContentItem-time">编辑于 2018-09-06</div><div><div class="Reward"><button class="Reward-activateBtn">开启赞赏</button><div class="Reward-activateHint">赞赏开启后,读者将可以付费支持你的创作。</div></div></div><div class="Post-topicsAndReviewer"><div class="TopicList Post-Topics"><div class="Tag Topic" data-za-detail-view-path-module="TopicItem" data-za-extra-module="{"card":{"content":{"type":"Topic","token":"19607065"}}}"><span class="Tag-content"><a class="TopicLink" href="https://www.zhihu.com/topic/19607065" target="_blank"><div class="Popover"><div id="Popover2-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover2-content">神经网络</div></div></a></span></div><div class="Tag Topic" data-za-detail-view-path-module="TopicItem" data-za-extra-module="{"card":{"content":{"type":"Topic","token":"19559450"}}}"><span class="Tag-content"><a class="TopicLink" href="https://www.zhihu.com/topic/19559450" target="_blank"><div class="Popover"><div id="Popover3-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover3-content">机器学习</div></div></a></span></div><div class="Tag Topic" data-za-detail-view-path-module="TopicItem" data-za-extra-module="{"card":{"content":{"type":"Topic","token":"20070859"}}}"><span class="Tag-content"><a class="TopicLink" href="https://www.zhihu.com/topic/20070859" target="_blank"><div class="Popover"><div id="Popover4-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover4-content">生成对抗网络(GAN)</div></div></a></span></div></div></div><div><div class="Sticky RichContent-actions is-bottom" style=""><div class="ContentItem-actions" data-za-detail-view-path-module="BottomBar" data-za-extra-module="{"card":{"content":{"type":"Post","id":"43843694"}}}"><span><button aria-label="赞同 163 " type="button" disabled="disabled" class="Button VoteButton VoteButton--up"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--TriangleUp VoteButton-TriangleUp" fill="currentColor" viewBox="0 0 24 24" width="10" height="10"><path d="M2 18.242c0-.326.088-.532.237-.896l7.98-13.203C10.572 3.57 11.086 3 12 3c.915 0 1.429.571 1.784 1.143l7.98 13.203c.15.364.236.57.236.896 0 1.386-.875 1.9-1.955 1.9H3.955c-1.08 0-1.955-.517-1.955-1.9z" fill-rule="evenodd"></path></svg></span>赞同 163</button></span><div class="css-qbubgm"><button type="button" class="Button BottomActions-CommentBtn Button--plain Button--withIcon Button--withLabel"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--Comment Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M10.241 19.313a.97.97 0 0 0-.77.2 7.908 7.908 0 0 1-3.772 1.482.409.409 0 0 1-.38-.637 5.825 5.825 0 0 0 1.11-2.237.605.605 0 0 0-.227-.59A7.935 7.935 0 0 1 3 11.25C3 6.7 7.03 3 12 3s9 3.7 9 8.25-4.373 9.108-10.759 8.063z" fill-rule="evenodd"></path></svg></span>56 条评论</button></div><div class="Popover ShareMenu"><div class="ShareMenu-toggler" id="Popover5-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover5-content"><button type="button" class="Button Button--plain Button--withIcon Button--withLabel"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--Share Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M2.931 7.89c-1.067.24-1.275 1.669-.318 2.207l5.277 2.908 8.168-4.776c.25-.127.477.198.273.39L9.05 14.66l.927 5.953c.18 1.084 1.593 1.376 2.182.456l9.644-15.242c.584-.892-.212-2.029-1.234-1.796L2.93 7.89z" fill-rule="evenodd"></path></svg></span>分享</button></div></div><button type="button" class="Button ContentItem-action Button--plain Button--withIcon Button--withLabel"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--Heart Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M2 8.437C2 5.505 4.294 3.094 7.207 3 9.243 3 11.092 4.19 12 6c.823-1.758 2.649-3 4.651-3C19.545 3 22 5.507 22 8.432 22 16.24 13.842 21 12 21 10.158 21 2 16.24 2 8.437z" fill-rule="evenodd"></path></svg></span>喜欢</button><button type="button" class="Button ContentItem-action Button--plain Button--withIcon Button--withLabel"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--Star Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M5.515 19.64l.918-5.355-3.89-3.792c-.926-.902-.639-1.784.64-1.97L8.56 7.74l2.404-4.871c.572-1.16 1.5-1.16 2.072 0L15.44 7.74l5.377.782c1.28.186 1.566 1.068.64 1.97l-3.89 3.793.918 5.354c.219 1.274-.532 1.82-1.676 1.218L12 18.33l-4.808 2.528c-1.145.602-1.896.056-1.677-1.218z" fill-rule="evenodd"></path></svg></span>收藏</button><div class="Post-ActionMenuButton"><div class="Popover"><div id="Popover6-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover6-content"><button type="button" class="Button Button--plain Button--withIcon Button--withLabel"><span style="display:inline-flex;align-items:center"><svg class="Zi Zi--Settings Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M20.868 17.185a.896.896 0 0 1-.452.137c-.123 0-1.397-.26-1.617-.233-1.354.014-1.78 1.276-1.835 1.742-.055.453 0 .892.191 1.303a.8.8 0 0 1-.068.851C16.224 21.877 14.922 22 14.73 22a.548.548 0 0 1-.356-.151c-.11-.096-.685-1.138-1.069-1.468-1.304-.955-2.247-.329-2.63 0-.398.33-.672.7-.836 1.125a.632.632 0 0 1-.329.37c-1.354.426-2.918-.919-3.014-1.056a.564.564 0 0 1-.123-.356c-.014-.138.383-1.276.342-1.688-.342-1.9-1.836-1.687-2.096-1.673a3.192 3.192 0 0 0-.918.178.873.873 0 0 1-.59-.055c-.887-.462-1.136-2.332-1.109-2.51.055-.315.192-.521.438-.604.425-.164.809-.452 1.151-.85.931-1.262.343-2.25 0-2.634-.342-.356-.726-.645-1.15-.809-.138-.041-.234-.151-.33-.316-.38-1.434.613-2.552.867-2.77.255-.22.6-.055.723 0 .425.164.877.219 1.343.15C6.7 6.636 6.784 5.141 6.81 4.908c.014-.247-.11-1.29-.137-1.4a.488.488 0 0 1 .027-.315C7.317 2.178 9.071 2 9.222 2a.56.56 0 0 1 .439.178c.11.124.63 1.111 1 1.4.4.338 1.583.83 2.59.013.397-.274.959-1.29 1.082-1.413A.55.55 0 0 1 14.717 2c1.56 0 2.329 1.029 2.438 1.22a.458.458 0 0 1 .069.371c-.028.151-.329 1.152-.26 1.605.365 1.537 1.383 1.742 1.89 1.783.493.028 1.644-.356 1.809-.343a.63.63 0 0 1 .424.206c.535.31.85 1.715.905 2.14.027.233-.014.439-.11.562-.11.138-1.165.714-1.48 1.112-.855.982-.342 2.25-.068 2.606.26.37 1.22.905 1.288.96.15.137.26.302.315.494.146 1.413-.89 2.387-1.069 2.47zm-8.905-.535c.644 0 1.246-.123 1.822-.356a4.576 4.576 0 0 0 1.493-1.016 4.694 4.694 0 0 0 1-1.495c.247-.562.357-1.18.357-1.81 0-.659-.11-1.262-.356-1.825a4.79 4.79 0 0 0-1-1.481 4.542 4.542 0 0 0-1.494-1.002 4.796 4.796 0 0 0-3.631 0 4.627 4.627 0 0 0-1.48 1.002c-.424.425-.767.919-1 1.481a4.479 4.479 0 0 0-.37 1.825c0 .644.124 1.248.37 1.81a4.62 4.62 0 0 0 1 1.495c.425.426.918.768 1.48 1.016a4.677 4.677 0 0 0 1.809.356z" fill-rule="evenodd"></path></svg></span>设置</button></div></div></div></div></div></div></article><div class="Post-Sub Post-NormalSub"><div class="PostIndex-Contributions" data-za-detail-view-path-module="ColumnList" data-za-detail-view-path-module_name="文章被以下专栏收录" data-za-extra-module="{}"><h3 class="BlockTitle">文章被以下专栏收录</h3><ul><div class="ContentItem Column-ColumnItem"><div class="ContentItem-main"><div class="ContentItem-image"><a class="ColumnLink" href="https://www.zhihu.com/column/c_80412427"><div class="Popover"><div id="Popover7-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover7-content"><img class="Avatar Avatar--medium Avatar--round" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-0b1fe24b358b429d8e0dd4a5d01750da_xs.jpg" srcset="https://pic4.zhimg.com/v2-0b1fe24b358b429d8e0dd4a5d01750da_l.jpg?source=172ae18b 2x" alt="人工智能学习笔记" width="40" height="40"></div></div></a></div><div class="ContentItem-head"><h2 class="ContentItem-title"><a class="ColumnLink ColumnItem-Title" href="https://www.zhihu.com/column/c_80412427"><div class="Popover"><div id="Popover8-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover8-content">人工智能学习笔记</div></div></a> · <button class="ColumnItem-CancleButton"><span class="status">投稿中</span><span class="cancel">撤销投稿</span></button></h2><div class="ContentItem-meta">为你打造属于你学习人工智能的最佳学习路径</div></div><div class="ContentItem-extra"><button type="button" class="Button FollowButton Button--primary Button--grey">已关注</button></div></div></div><div class="ContentItem Column-ColumnItem"><div class="ContentItem-main"><div class="ContentItem-image"><a class="ColumnLink" href="https://www.zhihu.com/column/c_167341220"><div class="Popover"><div id="Popover9-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover9-content"><img class="Avatar Avatar--medium Avatar--round" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-db630cba4a5dcd60ccd306633b89aa93_xs.jpg" srcset="https://pic1.zhimg.com/v2-db630cba4a5dcd60ccd306633b89aa93_l.jpg?source=172ae18b 2x" alt="轮子工厂" width="40" height="40"></div></div></a></div><div class="ContentItem-head"><h2 class="ContentItem-title"><a class="ColumnLink ColumnItem-Title" href="https://www.zhihu.com/column/c_167341220"><div class="Popover"><div id="Popover10-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover10-content">轮子工厂</div></div></a></h2><div class="ContentItem-meta">微信公众号:轮子工厂,6万多人的选择,值得关注!</div></div><div class="ContentItem-extra"><button type="button" class="Button FollowButton Button--primary Button--grey">已关注</button></div></div></div><div class="ContentItem Column-ColumnItem"><div class="ContentItem-main"><div class="ContentItem-image"><a class="ColumnLink" href="https://www.zhihu.com/column/c_1101886054759501824"><div class="Popover"><div id="Popover11-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover11-content"><img class="Avatar Avatar--medium Avatar--round" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/4b70deef7_xs.jpg" srcset="https://pic4.zhimg.com/4b70deef7_l.jpg?source=172ae18b 2x" alt="计算机视觉学习笔记" width="40" height="40"></div></div></a></div><div class="ContentItem-head"><h2 class="ContentItem-title"><a class="ColumnLink ColumnItem-Title" href="https://www.zhihu.com/column/c_1101886054759501824"><div class="Popover"><div id="Popover12-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover12-content">计算机视觉学习笔记</div></div></a></h2><div class="ContentItem-meta">记录有深度和最新的计算机视觉相关文章!</div></div><div class="ContentItem-extra"><button type="button" class="Button FollowButton Button--primary Button--blue">关注专栏</button></div></div></div><div class="ContentItem Column-ColumnItem"><div class="ContentItem-main"><div class="ContentItem-image"><a class="ColumnLink" href="https://www.zhihu.com/column/c_1293925349546061824"><div class="Popover"><div id="Popover13-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover13-content"><img class="Avatar Avatar--medium Avatar--round" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/4b70deef7_xs.jpg" srcset="https://pic4.zhimg.com/4b70deef7_l.jpg?source=172ae18b 2x" alt="人人都是计算机视觉算法工程师" width="40" height="40"></div></div></a></div><div class="ContentItem-head"><h2 class="ContentItem-title"><a class="ColumnLink ColumnItem-Title" href="https://www.zhihu.com/column/c_1293925349546061824"><div class="Popover"><div id="Popover14-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover14-content">人人都是计算机视觉算法工程师</div></div></a></h2><div class="ContentItem-meta">最通俗的语言,最完整的知识体系,人人都是CV工程师</div></div><div class="ContentItem-extra"><button type="button" class="Button FollowButton Button--primary Button--blue">关注专栏</button></div></div></div></ul></div><div class="Recommendations-Main" style="width: 1068px;"><h3 class="BlockTitle Recommendations-BlockTitle">推荐阅读</h3><ul class="Recommendations-List"><button class="PagingButton PagingButton-Previous" disabled="disabled" data-za-detail-view-path-module="Unknown" data-za-detail-view-path-module_name="推荐阅读" data-za-extra-module="{}"><svg class="Zi Zi--ArrowLeft" fill="#d3d3d3" viewBox="0 0 24 24" width="40" height="40"><path d="M14.782 16.78a.737.737 0 0 1-1.052 0L9.218 12.53a.758.758 0 0 1 0-1.063L13.73 7.22a.737.737 0 0 1 1.052 0c.29.294.29.77.001 1.063L11 12l3.782 3.716c.29.294.29.77 0 1.063z" fill-rule="evenodd"></path></svg></button><a href="https://zhuanlan.zhihu.com/p/92839469" class="PostItem"><div><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-029ed72380b1c75f5b2788d59ce45be5_250x0.jpg" srcset="https://pic4.zhimg.com/v2-029ed72380b1c75f5b2788d59ce45be5_qhd.jpg?source=172ae18b 2x" class="PostItem-TitleImage" alt="让算法拥有想象力的cycleGAN(二)pytorch实现"><h1 class="PostItem-Title">让算法拥有想象力的cycleGAN(二)pytorch实现</h1><div class="PostItem-Footer"><span>机器学习入坑者</span><span class="PostItem-FooterTitle"></span></div></div></a><a href="https://zhuanlan.zhihu.com/p/52233472" class="PostItem"><div><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-5ca6a701d92341b8357830cc176fb8a3_250x0.jpg" srcset="https://pic2.zhimg.com/v2-5ca6a701d92341b8357830cc176fb8a3_qhd.jpg?source=172ae18b 2x" class="PostItem-TitleImage" alt="生成对抗网络GAN(一):基本概念与算法流程"><h1 class="PostItem-Title">生成对抗网络GAN(一):基本概念与算法流程</h1><div class="PostItem-Footer"><span>Slumbers</span><span class="PostItem-FooterTitle"></span></div></div></a><a href="https://zhuanlan.zhihu.com/p/77987041" class="PostItem"><div><h1 class="PostItem-Title">GAN系列 - 使用50行PyTorch代码构建对抗生成网络 (GANs)</h1><p class="PostItem-Summary">2014年,Ian Goodfellow和他在University of Montreal 的同事们发表了一篇令人惊叹的论文,向世界介绍了对抗生成网络, GANs。通过计算图和博弈论的创新组合,他们表明,如果有足够的建模能…</p><div class="PostItem-Footer"><span>巧克力工厂...</span><span class="PostItem-FooterTitle">发表于开发工程师...</span></div></div></a><a href="https://zhuanlan.zhihu.com/p/91130185" class="PostItem"><div><img src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-e4a79435c8b7c2ef6fc2f449e2563d65_250x0.jpg" srcset="https://pic2.zhimg.com/v2-e4a79435c8b7c2ef6fc2f449e2563d65_qhd.jpg?source=172ae18b 2x" class="PostItem-TitleImage" alt="CGAN原理分析与pytorch实现"><h1 class="PostItem-Title">CGAN原理分析与pytorch实现</h1><div class="PostItem-Footer"><span>机器学习入坑者</span><span class="PostItem-FooterTitle"></span></div></div></a><button class="PagingButton PagingButton-Next" data-za-detail-view-path-module="Unknown" data-za-detail-view-path-module_name="推荐阅读" data-za-extra-module="{}"><svg class="Zi Zi--ArrowRight" fill="#d3d3d3" viewBox="0 0 24 24" width="40" height="40"><path d="M9.218 16.78a.737.737 0 0 0 1.052 0l4.512-4.249a.758.758 0 0 0 0-1.063L10.27 7.22a.737.737 0 0 0-1.052 0 .759.759 0 0 0-.001 1.063L13 12l-3.782 3.716a.758.758 0 0 0 0 1.063z" fill-rule="evenodd"></path></svg></button></ul></div><div class="Comments-container" data-za-detail-view-path-module="CommentList" data-za-extra-module="{}"><div class="CommentsV2 CommentsV2--withEditor CommentsV2-withPagination"><div class="Topbar CommentTopbar"><div class="Topbar-title"><h2 class="CommentTopbar-title">56 条评论</h2></div><div class="Topbar-options"><button type="button" class="Button Button--plain Button--withIcon Button--withLabel"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Switch Button-zi" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M13.004 7V4.232c0-.405.35-.733.781-.733.183 0 .36.06.501.17l6.437 5.033c.331.26.376.722.1 1.033a.803.803 0 0 1-.601.264H2.75a.75.75 0 0 1-.75-.75V7.75A.75.75 0 0 1 2.75 7h10.254zm-1.997 9.999v2.768c0 .405-.35.733-.782.733a.814.814 0 0 1-.5-.17l-6.437-5.034a.702.702 0 0 1-.1-1.032.803.803 0 0 1 .6-.264H21.25a.75.75 0 0 1 .75.75v1.499a.75.75 0 0 1-.75.75H11.007z" fill-rule="evenodd"></path></svg></span>切换为时间排序</button></div></div><div><div class="CommentsV2-footer CommentEditorV2--normal"><div class="CommentEditorV2-inputWrap"><div class="InputLike CommentEditorV2-input Editable"><div style="min-height: 198px;" class="Dropzone Editable-content RichText RichText--editable RichText--clearBoth ztext"><div class="DraftEditor-root"><div class="public-DraftEditorPlaceholder-root"><div class="public-DraftEditorPlaceholder-inner" id="placeholder-7c5jo" style="white-space: pre-wrap;">写下你的评论...</div></div><div class="DraftEditor-editorContainer"><div aria-describedby="placeholder-7c5jo" class="notranslate public-DraftEditor-content" role="textbox" spellcheck="true" style="outline: currentcolor none medium; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;" tabindex="0" contenteditable="true"><div data-contents="true"><div class="Editable-unstyled" data-block="true" data-editor="7c5jo" data-offset-key="1gdtb-0-0"><div data-offset-key="1gdtb-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="1gdtb-0-0"><br data-text="true"></span></div></div></div></div></div></div></div><input multiple="" type="file" style="display: none;" accept="image/webp,image/jpg,image/jpeg,image/png,image/gif"><div></div></div><div class="CommentEditorV2-inputUpload"><div class="CommentEditorV2-popoverWrap"><div class="Popover ImagePopover"><button aria-label="插入图片" data-tooltip="插入图片" data-tooltip-position="bottom" data-tooltip-will-hide-on-click="true" id="Popover25-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover25-content" type="button" class="Button Editable-control Button--plain"><svg class="Zi Zi--AddImage" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M13.25 5.5H6.868c-.755 0-1.368.613-1.368 1.368v10.264c0 .755.613 1.368 1.368 1.368h10.264c.755 0 1.368-.613 1.368-1.368V10.75H20v6.467A2.783 2.783 0 0 1 17.217 20H6.783A2.783 2.783 0 0 1 4 17.217V6.783A2.783 2.783 0 0 1 6.783 4h6.467v1.5zM8.158 13.4a1.033 1.033 0 0 1 1.744-.078l.258.37a.782.782 0 0 0 1.303-.031l1.332-2.113a1.173 1.173 0 0 1 2.01.043l2.03 3.537A1.25 1.25 0 0 1 15.748 17H8.25a1.252 1.252 0 0 1-1.085-1.873l.992-1.728zM9.5 11a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3zm3.75-5.5a.75.75 0 1 1 0-1.5.75.75 0 0 1 0 1.5zM18.5 4V2.25a.75.75 0 1 1 1.5 0V4h1.75a.75.75 0 1 1 0 1.5H20v1.75a.75.75 0 1 1-1.5 0V5.5h-1.75a.75.75 0 1 1 0-1.5h1.75zm.75 7.5a.75.75 0 1 1 0-1.5.75.75 0 0 1 0 1.5z"></path></svg></button></div><label class="UploadPicture-wrapper"><input type="file" accept="image/png,image/jpeg" class="UploadPicture-input"></label><div class="Popover CommentEditorV2-inputUpLoad-Icon"><button aria-label="插入表情" data-tooltip="插入表情" data-tooltip-position="bottom" data-tooltip-will-hide-on-click="true" id="Popover17-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover17-content" type="button" class="Button Editable-control Button--plain"><svg class="Zi Zi--Emotion" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M7.523 13.5h8.954c-.228 2.47-2.145 4-4.477 4-2.332 0-4.25-1.53-4.477-4zM12 21a9 9 0 1 1 0-18 9 9 0 0 1 0 18zm0-1.5a7.5 7.5 0 1 0 0-15 7.5 7.5 0 0 0 0 15zm-3-8a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3zm6 0a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3z"></path></svg></button></div></div></div></div><button type="button" disabled="disabled" class="Button CommentEditorV2-singleButton Button--primary Button--blue">发布</button></div></div><div><div class="CommentListV2"><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover26-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover26-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/champagnejin"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-f080ba2ee4c2b05f279ad55ab683f9d9_s.jpg" srcset="https://pic1.zhimg.com/v2-f080ba2ee4c2b05f279ad55ab683f9d9_xs.jpg?source=06d4cd63 2x" alt="ChampagneJin" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/champagnejin">ChampagneJin</a></span><span class="CommentItemV2-time">2019-02-23</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>感谢作者!终于解开了长久的疑惑!</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover27-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover27-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s_002.jpg" srcset="https://pic2.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/champagnejin">ChampagneJin</a></span><span class="CommentItemV2-time">2019-02-23</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">嘻嘻,互相交流</div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover28-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover28-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-cai-42-43"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_003.jpg" srcset="https://pic4.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="旺财" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-cai-42-43">旺财</a></span><span class="CommentItemV2-time">2019-03-21</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">请教作者一个问题。关于第一段代码(先更新判别器D),当判别器D更新完成后,D的模型参数的梯度是不是还存在?如果存在的话,这些梯度会不会影响到后续G的更新?</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover29-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover29-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s.jpg" srcset="https://pic1.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-cai-42-43">旺财</a></span><span class="CommentItemV2-time">2019-03-21</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">通过设置retain_graph=True故意保留的,因为更新g时需要用到d的输出。</div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover30-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover30-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-cai-42-43"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s.jpg" srcset="https://pic2.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="旺财" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-cai-42-43">旺财</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-time">2019-03-21</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">谢
谢回复!还是有些不明白,生成器和判别器这条支路上会有两次backward,第一次用dloss计算梯度更新判别器。第二次基于gloss计算梯度,此
时判别器中还保留着基于dloss的梯度,按照梯度累加的原则,判别器会把dloss和dloss的梯度累加起来,并回传更新生成器,不知道是不是这样?</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">展开其他 3 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover31-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover31-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-jin-xin-25-48"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-509b312c99a34c895b524d3f68cfaee7_s.jpg" srcset="https://pic4.zhimg.com/v2-509b312c99a34c895b524d3f68cfaee7_xs.jpg?source=06d4cd63 2x" alt="目标跟踪小虾" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-jin-xin-25-48">目标跟踪小虾</a></span><span class="CommentItemV2-time">2019-04-01</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>谢谢,您写得很仔细,我学到了很好的东西。这种干货的分析太棒了!</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover32-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover32-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/smuh"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/1c472e935_s.jpg" srcset="https://pic3.zhimg.com/1c472e935_xs.jpg?source=06d4cd63 2x" alt="kozo" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/smuh">kozo</a></span><span class="CommentItemV2-time">2019-04-02</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>请问楼主,在第一种策略中,optimizer_D.step()后已经更新了D网络的参数,此时不是更应该按照新的D参数,重新计算pred_gen,然后再更新G网络?策略一只计算一次pred_gen,是不是有些不对?</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover33-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover33-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s.jpg" srcset="https://pic1.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/smuh">kozo</a></span><span class="CommentItemV2-time">2019-04-02</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">不用那么精细,在很多实现中,都是更新好几次discriminator才更新一次generator的</div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover34-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover34-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/heeger-7"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-679f06f822220cec47d79b5d9b301a73_s.jpg" srcset="https://pic2.zhimg.com/v2-679f06f822220cec47d79b5d9b301a73_xs.jpg?source=06d4cd63 2x" alt="Heeger" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/heeger-7">Heeger</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-time">2020-03-22</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>按照GAN的理论来说,第一步更新许多次D是为了计算好两个分布的divergence,这样才能保证G的梯度是朝着减小divergence的方向走的,所以理论上确实应该在更新完D时候重新算一下pred_gen才对啊</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">展开其他 3 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover35-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover35-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/tjuxjh"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_002.jpg" srcset="https://pic1.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="信工吴彦祖" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/tjuxjh">信工吴彦祖</a></span><span class="CommentItemV2-time">2019-05-05</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">也
正是这个原因,下面在更新 generator 参数时,要先将生成器参数的梯度重新归零,避免受到 discriminator loss
回传过来的梯度影响。注意:它在反向传播时,设置了 retain graph = 0 这个参数。它的作用是保持计算图,因为 pytorch
默认一个计算图只计算一次反向传播,反向传播后,这个计算图的内存就被释放了。<br>Retain graph = 0? 我理解是不是retain graph =True</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover36-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover36-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/li-yue-zhi-74"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-4ceebf2b1550da9771ac31a9a11e6af2_s.jpg" srcset="https://pic4.zhimg.com/v2-4ceebf2b1550da9771ac31a9a11e6af2_xs.jpg?source=06d4cd63 2x" alt="节能君" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/li-yue-zhi-74">节能君</a></span><span class="CommentItemV2-time">2019-06-24</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>shide</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover37-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover37-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/zhang-han-xiao-6"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-b7dcecbfa1a0812607c7c2b6032e317b_s_002.jpg" srcset="https://pic2.zhimg.com/v2-b7dcecbfa1a0812607c7c2b6032e317b_xs.jpg?source=06d4cd63 2x" alt="Hanx Zhang" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/zhang-han-xiao-6">Hanx Zhang</a></span><span class="CommentItemV2-time">2019-08-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">请问在文章中 先训练判别器时<br>pred_gen = discriminator(gen_imgs) # 判别器对假数据的输出<br><br>这里的gen_imgs后面为什么不加detach?</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover38-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover38-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s.jpg" srcset="https://pic1.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/zhang-han-xiao-6">Hanx Zhang</a></span><span class="CommentItemV2-time">2019-08-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">因为它的梯度要传递到generator</div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover39-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover39-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/zhang-han-xiao-6"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-b7dcecbfa1a0812607c7c2b6032e317b_s.jpg" srcset="https://pic4.zhimg.com/v2-b7dcecbfa1a0812607c7c2b6032e317b_xs.jpg?source=06d4cd63 2x" alt="Hanx Zhang" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/zhang-han-xiao-6">Hanx Zhang</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-time">2019-08-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">emm,
梳理一下,在先训D时,是否只希望D的loss反向传播不影响到G (就像先训练G的情况一样)?假设以上成立,是否retain
graph在先训练D时也可以不需要了,因为D loss没有经过G,计算图不统一 (就像先训练G的情况下,G loss也没有 retain
graph,因为D loss 用不到 G loss梯度的计算图)</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">查看全部 14 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover40-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover40-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-4983043dc0637b56d236577f9cfa289c_s_002.jpg" srcset="https://pic1.zhimg.com/v2-4983043dc0637b56d236577f9cfa289c_xs.jpg?source=06d4cd63 2x" alt="Eree Bay" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang">Eree Bay</a></span><span class="CommentItemV2-time">2019-09-04</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>所以retaingraph和detach最好都用上,这样既可以在优化D的时候避免计算了多余的G的梯度,同时在优化G的时候避免重新构建一次计算图。这样理解是么。</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover41-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover41-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-4983043dc0637b56d236577f9cfa289c_s.jpg" srcset="https://pic2.zhimg.com/v2-4983043dc0637b56d236577f9cfa289c_xs.jpg?source=06d4cd63 2x" alt="Eree Bay" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang">Eree Bay</a></span><span class="CommentItemV2-time">2019-09-04</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>刚才说错了,用了detach的话graph应该就不一样了就没必要retain了</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover42-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover42-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_002.jpg" srcset="https://pic1.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="幽雎" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang">Eree Bay</a></span><span class="CommentItemV2-time">2020-06-12</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>这个正确。</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover43-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover43-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-4983043dc0637b56d236577f9cfa289c_s_003.jpg" srcset="https://pic4.zhimg.com/v2-4983043dc0637b56d236577f9cfa289c_xs.jpg?source=06d4cd63 2x" alt="Eree Bay" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/jia-xin-kang">Eree Bay</a></span><span class="CommentItemV2-time">2019-09-04</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>策略三是目前比较主流的实现方式,策略二直接和算法上的顺序就冲突了,对最后收敛效果应该有影响</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover44-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover44-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/chen-min-yu-76"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-11a8e1f0930c53d0593663af9b820bcf_s.jpg" srcset="https://pic2.zhimg.com/v2-11a8e1f0930c53d0593663af9b820bcf_xs.jpg?source=06d4cd63 2x" alt="SamuelYooooo" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/chen-min-yu-76">SamuelYooooo</a></span><span class="CommentItemV2-time">2019-10-19</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>在第一段代码里(i.e. 先train D),也可以直接gen_imgs.detach()。</p><p class="ztext-empty-paragraph"><br></p><p>对于具体先train G还是D,理论上没有区别。实际做起来,先D比较好,而且每个iteration一般D需要多train几次。</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn is-liked Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>2</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover45-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover45-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/eternity-73-15"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-ad6f2f64f98554517eda63273ca25700_s_002.jpg" srcset="https://pic2.zhimg.com/v2-ad6f2f64f98554517eda63273ca25700_xs.jpg?source=06d4cd63 2x" alt="eternity" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/eternity-73-15">eternity</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/chen-min-yu-76">SamuelYooooo</a></span><span class="CommentItemV2-time">2020-11-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">请问,gen_img加了detach以后,更新判别器D的时候只会释放从gen_img到判别器D输出这一段的计算图,而生成器G的计算图可以保留下来,对吧?</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover46-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover46-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/ac130-10"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/c4f3eeaf83f56dff405e6488d6d59672_s.jpg" srcset="https://pic1.zhimg.com/c4f3eeaf83f56dff405e6488d6d59672_xs.jpg?source=06d4cd63 2x" alt="ac130" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/ac130-10">ac130</a></span><span class="CommentItemV2-time">2019-11-29</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">感谢作者!写的相当清楚</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover47-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover47-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/du-lun-83"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_004.jpg" srcset="https://pic3.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="luke28" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/du-lun-83">luke28</a></span><span class="CommentItemV2-time">2019-11-29</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>感谢作者!!请问能把你解释的代码的全部源码的地址分享一下吗?</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover48-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover48-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qu-shi-62-9"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/c6157d91b69f8ad3190f65b1b142a040_s.jpg" srcset="https://pic4.zhimg.com/c6157d91b69f8ad3190f65b1b142a040_xs.jpg?source=06d4cd63 2x" alt="区士" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qu-shi-62-9">区士</a></span><span class="CommentItemV2-time">2020-04-30</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>感谢作者,对于GAN的训练过程真的是豁然开朗</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover49-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover49-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_003.jpg" srcset="https://pic4.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="幽雎" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-time">2020-06-12</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>谢谢作者,研究得很仔细,收获很大。看有的代码在交替训练D和G时都把另外一个梯度更新设置为False,有的代码却没这么设置,不太明白,什么时候需要这样特殊设置呢?</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover50-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover50-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/376092596"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/b1531e46a92b6c7a366f9b59d2c7d066_s.jpg" srcset="https://pic1.zhimg.com/b1531e46a92b6c7a366f9b59d2c7d066_xs.jpg?source=06d4cd63 2x" alt="totty" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/376092596">totty</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-time">2020-07-21</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>你
看到的策略应该不包含在作者提到的这三种里面。如果生成器和判别器只有梯度是前后回传,参数更新发生在每次循环的结尾
“optimizer_G.step(); optimizer_D.step()
”,需要在G梯度回传(g_loss.backward())前将D的梯度更新设置为False,避免梯度流入D。</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover51-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover51-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_003.jpg" srcset="https://pic4.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="幽雎" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-time">2020-08-18</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>谢
谢作者分享,收获很大。但是实际中碰到一个很奇怪的问题。训练中,我采用D训练2次,G训练1次的设置,在更新D和G中都重新定义了计算图。在更新D时,
对假的数据设置了detach(), backward()时没有设置retaingraph=True.
奇怪的是第一次更新G和D时都正常,但第二次迭代到更新D时就提示backward()让把retain_graph
设置为True.。同样第二次迭代更新G时也提示backward()让把retain_graph 设置为True. 不知道是什么原因呢?
恳请指点。</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover52-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover52-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s.jpg" srcset="https://pic1.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-time">2020-08-18</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>能不能用 minst 写个 demo 复现你遇到的问题然后发给我?隔空 debug 有点难……</p></div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover53-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover53-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_003.jpg" srcset="https://pic4.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="幽雎" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/qiao-dong-jia-99">幽雎</a></span><span class="CommentItemV2-time">2020-08-18</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">谢谢大佬,我把鉴别器模型下定义了个计算G和D损失的函数,更新G和D时调用一下直接得到对应的函数得到损失值,然后反向传播就好了,和MUNIT的那篇文章一样,不过还是不太明白为什么。</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">展开其他 1 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover54-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover54-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_004.jpg" srcset="https://pic3.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="夏虫不语冰" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12">夏虫不语冰</a></span><span class="CommentItemV2-time">2020-10-19</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>感谢题主,我还有个困惑想请教</p><p>相
比于我用detach或retaingragh操作,假如我延用先训练D再训G的思路,那么我在训练D的时候,直接让G的所有参数不求梯度
(requires_grad=False),尽让D的参数求梯度(requires_grad=True);而后在训练G的时候,我仅对G所有参数求梯
度(requires_grad=True),而对D的所有参数不求梯度(requires_grad=False)。这样的话有木有什么问题呢</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover55-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover55-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/bf28449bb_s_002.jpg" srcset="https://pic2.zhimg.com/bf28449bb_xs.jpg?source=06d4cd63 2x" alt="杨指北" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12">夏虫不语冰</a></span><span class="CommentItemV2-time">2020-10-19</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">训练g的时候,g的adversarial loss 梯度是由d反传回去的,设置d的参数 requires grad为 False,会导致 loss 的梯度传播不回去。事实上,这样设置后,调用 loss.backward() 会报错,</div></div><div class="CommentItemV2-footer"><button type="button" disabled="disabled" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>1</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover56-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover56-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_004.jpg" srcset="https://pic3.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="夏虫不语冰" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12">夏虫不语冰</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/yin-xiang-nan">杨指北</a></span><span class="CommentItemV2-roleInfo"> (作者) </span><span class="CommentItemV2-time">2020-10-23</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>感谢感谢,那还有一个问题</p><p>依旧是先训练D再训练G,看到还有代码实现,在训练D的时候,分别对loss_D_real和loss_D_fake进行backward调用,和题主给的把两个loss相加后再进行backward,这两种方式之间又有什么区别不</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">展开其他 1 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootComment"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover57-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover57-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-chui-33-37"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-96df877cbfa53b9fb7e66f11ee0c269e_s.jpg" srcset="https://pic1.zhimg.com/v2-96df877cbfa53b9fb7e66f11ee0c269e_xs.jpg?source=06d4cd63 2x" alt="Frank Wang" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-chui-33-37">Frank Wang</a></span><span class="CommentItemV2-time">2020-10-26</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>作者你好,我在github上看到别人的Wgan-gp代码<a href="http://link.zhihu.com/?target=https%3A//github.com/eriklindernoren/PyTorch-GAN/blob/master/implementations/wgan_gp/wgan_gp.py%23L173" class=" external" target="_blank" rel="nofollow noreferrer"><span class="invisible">https://</span><span class="visible">github.com/eriklinderno</span><span class="invisible">ren/PyTorch-GAN/blob/master/implementations/wgan_gp/wgan_gp.py#L173</span><span class="ellipsis"></span></a> 先D后G训练,其中loss_D.backward()并没有添加retain_graph=True这个参数,而且我运行过也不报错,请问是怎么回事呢</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover58-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover58-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/da8e974dc_s_003.jpg" srcset="https://pic4.zhimg.com/da8e974dc_xs.jpg?source=06d4cd63 2x" alt="夏虫不语冰" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12">夏虫不语冰</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-chui-33-37">Frank Wang</a></span><span class="CommentItemV2-time">2020-10-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>这个我知道,因为在训G的时候,又重新运行了fake_imgs = generator(z),因此计算图此时又是完整的了,就可以G.backward()了</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><li class="NestComment--child"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover59-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover59-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-chui-33-37"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-96df877cbfa53b9fb7e66f11ee0c269e_s.jpg" srcset="https://pic1.zhimg.com/v2-96df877cbfa53b9fb7e66f11ee0c269e_xs.jpg?source=06d4cd63 2x" alt="Frank Wang" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/wang-da-chui-33-37">Frank Wang</a></span><span class="CommentItemV2-reply">回复</span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/xia-chong-bu-yu-bing-12">夏虫不语冰</a></span><span class="CommentItemV2-time">2020-10-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext"><p>就是当训练G的时候,如果有fake_imgs=G(z),loss-D.backward()不需要这个参数,对吗?</p></div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li><div><div class="CommentMoreReplyButton"><button type="button" class="Button Button--plain">展开其他 1 条回复</button></div></div></ul><ul class="NestComment"><li class="NestComment--rootCommentNoChild"><div class="CommentItemV2"><div><div class="CommentItemV2-meta"><span class="UserLink CommentItemV2-avatar"><div class="Popover"><div id="Popover60-toggle" aria-haspopup="true" aria-expanded="false" aria-owns="Popover60-content"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/eternity-73-15"><img class="Avatar UserLink-avatar" src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/v2-ad6f2f64f98554517eda63273ca25700_s.jpg" srcset="https://pic1.zhimg.com/v2-ad6f2f64f98554517eda63273ca25700_xs.jpg?source=06d4cd63 2x" alt="eternity" width="24" height="24"></a></div></div></span><span class="UserLink"><a class="UserLink-link" data-za-detail-view-element_name="User" target="_blank" href="https://www.zhihu.com/people/eternity-73-15">eternity</a></span><span class="CommentItemV2-time">2020-11-27</span></div><div class="CommentItemV2-metaSibling"><div class="CommentRichText CommentItemV2-content"><div class="RichText ztext">请问,gen_img加了detach以后,先更新判别器D的时候只会释放从gen_img到判别器D输出这一段的计算图,而生成器G的计算图可以保留下来,对吧?</div></div><div class="CommentItemV2-footer"><button type="button" class="Button CommentItemV2-likeBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>赞</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Reply" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M22.959 17.22c-1.686-3.552-5.128-8.062-11.636-8.65-.539-.053-1.376-.436-1.376-1.561V4.678c0-.521-.635-.915-1.116-.521L1.469 10.67a1.506 1.506 0 0 0-.1 2.08s6.99 6.818 7.443 7.114c.453.295 1.136.124 1.135-.501V17a1.525 1.525 0 0 1 1.532-1.466c1.186-.139 7.597-.077 10.33 2.396 0 0 .396.257.536.257.892 0 .614-.967.614-.967z" fill-rule="evenodd"></path></svg></span>回复</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Recommend" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M6.433 14.285l-.918 5.355c-.219 1.274.532 1.82 1.676 1.218l4.81-2.528 4.807 2.528c1.145.601 1.896.056 1.677-1.218l-.918-5.355 3.89-3.792c.925-.903.64-1.785-.64-1.97l-2.843-.414c-.538.583-4.195 4.547-5.757 6.12-.865.822-1.572.015-1.572.015l-1.92-1.906s-.405-.623.213-1.204c.62-.58 1.174-.193 1.174-.193l1.32 1.14 4.008-4.34-2.404-4.872c-.572-1.159-1.5-1.159-2.072 0L8.56 7.741l-5.376.781c-1.279.186-1.566 1.068-.64 1.971l3.89 3.792z" fill-rule="evenodd"></path></svg></span>推荐</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--EyeSlash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="14" height="14"><path d="M17.007 11.504c0 .65-.13 1.26-.36 1.83l3 3.073S23 14.136 23 11.504C23 8.008 17.255 4 11.995 4c-1.4 0-2.741.25-3.982.701l2.161 2.16c.57-.23 1.18-.36 1.831-.36a5.004 5.004 0 0 1 5.002 5.003zM2.57 4.342l2.067 2.075C3.499 7.258 1 9.119 1 11.504c0 3.336 5.79 7.503 11.005 7.503 1.55 0 3.031-.3 4.382-.84l.42.42 2.125 2.118s.782.571 1.314 0-.074-1.305-.074-1.305L3.955 3.183s-.76-.742-1.385-.19c-.626.554 0 1.35 0 1.35zm4.963 4.96l1.55 1.552c-.05.21-.08.43-.08.65 0 1.66 1.341 3.001 3.002 3.001.22 0 .44-.03.65-.08l1.551 1.551c-.67.33-1.41.53-2.2.53a5.004 5.004 0 0 1-5.003-5.002c0-.79.2-1.53.53-2.201zm4.312-.78l3.151 3.152.02-.16c0-1.66-1.34-3.001-3.001-3.001l-.17.01z" opacity=".9" fill-rule="evenodd"></path></svg></span>折叠</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" style="transform: rotate(180deg); margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M14.445 9h5.387s2.997.154 1.95 3.669c-.168.51-2.346 6.911-2.346 6.911s-.763 1.416-2.86 1.416H8.989c-1.498 0-2.005-.896-1.989-2v-7.998c0-.987.336-2.032 1.114-2.639 4.45-3.773 3.436-4.597 4.45-5.83.985-1.13 3.2-.5 3.037 2.362C15.201 7.397 14.445 9 14.445 9zM3 9h2a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V10a1 1 0 0 1 1-1z" fill-rule="evenodd"></path></svg></span>踩</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Report" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M19.947 3.129c-.633.136-3.927.639-5.697.385-3.133-.45-4.776-2.54-9.949-.888-.997.413-1.277 1.038-1.277 2.019L3 20.808c0 .3.101.54.304.718a.97.97 0 0 0 .73.304c.275 0 .519-.102.73-.304.202-.179.304-.418.304-.718v-6.58c4.533-1.235 8.047.668 8.562.864 2.343.893 5.542.008 6.774-.657.397-.178.596-.474.596-.887V3.964c0-.599-.42-.972-1.053-.835z" fill-rule="evenodd"></path></svg></span>举报</button><button type="button" class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Trash" style="margin-right: 5px;" fill="currentColor" viewBox="0 0 24 24" width="16" height="16"><path d="M16.464 4s.051-2-1.479-2H9C7.194 2 7.465 4 7.465 4H4.752c-2.57 0-2.09 3.5 0 3.5l1.213 13.027S5.965 22 7.475 22h8.987c1.502 0 1.502-1.473 1.502-1.473l1.2-13.027c2.34 0 2.563-3.5 0-3.5h-2.7zM8.936 18.5l-.581-9h1.802v9H8.936zm4.824 0v-9h1.801l-.61 9H13.76z" fill-rule="evenodd"></path></svg></span>删除</button></div></div></div></div></li></ul></div></div></div></div></div></div></main><div class="CornerButtons"><div class="CornerAnimayedFlex"><button data-tooltip="回到顶部" data-tooltip-position="left" data-tooltip-will-hide-on-click="true" aria-label="回到顶部" type="button" class="Button CornerButton Button--plain"><svg class="Zi Zi--BackToTop" aria-label="回到顶部" fill="currentColor" viewBox="0 0 24 24" width="24" height="24"><path d="M16.036 19.59a1 1 0 0 1-.997.995H9.032a.996.996 0 0 1-.997-.996v-7.005H5.03c-1.1 0-1.36-.633-.578-1.416L11.33 4.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.005z"></path></svg></button></div></div></div></div><script id="js-initialData" type="text/json">{"initialState":{"common":{"ask":{}},"loading":{"global":{"count":0},"local":{"env\u002FgetIpinfo\u002F":false,"article\u002Fget\u002F":false,"brand\u002FgetUrl\u002F":false}},"club":{"tags":{},"admins":{"data":[]},"members":{"data":[]},"explore":{"candidateSyncClubs":{}},"profile":{},"checkin":{},"comments":{"paging":{},"loading":{},"meta":{},"ids":{}},"postList":{"paging":{},"loading":{},"ids":{}},"recommend":{"data":[]},"silences":{"data":[]},"application":{"profile":null}},"entities":{"users":{"2bcb30744c0f014ce6702f4d94922945":{"uid":28236180357120,"userType":"people","id":"2bcb30744c0f014ce6702f4d94922945"},"yin-xiang-nan":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic1.zhimg.com\u002Fbf28449bb.jpg?source=172ae18b","uid":"28236180357120","userType":"people","isFollowing":false,"urlToken":"yin-xiang-nan","id":"2bcb30744c0f014ce6702f4d94922945","description":"","name":"杨指北","isAdvertiser":false,"headline":"无业游民","gender":1,"url":"\u002Fpeople\u002F2bcb30744c0f014ce6702f4d94922945","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fbf28449bb_l.jpg?source=172ae18b","isOrg":false,"type":"people","vipInfo":{"isVip":true,"vipIcon":{"url":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-4812630bc27d642f7cafcd6cdeca3d7a_r.png?source=172ae18b","nightModeUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-c9686ff064ea3579730756ac6c289978_r.png?source=172ae18b"}},"levelInfo":{"exp":150802,"level":8,"nicknameColor":{"color":"","nightModeColor":""},"levelIcon":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-ce2d9b858d70d9e107f14c57a0ceb563_l.png","iconInfo":{"url":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-ce2d9b858d70d9e107f14c57a0ceb563_l.png","nightModeUrl":"https:\u002F\u002Fpic3.zhimg.com\u002Fv2-2c1a38e54e53ce62f8b7bc346a043c61_l.png","width":93,"height":51}},"badge":[],"badgeV2":{"title":"","mergedBadges":[],"detailBadges":[],"icon":"","nightIcon":""},"exposedMedal":{"medalId":"1124316222665379841","medalName":"我的知乎 2019","avatarUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-2592b0b52e1fac99f69b38e00252413b_r.png?source=172ae18b","miniAvatarUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-ad363cc3088dc8de7544fd08b1c4987a_l.png?source=172ae18b","description":"参与「我的知乎 2019」即可获得"}}},"questions":{},"answers":{},"articles":{"43843694":{"trackUrl":["https:\u002F\u002Fsugar.zhihu.com\u002Fplutus_adreaper\u002Fpage_monitor_log?si=__SESSIONID__&ti=__ATOKEN__&at=view&pf=__OS__&ed=BiBUKF0xBSkqGGFQA2B_AHGhHcvz4B4D&idfa=__IDFA__&imei=__IMEI__&androidid=__ANDROIDID__&oaid=__OAID__&ci=__CREATIVEID__&zid=__ZONEID__"],"id":43843694,"title":"Pytorch: detach 和 retain_graph","type":"article","articleType":"normal","excerptTitle":"","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fp\u002F43843694","imageUrl":"","titleImage":"","excerpt":"\u003Cimg src=\"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-4a20302598907d050d10ed93e9c41814_200x112.jpg\" data-caption=\"\" data-size=\"normal\" data-rawwidth=\"697\" data-rawheight=\"456\" data-watermark=\"watermark\" data-original-src=\"v2-4a20302598907d050d10ed93e9c41814\" data-watermark-src=\"v2-1c7add429e5c48bb783bc50e8af3fddd\" data-private-watermark-src=\"\" class=\"origin_image inline-img zh-lightbox-thumb\" data-original=\"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-4a20302598907d050d10ed93e9c41814_r.jpg\"\u002F\u003E本人观察 Pytorch 下的生成对抗网络(GAN)的实现代码,发现不同人的实现细节略有不同,其中用到了 detach 和 retain_graph,本文通过两个 gan 的代码,介绍它们的作用,并分析,不同的更新策略对程序效率的影响。 这两个 GAN 的实现中,其更新策略不同,前…","created":1536170007,"updated":1536241814,"author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic1.zhimg.com\u002Fbf28449bb.jpg?source=172ae18b","uid":"28236180357120","userType":"people","isFollowing":false,"urlToken":"yin-xiang-nan","id":"2bcb30744c0f014ce6702f4d94922945","description":"","name":"杨指北","isAdvertiser":false,"headline":"无业游民","gender":1,"url":"\u002Fpeople\u002F2bcb30744c0f014ce6702f4d94922945","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fbf28449bb_l.jpg?source=172ae18b","isOrg":false,"type":"people","vipInfo":{"isVip":true,"vipIcon":{"url":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-4812630bc27d642f7cafcd6cdeca3d7a_r.png?source=172ae18b","nightModeUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-c9686ff064ea3579730756ac6c289978_r.png?source=172ae18b"}},"levelInfo":{"exp":150802,"level":8,"nicknameColor":{"color":"","nightModeColor":""},"levelIcon":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-ce2d9b858d70d9e107f14c57a0ceb563_l.png","iconInfo":{"url":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-ce2d9b858d70d9e107f14c57a0ceb563_l.png","nightModeUrl":"https:\u002F\u002Fpic3.zhimg.com\u002Fv2-2c1a38e54e53ce62f8b7bc346a043c61_l.png","width":93,"height":51}},"badge":[],"badgeV2":{"title":"","mergedBadges":[],"detailBadges":[],"icon":"","nightIcon":""},"exposedMedal":{"medalId":"1124316222665379841","medalName":"我的知乎 2019","avatarUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-2592b0b52e1fac99f69b38e00252413b_r.png?source=172ae18b","miniAvatarUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-ad363cc3088dc8de7544fd08b1c4987a_l.png?source=172ae18b","description":"参与「我的知乎 2019」即可获得"}},"commentPermission":"all","copyrightPermission":"need_review","state":"published","imageWidth":0,"imageHeight":0,"content":"\u003Cp\u003E本人观察 Pytorch 下的生成对抗网络(GAN)的实现代码,发现不同人的实现细节略有不同,其中用到了 detach 和 retain_graph,本文通过两个 gan 的代码,介绍它们的作用,并分析,不同的更新策略对程序效率的影响。\u003C\u002Fp\u003E\u003Cp\u003E这两个 GAN 的实现中,其更新策略不同,前者是先更新判别器(discriminator)参数,再更新生成器(generator)参数,这正是原始论文\u003Ca href=\"https:\u002F\u002Flink.zhihu.com\u002F?target=https%3A\u002F\u002Farxiv.org\u002Fabs\u002F1406.2661\" class=\" wrap external\" target=\"_blank\" rel=\"nofollow noreferrer\"\u003EGenerative Adversarial Networks\u003C\u002Fa\u003E 中的算法(下图所示);后者是先更新 generator 参数,再更新 discriminator 参数,它们的实现孰优孰劣呢?\u003C\u002Fp\u003E\u003Cp class=\"ztext-empty-paragraph\"\u003E\u003Cbr\u002F\u003E\u003C\u002Fp\u003E\u003Cfigure data-size=\"normal\"\u003E\u003Cnoscript\u003E\u003Cimg src=\"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-1c7add429e5c48bb783bc50e8af3fddd_b.jpg\" data-caption=\"\" data-size=\"normal\" data-rawwidth=\"697\" data-rawheight=\"456\" class=\"origin_image zh-lightbox-thumb\" width=\"697\" data-original=\"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-1c7add429e5c48bb783bc50e8af3fddd_r.jpg\"\u002F\u003E\u003C\u002Fnoscript\u003E\u003Cimg src=\"data:image\u002Fsvg+xml;utf8,<svg xmlns='http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg' width='697' height='456'><\u002Fsvg>\" data-caption=\"\" data-size=\"normal\" data-rawwidth=\"697\" data-rawheight=\"456\" class=\"origin_image zh-lightbox-thumb lazy\" width=\"697\" data-original=\"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-1c7add429e5c48bb783bc50e8af3fddd_r.jpg\" data-actualsrc=\"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-1c7add429e5c48bb783bc50e8af3fddd_b.jpg\"\u002F\u003E\u003C\u002Ffigure\u003E\u003Ch2\u003E\u003Cb\u003EGAN 的基本原理\u003C\u002Fb\u003E\u003C\u002Fh2\u003E\u003Cp\u003E首先回顾一下生成对抗网络算法的基本原理,对于熟悉的同学,可以跳过这部分。限于篇幅,只介绍最原始的两种 GAN 损失函数,不失一般性。\u003C\u002Fp\u003E\u003Cp\u003E原始 GAN 的损失函数,主要来源于 binary cross-entropy loss:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L%28x%5E%7B%28i%29%7D%2Cy%5E%7B%28i%29%7D%29%3D-y%5E%7B%28i%29%7D%5Ccdot%5Cmathrm%7Blog%7DP%28x%5E%7B%28i%29%7D%29-%281-y%5E%7B%28i%29%7D%29%5Ccdot%5Cmathrm%7Blog%7D%281-P%28x%5E%7B%28i%29%7D%29%29%5Ctag%7B1%7D\" alt=\"L(x^{(i)},y^{(i)})=-y^{(i)}\\cdot\\mathrm{log}P(x^{(i)})-(1-y^{(i)})\\cdot\\mathrm{log}(1-P(x^{(i)}))\\tag{1}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E其中,y 为真实标签,取 1 (正样本) 或者 0 (负样本),P(x) 为 x 属于正样本的概率。\u003C\u002Fp\u003E\u003Cp\u003E\u003Cb\u003E判别器\u003C\u002Fb\u003E有两种输入,一种是真实的样本 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%28x%5Cin+P_r%2Cy%3D1%29\" alt=\"(x\\in P_r,y=1)\" eeimg=\"1\"\u002F\u003E;一种是生成器通过噪音 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=z\" alt=\"z\" eeimg=\"1\"\u002F\u003E 生成的假样本 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%28G%28z%29%5Cin+P_g%2C+y%3D0%29\" alt=\"(G(z)\\in P_g, y=0)\" eeimg=\"1\"\u002F\u003E 。判别器的工作是将这两种来源的样本区分开,故采用公式 (1) 计算其损失函数 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_d\" alt=\"L_d\" eeimg=\"1\"\u002F\u003E 。\u003C\u002Fp\u003E\u003Cp\u003E分别把真实样本和生成的假样本代入公式 (1),对于每个正样本,其损失函数只剩下:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_%7Bd_%7Breal%7D%7D%28x%5E%7B%28i%29%7D%5Cin+P_r%2Cy%5E%7B%28i%29%7D%3D1%29%3D-%5Cmathrm%7Blog%7DD%28x%5E%7B%28i%29%7D%29%5Ctag%7B2%7D\" alt=\"L_{d_{real}}(x^{(i)}\\in P_r,y^{(i)}=1)=-\\mathrm{log}D(x^{(i)})\\tag{2}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E而对于每个负样本,损失函数为:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_%7Bd_%7Bfake%7D%7D%28G%28z%5E%7B%28i%29%7D%29%2Cy%5E%7B%28i%29%7D%3D0%29%3D-%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B3%7D\" alt=\"L_{d_{fake}}(G(z^{(i)}),y^{(i)}=0)=-\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{3}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E(这里把公式 (1) 的 P(x) 改为 D(x),表示是 discriminator 的输出,和上图中的算法保持一致)。把这两个损失函数加起来,算一下平均值:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_%7Bd_%7Btotal%7D%7D%3D-%5Cfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Cmathrm%7Blog%7DD%28x%5E%7B%28i%29%7D%29%2B%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B4%7D\" alt=\"L_{d_{total}}=-\\frac{1}{m}\\sum_{i=1}^{m}\\mathrm{log}D(x^{(i)})+\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{4}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E然后,将这个损失函数对 discriminator 的参数 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Ctheta_d\" alt=\"\\theta_d\" eeimg=\"1\"\u002F\u003E 求导,即得到上图算法中的梯度:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Cfrac%7B%5Cmathrm%7Bd%7DL_%7Bd_%7Btotal%7D%7D%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_d%7D%3D-%5Cnabla_%7B%5Ctheta_d%7D%5Cfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Cmathrm%7Blog%7DD%28x%5E%7B%28i%29%7D%29%2B%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B5%7D\" alt=\"\\frac{\\mathrm{d}L_{d_{total}}}{\\mathrm{d}\\theta_d}=-\\nabla_{\\theta_d}\\frac{1}{m}\\sum_{i=1}^{m}\\mathrm{log}D(x^{(i)})+\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{5}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E(\u003Ci\u003E\u003Cb\u003E注意算法截图中用梯度上升优化,所以和上式差一个负号\u003C\u002Fb\u003E\u003C\u002Fi\u003E)\u003C\u002Fp\u003E\u003Cp\u003E\u003Cb\u003E生成器\u003C\u002Fb\u003E输入噪声 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=z%5E%7B%28i%29%7D\" alt=\"z^{(i)}\" eeimg=\"1\"\u002F\u003E ,输出一个假数据 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=G%28z%5E%7B%28i%29%7D%29\" alt=\"G(z^{(i)})\" eeimg=\"1\"\u002F\u003E 。它希望这个假数据能骗过判别器。而上文判别器对假数据的损失函数为:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_%7Bd_%7Bfake%7D%7D%28G%28z%5E%7B%28i%29%7D%29%2Cy%5E%7B%28i%29%7D%3D0%29%3D-%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B3%7D\" alt=\"L_{d_{fake}}(G(z^{(i)}),y^{(i)}=0)=-\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{3}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E这个损失越大,意味着判别器的性能越差,同时也意味着生成器的性能越好。也就是说,我们希望朝着增大该损失的方向来调整生成器的参数。如果采用梯度下降优化生成器,也就是要减小它的相反数,即生成器的损失函数:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_g%28z%5E%7B%28i%29%7D%29%3D%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B6%7D\" alt=\"L_g(z^{(i)})=\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{6}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E这就是原始 GAN 生成器的第一种损失函数,对其取平均再对生成器参数 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Ctheta_g\" alt=\"\\theta_g\" eeimg=\"1\"\u002F\u003E 求梯度,得到算法截图里面的公式:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Cfrac%7B%5Cmathrm%7Bd%7DL_%7Bg_%7Btotal%7D%7D%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_g%7D%3D%5Cnabla_%7B%5Ctheta_g%7D%5Cfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5Em%5Cmathrm%7Blog%7D%5Cleft%281-D%28G%28z%5E%7B%28i%29%7D%29%29%5Cright%29%5Ctag%7B7%7D\" alt=\"\\frac{\\mathrm{d}L_{g_{total}}}{\\mathrm{d}\\theta_g}=\\nabla_{\\theta_g}\\frac{1}{m}\\sum_{i=1}^m\\mathrm{log}\\left(1-D(G(z^{(i)}))\\right)\\tag{7}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E(\u003Ci\u003E\u003Cb\u003E算法截图中用梯度下降优化,所以和上式相同\u003C\u002Fb\u003E\u003C\u002Fi\u003E)\u003C\u002Fp\u003E\u003Cp\u003E但是,公式 (6) 的损失函数在训练初期梯度会特别小。因为刚开始训练时,生成器生成的数据十分不真实,导致判别器很容易判断出来它是假数据。因此 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=D%28G%28z%5E%7B%28i%29%7D%29%29\" alt=\"D(G(z^{(i)}))\" eeimg=\"1\"\u002F\u003E 是个接近于0 的常数。而损失函数 (6) 对 generator 参数 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Ctheta_g\" alt=\"\\theta_g\" eeimg=\"1\"\u002F\u003E 的导数为:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Cfrac%7B%5Cmathrm%7Bd%7DL_g%28z%5E%7B%28i%29%7D%29%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_g%7D%3D-%5Cfrac%7B1%7D%7B1-D%28G%28z%5E%7B%28i%29%7D%29%29%7D%5Ccdot%5Cfrac%7B%5Cmathrm%7Bd%7DD%28G%28z%5E%7B%28i%29%7D%29%29%7D%7B%5Cmathrm%7Bd%7DG%28z%5E%7B%28i%29%7D%29%7D%5Ccdot%5Cfrac%7B%5Cmathrm%7Bd%7DG%28z%5E%7B%28i%29%7D%29%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_g%7D%5Ctag%7B8%7D\" alt=\"\\frac{\\mathrm{d}L_g(z^{(i)})}{\\mathrm{d}\\theta_g}=-\\frac{1}{1-D(G(z^{(i)}))}\\cdot\\frac{\\mathrm{d}D(G(z^{(i)}))}{\\mathrm{d}G(z^{(i)})}\\cdot\\frac{\\mathrm{d}G(z^{(i)})}{\\mathrm{d}\\theta_g}\\tag{8}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E由于 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=D%28G%28z%5E%7B%28i%29%7D%29%29\" alt=\"D(G(z^{(i)}))\" eeimg=\"1\"\u002F\u003E 是接近于0的常数,所以 (8) 中等号右边第一项近似为 -1,而中间项为接近 0 的数(因为 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=D%28G%28z%5E%7B%28i%29%7D%29%29\" alt=\"D(G(z^{(i)}))\" eeimg=\"1\"\u002F\u003E 是接近 0 的常函数,而常函数的梯度为 0)。\u003C\u002Fp\u003E\u003Cp\u003E因此,GAN 的作者又给出了第二种生成器损失函数:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=L_g%28z%5E%7B%28i%29%7D%29%3D-%5Cmathrm%7Blog%7D%28D%28G%28z%5E%7B%28i%29%7D%29%29%29%5Ctag%7B9%7D\" alt=\"L_g(z^{(i)})=-\\mathrm{log}(D(G(z^{(i)})))\\tag{9}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E这个损失函数很好理解,就是要生成这样的数据:使得输入 discriminator 后,输出一个较大的概率,即对生成器来说 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Cmathrm%7Blog%7D%28D%28G%28z%5E%7B%28i%29%7D%29%29%29\" alt=\"\\mathrm{log}(D(G(z^{(i)})))\" eeimg=\"1\"\u002F\u003E 越大越好,取它的相反数,也就是公式 (9) 越小越好。我们计算公式 (9) 的损失函数对生成器参数 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Ctheta_g\" alt=\"\\theta_g\" eeimg=\"1\"\u002F\u003E 的导数:\u003C\u002Fp\u003E\u003Cp\u003E\u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=%5Cfrac%7B%5Cmathrm%7Bd%7DL_g%28z%5E%7B%28i%29%7D%29%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_g%7D%3D-%5Cfrac%7B1%7D%7BD%28G%28z%5E%7B%28i%29%7D%29%29%7D%5Ccdot%5Cfrac%7B%5Cmathrm%7Bd%7DD%28G%28z%5E%7B%28i%29%7D%29%29%7D%7B%5Cmathrm%7Bd%7DG%28z%5E%7B%28i%29%7D%29%7D%5Ccdot%5Cfrac%7B%5Cmathrm%7Bd%7DG%28z%5E%7B%28i%29%7D%29%7D%7B%5Cmathrm%7Bd%7D%5Ctheta_g%7D%5Ctag%7B10%7D\" alt=\"\\frac{\\mathrm{d}L_g(z^{(i)})}{\\mathrm{d}\\theta_g}=-\\frac{1}{D(G(z^{(i)}))}\\cdot\\frac{\\mathrm{d}D(G(z^{(i)}))}{\\mathrm{d}G(z^{(i)})}\\cdot\\frac{\\mathrm{d}G(z^{(i)})}{\\mathrm{d}\\theta_g}\\tag{10}\" eeimg=\"1\"\u002F\u003E \u003C\u002Fp\u003E\u003Cp\u003E和 (8) 比只有等号右边第一项不同,当 \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=D%28G%28z%5E%7B%28i%29%7D%29%29\" alt=\"D(G(z^{(i)}))\" eeimg=\"1\"\u002F\u003E 接近 0 时, \u003Cimg src=\"https:\u002F\u002Fwww.zhihu.com\u002Fequation?tex=1%2FD%28G%28z%5E%7B%28i%29%7D%29%29\" alt=\"1\u002FD(G(z^{(i)}))\" eeimg=\"1\"\u002F\u003E 接近无穷大,和中间那项相乘,一定程度抵消了中间项接近 0 的问题,使计算出来的梯度增大了。\u003C\u002Fp\u003E\u003Cp\u003E在实现过程中,基本上都是采用公式 (9) 作为生成器损失函数,因为这十分方便,只要在计算生成器损失函数时,把虚假数据的标签标记为真,代入 Binary cross-entropy loss 的公式即可。\u003C\u002Fp\u003E\u003Ch2\u003E\u003Cb\u003E更新策略\u003C\u002Fb\u003E\u003C\u002Fh2\u003E\u003Cp\u003E下面进入本文正题,即,在 pytorch 中,detach 和 retain_graph 是干什么用的?本文将借助两段 GAN 的实现代码,来举例介绍它们的作用。\u003C\u002Fp\u003E\u003Cp\u003E\u003Cb\u003E第一段代码先更新判别器,再更新生成器。\u003C\u002Fb\u003E我们分析循环中一个 step 的代码:\u003C\u002Fp\u003E\u003Cdiv class=\"highlight\"\u003E\u003Cpre\u003E\u003Ccode class=\"language-python3\"\u003E\u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Etorch\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Esize\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E1\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Efill_\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mf\"\u003E1.0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eto\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Edevice\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 真实标签,都是1\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Efake\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Etorch\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Esize\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E1\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Efill_\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mf\"\u003E0.0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eto\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Edevice\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 假标签,都是0\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E# 训练判别器\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ereal_imgs\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eto\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Edevice\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ez\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Etorch\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Erandn\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E((\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eshape\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E[\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E],\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E100\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E))\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eto\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Edevice\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 噪声\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Egen_imgs\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Egenerator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ez\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 从噪声中生成假数据\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Epred_gen\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Ediscriminator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Egen_imgs\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器对假数据的输出\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Epred_real\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Ediscriminator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ereal_imgs\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器对真数据的输出\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_D\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ezero_grad\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 把判别器中所有参数的梯度归零\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ereal_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Epred_real\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E,\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器对真实样本的损失\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Efake_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Epred_gen\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E,\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Efake\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器对假样本的损失\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ed_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ereal_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E+\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Efake_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E\u002F\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E2\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 两项损失相加取平均\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E# 下面这行代码十分重要,将在正文着重讲解\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ed_loss\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ebackward\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eretain_graph\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E\u003Cspan class=\"kc\"\u003ETrue\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# retain_graph 十分重要,否则计算图内存将会被释放\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_D\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Estep\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器参数更新\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#---------\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#训练生成器\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#---------\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eg_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Epred_gen\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E,\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器的损失函数\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_G\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ezero_grad\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器参数梯度归零\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eg_loss\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ebackward\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器的损失函数梯度反向传播\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_G\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Estep\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器参数更新\u003C\u002Fspan\u003E\u003C\u002Fcode\u003E\u003C\u002Fpre\u003E\u003C\u002Fdiv\u003E\u003Cp\u003E上面的代码中 d_loss.backward(retain_graph=True) 十分关键,它用于反向传播 discriminator loss 的梯度。那么,具体传播到什么地方去呢?\u003C\u002Fp\u003E\u003Cp\u003E这要看 d_loss 是由哪几部分构成的:real_loss 和 fake_loss,而 fake_loss 又是从 noise 经过 generator 来的。因此,d_loss 反向传播,将一传到底,不但计算了 discriminator 的梯度,同时还计算了 generator 的梯度,虽然这一步optimizer_D.step()只更新 discriminator 的参数。\u003C\u002Fp\u003E\u003Cp\u003E也正是这个原因,下面在更新 generator 参数时,要先将生成器参数的梯度重新归零,避免受到 discriminator loss 回传过来的梯度影响。注意:它在反向传播时,设置了\u003Cb\u003E retain graph = 0 \u003C\u002Fb\u003E这个参数。它的作用是\u003Cb\u003E保持计算图\u003C\u002Fb\u003E,因为 pytorch 默认\u003Cb\u003E一个计算图只计算一次反向传播,反向传播后,这个计算图的内存就被释放了\u003C\u002Fb\u003E。而后面的 generator 算梯度时还要用到这个计算图,所以用这个参数控制计算图不被释放。\u003C\u002Fp\u003E\u003Cp\u003Egenerator 的 损失在回传时,同样要经过 discriminator 网络才能传递回自身(系统从输入噪声到 Discriminator 输出,从头到尾只有一次前向传播,而有两次反向传播,故在第一次反向传播时,要保持计算图不被释放)。因此,在回传梯度时,同样也计算了一遍 discriminator 的参数梯度,只不过这次 discriminator 的参数不更新,只更新 generator 的参数,即 optimizer_G.step()。同时,我们看到,下一个 step 首先将 discriminator 的梯度重置为 0,就是为了防止 generator loss 反向传播时顺带计算的梯度对其造成影响(还有上一步 discriminator loss 回传时累积的梯度)。\u003C\u002Fp\u003E\u003Cp\u003E综上,我们看到,为了完成一步参数更新,我们进行了两次反向传播,第一次反向传播为了更新 discriminator 的参数,但多余计算了 generator 的梯度。第二次反向传播为了更新 generator 的参数,但是不得不多计算 discriminator 的梯度。\u003C\u002Fp\u003E\u003Cp class=\"ztext-empty-paragraph\"\u003E\u003Cbr\u002F\u003E\u003C\u002Fp\u003E\u003Cp\u003E\u003Cb\u003E对于先更新生成器参数的情况\u003C\u002Fb\u003E,我们也分析其循环中一个 step 的代码:\u003C\u002Fp\u003E\u003Cdiv class=\"highlight\"\u003E\u003Cpre\u003E\u003Ccode class=\"language-python3\"\u003E\u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003EVariable\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Esize\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E1\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Efill_\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mf\"\u003E1.0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Erequires_grad\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E\u003Cspan class=\"kc\"\u003EFalse\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 真实样本的标签,都是 1\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Efake\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003EVariable\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Esize\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E1\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Efill_\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mf\"\u003E0.0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Erequires_grad\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E\u003Cspan class=\"kc\"\u003EFalse\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成样本的标签,都是 0\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ereal_imgs\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003EVariable\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Etype\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E))\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#-----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E# 训练生成器\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#-----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_G\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ezero_grad\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器参数梯度归零\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ez\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003EVariable\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003ETensor\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Enp\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Erandom\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Enormal\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E,\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E1\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E,\u003C\u002Fspan\u003E \u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eimgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Eshape\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E[\u003C\u002Fspan\u003E\u003Cspan class=\"mi\"\u003E0\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E],\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eopt\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Elatent_dim\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E))))\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 噪声\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Egen_imgs\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Egenerator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ez\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 根据噪声生成虚假样本\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eg_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ediscriminator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Egen_imgs\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 用真实的标签+假样本,计算生成器损失\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eg_loss\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ebackward\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器梯度反向传播,反向传播经过了判别器,故此时判别器参数也有梯度\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_G\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Estep\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 生成器参数更新,判别器参数虽然有梯度,但是这一步不能更新判别器\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E# 训练判别器\u003C\u002Fspan\u003E\n\u003Cspan class=\"c1\"\u003E#----------\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_D\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ezero_grad\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 把生成器损失函数梯度反向传播时,顺带计算的判别器参数梯度清空\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ereal_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ediscriminator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ereal_imgs\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E),\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Evalid\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 真样本+真标签:判别器损失\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Efake_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Eadversarial_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ediscriminator\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Egen_imgs\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Edetach\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()),\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Efake\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 假样本+假标签:判别器损失\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ed_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E=\u003C\u002Fspan\u003E \u003Cspan class=\"p\"\u003E(\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ereal_loss\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E+\u003C\u002Fspan\u003E \u003Cspan class=\"n\"\u003Efake_loss\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E)\u003C\u002Fspan\u003E \u003Cspan class=\"o\"\u003E\u002F\u003C\u002Fspan\u003E \u003Cspan class=\"mi\"\u003E2\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器总的损失函数\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Ed_loss\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Ebackward\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器损失回传\u003C\u002Fspan\u003E\n\u003Cspan class=\"n\"\u003Eoptimizer_D\u003C\u002Fspan\u003E\u003Cspan class=\"o\"\u003E.\u003C\u002Fspan\u003E\u003Cspan class=\"n\"\u003Estep\u003C\u002Fspan\u003E\u003Cspan class=\"p\"\u003E()\u003C\u002Fspan\u003E \u003Cspan class=\"c1\"\u003E# 判别器参数更新\u003C\u002Fspan\u003E\u003C\u002Fcode\u003E\u003C\u002Fpre\u003E\u003C\u002Fdiv\u003E\u003Cp\u003E上述代码先更新生成器参数,再更新判别器参数。那么除了顺序上的区别,和先更新判别器,再更新生成器,还有什么不同?答案是,计算图的遍历次数不同。\u003C\u002Fp\u003E\u003Cp\u003E为了更新生成器参数,用生成器的损失函数计算梯度,然后反向传播,传播图中经过了discriminator,根据链式法则,不得不顺带计算一下判别器的参数梯度,虽然在这一步不会更新判别器参数。反向传播过后,noise 到 fake image 再到 discriminator 的输出这个前向传播的计算图就被释放掉了,后面也不会再用到。\u003C\u002Fp\u003E\u003Cp\u003E接着更新判别器参数,此时注意到,我们输入判别器的是两部分,一部分是真实数据,另一部分是生成器的输出,也就是假数据。注意观察细节,在判别器前向传播过程,输入的假数据被 detach 了:discriminator(gen_imgs.detach()),detach 的意思是,这个数据和生成它的计算图“脱钩”了,即梯度传到它那个地方就停了,不再继续往前传播(实际上也不会再往前传播了,因为 generator 的计算图在第一次反向传播过后就被释放了)。因此,判别器梯度反向传播,就到它自己身上为止。\u003C\u002Fp\u003E\u003Cp\u003E因此,比起第一种策略,要少计算一次 generator 的所有参数的梯度,同时,也不必刻意保存一次计算图,占用不必要的内存。\u003C\u002Fp\u003E\u003Cp\u003E但需要注意的是,在第一种策略中,noise 从 generator 输入,到 discriminator 输出,只经历了一次前向传播,discriminator 端的输出,被用了两次,一次是计算 discriminator 的损失函数,另一次是计算 generator 的损失函数。\u003C\u002Fp\u003E\u003Cp\u003E而在第二种策略中,noise 从 generator 输入,到discriminator 输出,计算 generator 损失,回传,这一步更新了 generator 的参数,并释放了计算图。下一步更新 discriminator 的参数时,generator 的输出经过 detach 后,又通过了一遍 discriminator,相当于,generator 的输出前后两次通过了 discriminator ,得到相同的输出。显然,这也是冗余的。\u003C\u002Fp\u003E\u003Cp class=\"ztext-empty-paragraph\"\u003E\u003Cbr\u002F\u003E\u003C\u002Fp\u003E\u003Cp\u003E综上,这两段代码各有利弊:\u003C\u002Fp\u003E\u003Cp\u003E第一段代码,好处是 noise 只进行了一次前向传播,缺点是,更新 discriminator 参数时,多计算了一次 generator 的梯度,同时,第一次更新 discriminator 需要保留计算图,保证算 generator loss 时计算图不被销毁。\u003C\u002Fp\u003E\u003Cp\u003E第二段代码,好处是通过先更新 generator ,使更新后的前向传播计算图可以放心被销毁,因此不用保留计算图占用内存。同时,在更新 discriminator 的时候,也不会像上面的那段代码,计算冗余的 generator 的梯度。缺点是,在 discriminator 上,对 generator 的输出算了两次前向传播,第二次又产生了新的计算图(但比第一次的小)。\u003C\u002Fp\u003E\u003Cp\u003E一个多计算了一次 generator 梯度,一个多计算一次 discriminator 前向传播。因此,两者差别不大。如果 discriminator 比generator 复杂,那么应该采取第一种策略,如果 discriminator 比 generator 简单,那么应该采取第二种策略,通常情况下,discriminator 要比 generator 简单,故应该采取第二种策略居多。\u003C\u002Fp\u003E\u003Cp class=\"ztext-empty-paragraph\"\u003E\u003Cbr\u002F\u003E\u003C\u002Fp\u003E\u003Cp\u003E但是第二种先更新generator,再更新 discriminator 总是给人感觉怪怪得,因为 generator 的更新需要 discriminator 提供准确的 loss 和 gradient,否则岂不是在瞎更新?\u003C\u002Fp\u003E\u003Cp class=\"ztext-empty-paragraph\"\u003E\u003Cbr\u002F\u003E\u003C\u002Fp\u003E\u003Cp\u003E还有一种没提到的策略,noise 从 generator 输入,输出 fake data,然后 detach 一下,随着 true data 一起输入 discriminator,计算 discriminator 损失,并更新 discriminator 参数。接下来,再把没经过 detach 的 fake data 输入到discriminator 中,计算 generator loss,再反向传播梯度,更新 generator 的参数。这种策略,计算了两次 discriminator 梯度,一次 generator 梯度。感觉这种比较符合先更新 discriminator 的习惯。缺点是,之前的 generator 生成的计算图得保留着,直到 discriminator 更新完,再释放。不像策略二,马上用完马上释放。综合来说,还是策略二最好,策略三其次,策略一最差(差在多计算一次 generator gradient 上,而通常多计算一次 generator gradient 的运算量比多计算一次 discriminator 前向传播的运算量大),因此,detach 还是很有必要的。\u003C\u002Fp\u003E","adminClosedComment":false,"topics":[{"url":"https:\u002F\u002Fwww.zhihu.com\u002Fapi\u002Fv4\u002Ftopics\u002F19607065","type":"topic","id":"19607065","name":"神经网络"},{"url":"https:\u002F\u002Fwww.zhihu.com\u002Fapi\u002Fv4\u002Ftopics\u002F19559450","type":"topic","id":"19559450","name":"机器学习"},{"url":"https:\u002F\u002Fwww.zhihu.com\u002Fapi\u002Fv4\u002Ftopics\u002F20070859","type":"topic","id":"20070859","name":"生成对抗网络(GAN)"}],"voteupCount":163,"voting":0,"column":{"description":"一些计算机专业领域的学习笔记~","canManage":false,"intro":"微信公众号:轮子工厂,6万多人的选择,值得关注!","isFollowing":true,"urlToken":"c_167341220","id":"c_167341220","articlesCount":324,"acceptSubmission":true,"title":"轮子工厂","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_167341220","commentPermission":"all","created":1519957181,"updated":1599487122,"imageUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-db630cba4a5dcd60ccd306633b89aa93_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd.jpg?source=172ae18b","uid":"58629923274752","userType":"people","isFollowing":false,"urlToken":"tanqingbo","id":"413d6f0390e1f6d0122019a93301ded9","description":"计算机博士在读\n公众号@轮子工厂\n个人网站:http:\u002F\u002Ftanqingbo.cn\u002F\n全网20万精准粉丝\n年懂技术也懂内容\n微信:best_changzhang","name":"谭庆波","isAdvertiser":false,"headline":"公众号:轮子工厂,回复「1024」获取程序员神秘大礼包~","gender":1,"url":"\u002Fpeople\u002F413d6f0390e1f6d0122019a93301ded9","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":4682,"type":"column"},"commentCount":56,"contributions":[{"id":2051695,"state":"need_approved","type":"include","column":{"description":"关于人工智能、复杂网络、系统科学方面的知识笔记","canManage":false,"intro":"为你打造属于你学习人工智能的最佳学习路径","isFollowing":true,"urlToken":"c_80412427","id":"c_80412427","articlesCount":223,"acceptSubmission":true,"title":"人工智能学习笔记","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_80412427","commentPermission":"all","created":1488258563,"updated":1599138364,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-0b1fe24b358b429d8e0dd4a5d01750da_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-aec38e760b441fb0b6496c2b9429f865.jpg?source=172ae18b","uid":"632671950552698880","userType":"people","isFollowing":false,"urlToken":"zhang-jiang-78-29-94","id":"6cd4ac54f8fe9179477d510ab0e40235","description":"张江,北京师范大学系统科学学院教授,集智俱乐部、集智学园创始人,腾讯研究院特聘顾问,人工智能研究者与布道师。十五年来,其开创的集智俱乐部已经成为全国最活跃、历史最悠久的人工智能俱乐部之一,获得过凯风基金会的赞助,孕育出彩云天气人工智能团队和Momenta自动驾驶项目,先后出版了《科学的极致——漫谈人工智能》、《走近2050——注意力、互联网与人工智能》两本广受欢迎的人工智能科普书。其中《科学的极致》被杨澜在其微博推荐,已反复再版,《走近2050》刚一面市就冲击到了亚马逊计算机类图书排行榜首位。","name":"张江","isAdvertiser":false,"headline":"打造没有‘’围墙‘’的研究所!","gender":1,"url":"\u002Fpeople\u002F6cd4ac54f8fe9179477d510ab0e40235","avatarUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-aec38e760b441fb0b6496c2b9429f865_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":10568,"type":"column"}},{"id":2076613,"state":"accepted","type":"include","column":{"description":"一些计算机专业领域的学习笔记~","canManage":false,"intro":"微信公众号:轮子工厂,6万多人的选择,值得关注!","isFollowing":true,"urlToken":"c_167341220","id":"c_167341220","articlesCount":324,"acceptSubmission":true,"title":"轮子工厂","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_167341220","commentPermission":"all","created":1519957181,"updated":1599487122,"imageUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-db630cba4a5dcd60ccd306633b89aa93_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd.jpg?source=172ae18b","uid":"58629923274752","userType":"people","isFollowing":false,"urlToken":"tanqingbo","id":"413d6f0390e1f6d0122019a93301ded9","description":"计算机博士在读\n公众号@轮子工厂\n个人网站:http:\u002F\u002Ftanqingbo.cn\u002F\n全网20万精准粉丝\n年懂技术也懂内容\n微信:best_changzhang","name":"谭庆波","isAdvertiser":false,"headline":"公众号:轮子工厂,回复「1024」获取程序员神秘大礼包~","gender":1,"url":"\u002Fpeople\u002F413d6f0390e1f6d0122019a93301ded9","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":4682,"type":"column"}},{"id":26021014,"state":"accepted","type":"first_publish","column":{"description":"记录有深度和最新的计算机视觉相关学习笔记!","canManage":false,"intro":"记录有深度和最新的计算机视觉相关文章!","isFollowing":false,"urlToken":"c_1101886054759501824","id":"c_1101886054759501824","articlesCount":3,"acceptSubmission":true,"title":"计算机视觉学习笔记","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_1101886054759501824","commentPermission":"all","created":1555490107,"updated":1591364210,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002F4b70deef7_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59.jpg?source=172ae18b","uid":"61292979159040","userType":"people","isFollowing":false,"urlToken":"ranjiewen","id":"b5a2a257c0b0fedf0318b73b881c810d","description":"图像处理\u002F计算机视觉\u002F深度学习; \n日常想回成都过日子…","name":"jiewen","isAdvertiser":false,"headline":"保持好奇心......","gender":1,"url":"\u002Fpeople\u002Fb5a2a257c0b0fedf0318b73b881c810d","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":34,"type":"column"}},{"id":26864886,"state":"accepted","type":"first_publish","column":{"description":"","canManage":false,"intro":"最通俗的语言,最完整的知识体系,人人都是CV工程师","isFollowing":false,"urlToken":"c_1293925349546061824","id":"c_1293925349546061824","articlesCount":42,"acceptSubmission":true,"title":"人人都是计算机视觉算法工程师","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_1293925349546061824","commentPermission":"all","created":1601275843,"updated":1604925121,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002F4b70deef7_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774.jpg?source=172ae18b","uid":"606094689548505088","userType":"people","isFollowing":false,"urlToken":"caishuxiao","id":"371bedadfda430022a92fc7556d80ef7","description":"永远向有结果的人学习","name":"菜务自由","isAdvertiser":false,"headline":"永远向有结果的人学习","gender":1,"url":"\u002Fpeople\u002F371bedadfda430022a92fc7556d80ef7","avatarUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":23,"type":"column"}}],"isTitleImageFullScreen":false,"upvotedFollowees":[],"commercialInfo":{"isCommercial":false,"plugin":{}},"suggestEdit":{"status":false,"reason":"","tip":"","url":"","title":""},"reason":"","annotationAction":[],"canTip":false,"canOpenTipjar":true,"tipjarorsCount":0,"isLabeled":false,"hasPublishingDraft":false,"isFavorited":true,"favlistsCount":105,"isNormal":true,"status":0,"activityToppingInfo":{"state":"untopped"},"shareText":"Pytorch: detach 和 retain_graph - 来自知乎专栏「轮子工厂」,作者: 杨指北 https:\u002F\u002Fzhuanlan.zhihu.com\u002Fp\u002F43843694 (想看更多?下载 @知乎 App:http:\u002F\u002Fweibo.com\u002Fp\u002F100404711598 )","canComment":{"status":true,"reason":""},"mcnFpShow":-1,"isVisible":true,"isLiked":false,"likedCount":12,"visibleOnlyToAuthor":false,"hasColumn":true,"republishers":[{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774.jpg?source=172ae18b","uid":"606094689548505088","userType":"people","isFollowing":false,"urlToken":"caishuxiao","id":"371bedadfda430022a92fc7556d80ef7","description":"永远向有结果的人学习","name":"菜务自由","isAdvertiser":false,"headline":"永远向有结果的人学习","gender":1,"url":"\u002Fpeople\u002F371bedadfda430022a92fc7556d80ef7","avatarUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774_l.jpg?source=172ae18b","isOrg":false,"type":"people"},{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59.jpg?source=172ae18b","uid":"61292979159040","userType":"people","isFollowing":false,"urlToken":"ranjiewen","id":"b5a2a257c0b0fedf0318b73b881c810d","description":"图像处理\u002F计算机视觉\u002F深度学习; \n日常想回成都过日子…","name":"jiewen","isAdvertiser":false,"headline":"保持好奇心......","gender":1,"url":"\u002Fpeople\u002Fb5a2a257c0b0fedf0318b73b881c810d","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59_l.jpg?source=172ae18b","isOrg":false,"type":"people"}]}},"columns":{"c_80412427":{"description":"关于人工智能、复杂网络、系统科学方面的知识笔记","canManage":false,"intro":"为你打造属于你学习人工智能的最佳学习路径","isFollowing":true,"urlToken":"c_80412427","id":"c_80412427","articlesCount":223,"acceptSubmission":true,"title":"人工智能学习笔记","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_80412427","commentPermission":"all","created":1488258563,"updated":1599138364,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-0b1fe24b358b429d8e0dd4a5d01750da_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-aec38e760b441fb0b6496c2b9429f865.jpg?source=172ae18b","uid":"632671950552698880","userType":"people","isFollowing":false,"urlToken":"zhang-jiang-78-29-94","id":"6cd4ac54f8fe9179477d510ab0e40235","description":"张江,北京师范大学系统科学学院教授,集智俱乐部、集智学园创始人,腾讯研究院特聘顾问,人工智能研究者与布道师。十五年来,其开创的集智俱乐部已经成为全国最活跃、历史最悠久的人工智能俱乐部之一,获得过凯风基金会的赞助,孕育出彩云天气人工智能团队和Momenta自动驾驶项目,先后出版了《科学的极致——漫谈人工智能》、《走近2050——注意力、互联网与人工智能》两本广受欢迎的人工智能科普书。其中《科学的极致》被杨澜在其微博推荐,已反复再版,《走近2050》刚一面市就冲击到了亚马逊计算机类图书排行榜首位。","name":"张江","isAdvertiser":false,"headline":"打造没有‘’围墙‘’的研究所!","gender":1,"url":"\u002Fpeople\u002F6cd4ac54f8fe9179477d510ab0e40235","avatarUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-aec38e760b441fb0b6496c2b9429f865_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":10568,"type":"column"},"c_167341220":{"description":"一些计算机专业领域的学习笔记~","canManage":false,"intro":"微信公众号:轮子工厂,6万多人的选择,值得关注!","isFollowing":true,"urlToken":"c_167341220","id":"c_167341220","articlesCount":324,"acceptSubmission":true,"title":"轮子工厂","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_167341220","commentPermission":"all","created":1519957181,"updated":1599487122,"imageUrl":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-db630cba4a5dcd60ccd306633b89aa93_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd.jpg?source=172ae18b","uid":"58629923274752","userType":"people","isFollowing":false,"urlToken":"tanqingbo","id":"413d6f0390e1f6d0122019a93301ded9","description":"计算机博士在读\n公众号@轮子工厂\n个人网站:http:\u002F\u002Ftanqingbo.cn\u002F\n全网20万精准粉丝\n年懂技术也懂内容\n微信:best_changzhang","name":"谭庆波","isAdvertiser":false,"headline":"公众号:轮子工厂,回复「1024」获取程序员神秘大礼包~","gender":1,"url":"\u002Fpeople\u002F413d6f0390e1f6d0122019a93301ded9","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-2607729058e6550bfcef7b4842bdbfbd_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":4682,"type":"column"},"c_1101886054759501824":{"description":"记录有深度和最新的计算机视觉相关学习笔记!","canManage":false,"intro":"记录有深度和最新的计算机视觉相关文章!","isFollowing":false,"urlToken":"c_1101886054759501824","id":"c_1101886054759501824","articlesCount":3,"acceptSubmission":true,"title":"计算机视觉学习笔记","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_1101886054759501824","commentPermission":"all","created":1555490107,"updated":1591364210,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002F4b70deef7_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic1.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59.jpg?source=172ae18b","uid":"61292979159040","userType":"people","isFollowing":false,"urlToken":"ranjiewen","id":"b5a2a257c0b0fedf0318b73b881c810d","description":"图像处理\u002F计算机视觉\u002F深度学习; \n日常想回成都过日子…","name":"jiewen","isAdvertiser":false,"headline":"保持好奇心......","gender":1,"url":"\u002Fpeople\u002Fb5a2a257c0b0fedf0318b73b881c810d","avatarUrl":"https:\u002F\u002Fpic2.zhimg.com\u002Fv2-61200e7fde967f2de94feaab9ac35c59_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":34,"type":"column"},"c_1293925349546061824":{"description":"","canManage":false,"intro":"最通俗的语言,最完整的知识体系,人人都是CV工程师","isFollowing":false,"urlToken":"c_1293925349546061824","id":"c_1293925349546061824","articlesCount":42,"acceptSubmission":true,"title":"人人都是计算机视觉算法工程师","url":"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fc_1293925349546061824","commentPermission":"all","created":1601275843,"updated":1604925121,"imageUrl":"https:\u002F\u002Fpic4.zhimg.com\u002F4b70deef7_720w.jpg?source=172ae18b","author":{"isFollowed":false,"avatarUrlTemplate":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774.jpg?source=172ae18b","uid":"606094689548505088","userType":"people","isFollowing":false,"urlToken":"caishuxiao","id":"371bedadfda430022a92fc7556d80ef7","description":"永远向有结果的人学习","name":"菜务自由","isAdvertiser":false,"headline":"永远向有结果的人学习","gender":1,"url":"\u002Fpeople\u002F371bedadfda430022a92fc7556d80ef7","avatarUrl":"https:\u002F\u002Fpic4.zhimg.com\u002Fv2-02701966a3ffdb001ab927a5babe9774_l.jpg?source=172ae18b","isOrg":false,"type":"people"},"followers":23,"type":"column"}},"topics":{},"roundtables":{},"favlists":{},"comments":{},"notifications":{},"ebooks":{},"activities":{},"feeds":{},"pins":{},"promotions":{},"drafts":{},"chats":{},"posts":{},"clubs":{},"clubTags":{}},"currentUser":"2bcb30744c0f014ce6702f4d94922945","account":{"lockLevel":{},"unlockTicketStatus":false,"unlockTicket":null,"challenge":[],"errorStatus":false,"message":"","isFetching":false,"accountInfo":{},"urlToken":{"loading":false}},"settings":{"socialBind":null,"inboxMsg":null,"notification":{},"email":{},"privacyFlag":null,"blockedUsers":{"isFetching":false,"paging":{"pageNo":1,"pageSize":6},"data":[]},"blockedFollowees":{"isFetching":false,"paging":{"pageNo":1,"pageSize":6},"data":[]},"ignoredTopics":{"isFetching":false,"paging":{"pageNo":1,"pageSize":6},"data":[]},"restrictedTopics":null,"laboratory":{}},"notification":{},"people":{"profileStatus":{},"activitiesByUser":{},"answersByUser":{},"answersSortByVotesByUser":{},"answersIncludedByUser":{},"votedAnswersByUser":{},"thankedAnswersByUser":{},"voteAnswersByUser":{},"thankAnswersByUser":{},"topicAnswersByUser":{},"zvideosByUser":{},"articlesByUser":{},"articlesSortByVotesByUser":{},"articlesIncludedByUser":{},"pinsByUser":{},"questionsByUser":{},"commercialQuestionsByUser":{},"favlistsByUser":{},"followingByUser":{},"followersByUser":{},"mutualsByUser":{},"followingColumnsByUser":{},"followingQuestionsByUser":{},"followingFavlistsByUser":{},"followingTopicsByUser":{},"publicationsByUser":{},"columnsByUser":{},"allFavlistsByUser":{},"brands":null,"creationsByUser":{},"creationsSortByVotesByUser":{},"creationsFeed":{},"infinity":{}},"env":{"ab":{"config":{"experiments":[{"expId":"launch-qa_art2qa_new-3","expPrefix":"qa_art2qa_new","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-qa_cl_guest-2","expPrefix":"qa_cl_guest","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-qa_column_invite-2","expPrefix":"qa_column_invite","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-qa_repost-2","expPrefix":"qa_repost","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-se_sug_v2-3","expPrefix":"se_sug_v2","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-tp_zrec-8","expPrefix":"tp_zrec","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-us_adjust_new-11","expPrefix":"us_adjust_new","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-us_noti_count-8","expPrefix":"us_noti_count","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_andplay_d-2","expPrefix":"vd_andplay_d","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_hookupplay_an-2","expPrefix":"vd_hookupplay_an","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_ios_play-4","expPrefix":"vd_ios_play","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_timeguide-2","expPrefix":"vd_timeguide","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_video_replay-3","expPrefix":"vd_video_replay","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"launch-vd_zvideo_link-10","expPrefix":"vd_zvideo_link","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"zanswer-2","expPrefix":"zanswer","isDynamicallyUpdated":false,"isRuntime":false,"includeTriggerInfo":false},{"expId":"recnew_2th-3","expPrefix":"recnew_2th","isDynamicallyUpdated":false,"isRuntime":false,"includeTriggerInfo":false},{"expId":"li_video-1","expPrefix":"li_video","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"se_meta_ss-3","expPrefix":"se_meta_ss","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"qa_cdzixun-1","expPrefix":"qa_cdzixun","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"qa_fo_recom_h-2","expPrefix":"qa_fo_recom_h","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"vd_bullet_gui-1","expPrefix":"vd_bullet_gui","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"vd_video_tab-9","expPrefix":"vd_video_tab","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"li_tp_paidanswer-10","expPrefix":"li_tp_paidanswer","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"li_video_section-6","expPrefix":"li_video_section","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"tp_content-2","expPrefix":"tp_content","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"captcha_v2-2_v9","expPrefix":"captcha_v2","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"meta_ebook-1_v1","expPrefix":"meta_ebook","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"club_fn-1_v4","expPrefix":"club_fn","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"webpImg-1_v3","expPrefix":"webpImg","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"general_1-2_v1","expPrefix":"general_1","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"correct_gpu-6_v5","expPrefix":"correct_gpu","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"se_fix_ebook-1_v3","expPrefix":"se_fix_ebook","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"correct_pos-4_v2","expPrefix":"correct_pos","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"rec_new2th-2_v3","expPrefix":"rec_new2th","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"hw_aa_30-1_v2","expPrefix":"hw_aa_30","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false},{"expId":"hw_aa_50-2_v1","expPrefix":"hw_aa_50","isDynamicallyUpdated":true,"isRuntime":false,"includeTriggerInfo":false}],"params":[{"id":"se_cvr_boost","type":"Int","value":"0","chainId":"_gene_","layerId":"se_cvr_boost","key":183},{"id":"rec_new2th","type":"Int","value":"1","chainId":"_gene_","layerId":"Hump","key":320},{"id":"gue_q_share","type":"String","value":"0","layerId":"gueqa_layer_647"},{"id":"pf_adjust","type":"String","value":"1","chainId":"_all_","layerId":"pfus_layer_9"},{"id":"gue_andplayd","type":"String","value":"1","layerId":"guevd_layer_686"},{"id":"gue_iosplay","type":"String","value":"0","layerId":"guevd_layer_896"},{"id":"pfd_newbie2","type":"Int","value":"0","chainId":"_gene_","layerId":"pfd_newbie2","key":71},{"id":"li_sp_mqbk","type":"String","value":"0","chainId":"_all_","layerId":"lili_layer_748"},{"id":"gue_bullet_second","type":"String","value":"1","layerId":"guevd_layer_1"},{"id":"gue_video_replay","type":"String","value":"2","layerId":"guevd_layer_3"},{"id":"ge_sxzx","type":"String","value":"0","chainId":"_gene_","layerId":"gere_layer_990","key":3060},{"id":"ge_sug_v2","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_1000","key":3189},{"id":"ge_usercard1","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_742","key":2740},{"id":"ge_emoji","type":"String","value":"0","chainId":"_gene_","layerId":"getp_layer_827","key":3209},{"id":"se_doc_cnt","type":"Int","value":"0","chainId":"_gene_","layerId":"uD1x","key":302},{"id":"gue_art2qa","type":"String","value":"0","layerId":"gueqa_layer_579"},{"id":"web_collection_guest","type":"String","value":"1","layerId":"webqa_layer_4"},{"id":"zr_expslotpaid","type":"String","value":"1","chainId":"_all_","layerId":"zrrec_layer_11"},{"id":"tp_zrec","type":"String","value":"1","chainId":"_all_","layerId":"tptp_layer_619"},{"id":"gue_vid_tab","type":"String","value":"7","layerId":"guevd_layer_900"},{"id":"li_video_section","type":"String","value":"1","chainId":"_all_","layerId":"lili_layer_7"},{"id":"web_answerlist_ad","type":"String","value":"0","layerId":"webqa_layer_1"},{"id":"se_ffzx_jushen1","type":"String","value":"0","chainId":"_all_","layerId":"sese_layer_4"},{"id":"gue_card_test","type":"String","value":"1","layerId":"gueqa_layer_2"},{"id":"gue_art_ui","type":"String","value":"2","layerId":"gueqa_layer_647"},{"id":"ge_newcard","type":"String","value":"3","chainId":"_gene_","layerId":"geus_layer_839","key":2997},{"id":"ge_upload","type":"String","value":"0","chainId":"_gene_","layerId":"geus_layer_839","key":2892},{"id":"hw_aa_30","type":"Int","value":"0","chainId":"_gene_","layerId":"hw_aa_30","key":361},{"id":"web_heifetz_grow_ad","type":"String","value":"1","layerId":"webgw_layer_3"},{"id":"web_login","type":"String","value":"0","layerId":"webgw_layer_759"},{"id":"gue_goods_card","type":"String","value":"0","layerId":"gueqa_layer_1"},{"id":"gue_v_serial","type":"String","value":"1","layerId":"guevd_layer_695"},{"id":"ge_rec_2th","type":"String","value":"11","chainId":"_gene_","layerId":"geli_layer_965","key":3023},{"id":"li_panswer_topic","type":"String","value":"1","chainId":"_all_","layerId":"lili_layer_602"},{"id":"ge_sug_rep","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_1034","key":3158},{"id":"gue_zvideo_link","type":"String","value":"1","layerId":"guevd_layer_2"},{"id":"qap_question_visitor","type":"String","value":" 0","chainId":"_all_","layerId":"qapqa_layer_2"},{"id":"web_audit_01","type":"String","value":"case1","layerId":"webre_layer_1"},{"id":"ge_newyanzhi","type":"String","value":"0","chainId":"_gene_","layerId":"geus_layer_1019","key":2788},{"id":"show_ad","type":"Int","value":"0","chainId":"_gene_","layerId":"show_ad","key":27},{"id":"hot_card","type":"Int","value":"0","chainId":"_gene_","layerId":"hot_card","key":108},{"id":"web_sem_ab","type":"String","value":"1","layerId":"webgw_layer_3"},{"id":"li_edu_page","type":"String","value":"old","chainId":"_all_","layerId":"lili_layer_580"},{"id":"gue_video_guide","type":"String","value":"1","layerId":"guevd_layer_625"},{"id":"Test_Punk","type":"Int","value":"0","layerId":"Test_Punk"},{"id":"se_fix_ebook","type":"Int","value":"0","chainId":"_gene_","layerId":"se_fix_ebook","key":103},{"id":"ge_prf_rec","type":"String","value":"0","chainId":"_gene_","layerId":"getop_layer_991","key":3040},{"id":"gue_bullet_guide","type":"String","value":"发个弹幕聊聊…","layerId":"guevd_layer_0"},{"id":"zr_slotpaidexp","type":"String","value":"1","chainId":"_all_","layerId":"zrrec_layer_5"},{"id":"tp_dingyue_video","type":"String","value":"0","chainId":"_all_","layerId":"tptp_layer_4"},{"id":"hw_aa_50","type":"Int","value":"1","chainId":"_gene_","layerId":"hw_aa_50","key":362},{"id":"ge_video","type":"String","value":"0","chainId":"_gene_","layerId":"geli_layer_856","key":2831},{"id":"gue_messrec","type":"String","value":"0","layerId":"gueqa_layer_769"},{"id":"web_answer_list_ad","type":"String","value":"1","layerId":"webqa_layer_4"},{"id":"tp_contents","type":"String","value":"2","chainId":"_all_","layerId":"tptp_layer_627"},{"id":"top_test_4_liguangyi","type":"String","value":"1","chainId":"_all_","layerId":"iosus_layer_1"},{"id":"meta_ebook","type":"Int","value":"0","layerId":"meta_ebook"},{"id":"zanswer","type":"Int","value":"1","layerId":"zanswer"},{"id":"webpImg","type":"Int","value":"1","layerId":"JnVt"},{"id":"general_1","type":"Int","value":"2","chainId":"_gene_","layerId":"general_1","key":8},{"id":"gue_cdzixun","type":"String","value":"0","layerId":"gueqa_layer_3"},{"id":"gue_fo_recom","type":"String","value":"0","layerId":"gueqa_layer_780"},{"id":"ge_yuzhi_v1","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_1029","key":3127},{"id":"ge_guess","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_938","key":2912},{"id":"captcha_v2","type":"Int","value":"1","layerId":"captcha_v2"},{"id":"recnew_2th","type":"Int","value":"21","chainId":"_gene_","layerId":"recnew_2th","key":67},{"id":"ge_newbie3","type":"Int","value":"0","chainId":"_gene_","layerId":"ge_newbie3","key":180},{"id":"se_zp_five","type":"Int","value":"0","chainId":"_gene_","layerId":"se_zp_five","key":344},{"id":"ge_search_ui","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_838","key":2898},{"id":"pf_noti_entry_num","type":"String","value":"2","chainId":"_all_","layerId":"pfus_layer_718"},{"id":"se_prerank","type":"Int","value":"0","chainId":"_gene_","layerId":"6XUc","key":354},{"id":"li_vip_verti_search","type":"String","value":"0","chainId":"_all_","layerId":"lili_layer_2"},{"id":"gue_repost","type":"String","value":"1","layerId":"gueqa_layer_671"},{"id":"ge_meta_ss","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_834","key":3079},{"id":"ge_infinity6","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_815","key":2817},{"id":"ge_entity","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_946","key":3036},{"id":"gue_playh_an","type":"String","value":"1","layerId":"guevd_layer_622"},{"id":"li_paid_answer_exp","type":"String","value":"0","chainId":"_all_","layerId":"lili_layer_3"},{"id":"ge_dipin_pre","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_1000","key":3124},{"id":"ge_relation2","type":"String","value":"1","chainId":"_gene_","layerId":"gese_layer_815","key":2796},{"id":"gue_profile_video","type":"String","value":"1","layerId":"guevd_layer_5"},{"id":"pfd_newbie","type":"Int","value":"0","chainId":"_gene_","layerId":"pfd_newbie","key":63},{"id":"correct_gpu","type":"Int","value":"5","chainId":"_gene_","layerId":"correct_gpu","key":66},{"id":"web_scl_rec","type":"String","value":"0","layerId":"webgw_layer_759"},{"id":"ge_hard_s_ma","type":"String","value":"0","chainId":"_gene_","layerId":"geli_layer_856","key":3031},{"id":"gue_visit_n_artcard","type":"String","value":"1","layerId":"gueqa_layer_579"},{"id":"gue_recmess","type":"String","value":"0","layerId":"gueqa_layer_795"},{"id":"qap_question_author","type":"String","value":"0","chainId":"_all_","layerId":"qapqa_layer_2"},{"id":"ge_item","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_945","key":2971},{"id":"club_fn","type":"Int","value":"1","layerId":"club_fn"},{"id":"correct_pos","type":"Int","value":"2","chainId":"_gene_","layerId":"correct_pos","key":104},{"id":"ge_rec_sup","type":"Int","value":"0","chainId":"_gene_","layerId":"ge_rec_sup","key":197},{"id":"se_4a","type":"Int","value":"0","chainId":"_gene_","layerId":"rtiq","key":335},{"id":"gue_sharp","type":"String","value":"1","layerId":"guevd_layer_686"},{"id":"web_column_auto_invite","type":"String","value":"1","layerId":"webqa_layer_1"},{"id":"ge_v_rank_v3","type":"String","value":"0","chainId":"_gene_","layerId":"gese_layer_1047","key":2966},{"id":"tp_topic_style","type":"String","value":"0","chainId":"_all_","layerId":"tptp_layer_4"},{"id":"gue_bulletmb","type":"String","value":"0","layerId":"guevd_layer_812"}],"chains":[{"chainId":"_all_"}],"encodedParams":"ClK3AEABRwD0C3UMtAqJDC4BtQtMC2kBzwtWDOQKGwBsAGcA4AtqAQ8LCAA3DGALQwC0AFgBUgtiAQcMAQvcCzQM7Ao\u002FAEIA1wubC2gAxQBPAZYLEikAAQAAAQAAAAMAAAsBAAAAAAABAAIBABUAAAEAAQAAAAEABQAAAgAAAA=="},"triggers":{}},"userAgent":{"Edge":false,"IE":false,"Wechat":false,"Weibo":false,"QQ":false,"MQQBrowser":false,"Qzone":false,"Mobile":false,"Android":false,"iOS":false,"isAppleDevice":false,"Zhihu":false,"ZhihuHybrid":false,"isBot":false,"Tablet":false,"UC":false,"Sogou":false,"Qihoo":false,"Baidu":false,"BaiduApp":false,"Safari":false,"GoogleBot":false,"AndroidDaily":false,"iOSDaily":false,"WxMiniProgram":false,"BaiduMiniProgram":false,"QQMiniProgram":false,"JDMiniProgram":false,"isWebView":false,"isMiniProgram":false,"origin":"Mozilla\u002F5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko\u002F20100101 Firefox\u002F73.0"},"appViewConfig":{},"ctx":{"path":"\u002Fp\u002F43843694","query":{},"href":"http:\u002F\u002Fzhuanlan.zhihu.com\u002Fp\u002F43843694","host":"zhuanlan.zhihu.com"},"trafficSource":"production","edition":{"beijing":false,"baidu":false,"sogou":false,"baiduBeijing":false,"sogouBeijing":false,"sogouInput":false,"baiduSearch":false,"googleSearch":false,"miniProgram":false,"xiaomi":false},"theme":"light","enableShortcut":true,"referer":"","xUDId":"AHDYFEOJYhKPToQxOOJvuf1dL5wszuk9fro=","mode":"ssr","conf":{},"xTrafficFreeOrigin":"","ipInfo":{"cityName":"里昂","countryName":"法国","regionName":"法国","countryCode":"FR"},"logged":true,"vars":{"passThroughHeaders":{}}},"me":{"columnContributions":[]},"label":{"recognizerLists":{}},"ecommerce":{},"comments":{"pagination":{},"collapsed":{},"reverse":{},"reviewing":{},"conversation":{},"parent":{}},"commentsV2":{"stickers":[],"commentWithPicPermission":{},"notificationsComments":{},"pagination":{},"collapsed":{},"reverse":{},"reviewing":{},"conversation":{},"conversationMore":{},"parent":{}},"pushNotifications":{"default":{"isFetching":false,"isDrained":false,"ids":[]},"follow":{"isFetching":false,"isDrained":false,"ids":[]},"vote_thank":{"isFetching":false,"isDrained":false,"ids":[]},"currentTab":"default","notificationsCount":{"default":0,"follow":0,"vote_thank":0}},"messages":{"data":{},"currentTab":"common","messageCount":0},"register":{"registerValidateSucceeded":null,"registerValidateErrors":{},"registerConfirmError":null,"sendDigitsError":null,"registerConfirmSucceeded":null},"login":{"loginUnregisteredError":false,"loginBindWechatError":false,"loginConfirmError":null,"sendDigitsError":null,"needSMSIdentify":false,"validateDigitsError":false,"loginConfirmSucceeded":null,"qrcodeLoginToken":"","qrcodeLoginScanStatus":0,"qrcodeLoginError":null,"qrcodeLoginReturnNewToken":false},"active":{"sendDigitsError":null,"activeConfirmSucceeded":null,"activeConfirmError":null},"switches":{},"captcha":{"captchaNeeded":false,"captchaValidated":false,"captchaBase64String":null,"captchaValidationMessage":null,"loginCaptchaExpires":false},"sms":{"supportedCountries":[]},"chat":{"chats":{},"inbox":{"recents":{"isFetching":false,"isDrained":false,"isPrevDrained":false,"result":[],"next":null,"key":null},"strangers":{"isFetching":false,"isDrained":false,"isPrevDrained":false,"result":[],"next":null,"key":null},"friends":{"isFetching":false,"isDrained":false,"isPrevDrained":false,"result":[],"next":null,"key":null},"search":{"isFetching":false,"isDrained":false,"isPrevDrained":false,"result":[],"next":null,"key":null},"config":{"newCount":0,"strangerMessageSwitch":false,"strangerMessageUnread":false,"friendCount":0}},"global":{"isChatMqttExisted":false}},"emoticons":{"emoticonGroupList":[],"emoticonGroupDetail":{}},"creator":{"currentCreatorUrlToken":null,"homeData":{"recommendQuestions":[]},"tools":{"question":{"invitationCount":{"questionFolloweeCount":0,"questionTotalCount":0},"goodatTopics":[]},"customPromotion":{"itemLists":{}},"recommend":{"recommendTimes":{}}},"explore":{"academy":{"tabs":[],"article":{}}},"rights":[],"rightsStatus":{},"levelUpperLimit":10,"account":{"growthLevel":{}},"mcn":{},"applyStatus":{},"videoSupport":{},"mcnManage":{},"tasks":{},"recentlyCreated":[]},"answers":{"voters":{},"copyrightApplicants":{},"favlists":{},"newAnswer":{},"concernedUpvoters":{},"simpleConcernedUpvoters":{},"paidContent":{},"settings":{}},"recommendation":{"homeRecommendations":[]},"shareTexts":{},"articles":{"voters":{}},"previewPost":{},"favlists":{"relations":{}},"columns":{"voters":{}},"reward":{"answer":{},"article":{},"question":{}},"video":{"data":{},"shareVideoDetail":{},"last":{}},"topstory":{"recommend":{"isFetching":false,"isDrained":false,"afterId":0,"items":[],"next":null},"follow":{"isFetching":false,"isDrained":false,"afterId":0,"items":[],"next":null},"room":{"meta":{},"isFetching":false,"afterId":0,"items":[],"next":null},"followWonderful":{"isFetching":false,"isDrained":false,"afterId":0,"items":[],"next":null},"sidebar":null,"announcement":{},"hotListCategories":[],"hotList":[],"guestFeeds":{"isFetching":false,"isDrained":false,"afterId":0,"items":[],"next":null},"followExtra":{"isNewUser":null,"isFetched":false,"followCount":0,"followers":[]},"hotDaily":{"data":[],"paging":{}},"hotHighlight":{"isFetching":false,"isDrained":false,"data":[],"paging":{}}},"readStatus":{},"column":{},"requestColumn":{"categories":[],"error":null},"articleContribution":{"contributeRequests":[],"deleteContributeIdList":[],"handledContributeIdList":[],"recommendedColumns":[],"pinnedColumns":[],"sentContributeRequestsIdList":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"c_80412427","c_167341220","c_1101886054759501824","c_1293925349546061824"]},"columnContribution":{"contributeRequests":[],"autoInviteEnabled":false,"recommendedContributors":[],"contributionInvitation":null},"draftHistory":{"history":{},"drafts":{}},"upload":{},"articleDraft":{"titleImage":"","titleImageSize":{},"isTitleImageFullScreen":false,"canTitleImageFullScreen":false,"title":"","titleImageUploading":false,"error":"","content":"","draftLoading":false,"updating":false,"globalLoading":false,"pendingVideo":{"resource":null,"error":null},"deleteFail":{"fail":false},"recommendTopics":[],"selectedColumn":0,"articleDisclaimers":[]},"articleDrafts":{"isDrained":false,"isLoading":false,"items":[]},"columnAutocomplete":{"users":[],"friends":[]},"columnCollection":{},"userProfit":{"permission":{"permissionStatus":{"zhiZixuan":0,"recommend":-1,"task":0,"plugin":0},"visible":false}},"mcn":{"bindInfo":{},"memberCategoryList":[],"producerList":[],"categoryList":[],"lists":{},"banners":{},"protocolStatus":{"isAgreedNew":true,"isAgreedOld":true},"probationCountdownDays":0},"zvideos":{"campaigns":{},"tagoreCategory":[],"recommendations":{},"insertable":{},"recruit":{"form":{"platform":"","nickname":"","followerCount":"","domain":"","contact":""},"submited":false,"ranking":[]},"club":{}},"republish":{}},"fetchHost":"www.zhihu.com","subAppName":"column"}</script><script src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/vendor.js"></script><script src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/column_006.js"></script><script src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/hm.js" async=""></script><script src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/zap.js"></script><script src="Pytorch%20%20detach%20%E5%92%8C%20retain_graph%20-%20%E7%9F%A5%E4%B9%8E_files/push.js"></script><div><div style="display: none;">想来知乎工作?请发送邮件到 [email protected]</div></div><div><div><div class="css-8pdeid"></div></div></div><div><div><div style="left: -1179px; top: -999px;" class="Editable-languageSuggestions"><div><div class="Popover"><label class="Editable-languageSuggestionsInput Input-wrapper"><input autocomplete="off" role="combobox" aria-expanded="false" aria-autocomplete="list" aria-activedescendant="AutoComplete19-0" id="Popover18-toggle" aria-haspopup="true" aria-owns="Popover18-content" class="Input" placeholder="选择语言"><svg class="Zi Zi--Select" fill="#afbdcf" viewBox="0 0 24 24" width="24" height="24"><path d="M12 16.183l2.716-2.966a.757.757 0 0 1 1.064.001.738.738 0 0 1 0 1.052l-3.247 3.512a.758.758 0 0 1-1.064 0L8.22 14.27a.738.738 0 0 1 0-1.052.758.758 0 0 1 1.063 0L12 16.183zm0-9.365L9.284 9.782a.758.758 0 0 1-1.064 0 .738.738 0 0 1 0-1.052l3.248-3.512a.758.758 0 0 1 1.065 0L15.78 8.73a.738.738 0 0 1 0 1.052.757.757 0 0 1-1.063.001L12 6.818z" fill-rule="evenodd"></path></svg></label></div></div></div></div></div></body></html>