gan_api.html 59.3 KB
Newer Older
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


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Design for GAN &mdash; PaddlePaddle  文档</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  
  
        <link rel="index" title="索引"
              href="../genindex.html"/>
        <link rel="search" title="搜索" href="../search.html"/>
    <link rel="top" title="PaddlePaddle  文档" href="../index.html"/> 

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/css/perfect-scrollbar.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/override.css" type="text/css" />
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?b9a314ab40d04d805655aab1deee08ba";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>

  

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  
  <header class="site-header">
    <div class="site-logo">
      <a href="/"><img src="../_static/images/PP_w.png"></a>
    </div>
    <div class="site-nav-links">
      <div class="site-menu">
        <a class="fork-on-github" href="https://github.com/PaddlePaddle/Paddle" target="_blank"><i class="fa fa-github"></i>Fork me on Github</a>
        <div class="language-switcher dropdown">
          <a type="button" data-toggle="dropdown">
            <span>English</span>
            <i class="fa fa-angle-up"></i>
            <i class="fa fa-angle-down"></i>
          </a>
          <ul class="dropdown-menu">
            <li><a href="/doc_cn">中文</a></li>
            <li><a href="/doc">English</a></li>
          </ul>
        </div>
        <ul class="site-page-links">
          <li><a href="/">Home</a></li>
        </ul>
      </div>
      <div class="doc-module">
        
        <ul>
<li class="toctree-l1"><a class="reference internal" href="../getstarted/index_cn.html">新手入门</a></li>
<li class="toctree-l1"><a class="reference internal" href="../howto/index_cn.html">进阶指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/index_cn.html">API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index_cn.html">FAQ</a></li>
88
<li class="toctree-l1"><a class="reference internal" href="../mobile/index_cn.html">MOBILE</a></li>
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
</ul>

        
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>        
      </div>
    </div>
  </header>
  
  <div class="main-content-wrap">

    
    <nav class="doc-menu-vertical" role="navigation">
        
          
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../getstarted/index_cn.html">新手入门</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../getstarted/build_and_install/index_cn.html">安装与编译</a><ul>
112 113
<li class="toctree-l3"><a class="reference internal" href="../getstarted/build_and_install/pip_install_cn.html">使用pip安装</a></li>
<li class="toctree-l3"><a class="reference internal" href="../getstarted/build_and_install/docker_install_cn.html">使用Docker安装运行</a></li>
114
<li class="toctree-l3"><a class="reference internal" href="../howto/dev/build_cn.html">用Docker编译和测试PaddlePaddle</a></li>
115
<li class="toctree-l3"><a class="reference internal" href="../getstarted/build_and_install/build_from_source_cn.html">从源码编译</a></li>
116 117 118 119 120 121 122 123 124 125 126 127
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../getstarted/concepts/use_concepts_cn.html">基本使用概念</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../howto/index_cn.html">进阶指南</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../howto/usage/cmd_parameter/index_cn.html">设置命令行参数</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../howto/usage/cmd_parameter/use_case_cn.html">使用案例</a></li>
<li class="toctree-l3"><a class="reference internal" href="../howto/usage/cmd_parameter/arguments_cn.html">参数概述</a></li>
<li class="toctree-l3"><a class="reference internal" href="../howto/usage/cmd_parameter/detail_introduction_cn.html">细节描述</a></li>
</ul>
</li>
128
<li class="toctree-l2"><a class="reference internal" href="../howto/usage/cluster/cluster_train_cn.html">PaddlePaddle分布式训练</a></li>
129 130 131
<li class="toctree-l2"><a class="reference internal" href="../howto/usage/k8s/k8s_basis_cn.html">Kubernetes 简介</a></li>
<li class="toctree-l2"><a class="reference internal" href="../howto/usage/k8s/k8s_cn.html">Kubernetes单机训练</a></li>
<li class="toctree-l2"><a class="reference internal" href="../howto/usage/k8s/k8s_distributed_cn.html">Kubernetes分布式训练</a></li>
132
<li class="toctree-l2"><a class="reference internal" href="../howto/dev/contribute_to_paddle_cn.html">如何贡献代码</a></li>
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<li class="toctree-l2"><a class="reference internal" href="../howto/dev/write_docs_cn.html">如何贡献/修改文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../howto/deep_model/rnn/index_cn.html">RNN相关模型</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../howto/deep_model/rnn/rnn_config_cn.html">RNN配置</a></li>
<li class="toctree-l3"><a class="reference internal" href="../howto/deep_model/rnn/recurrent_group_cn.html">Recurrent Group教程</a></li>
<li class="toctree-l3"><a class="reference internal" href="../howto/deep_model/rnn/hierarchical_layer_cn.html">支持双层序列作为输入的Layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../howto/deep_model/rnn/hrnn_rnn_api_compare_cn.html">单双层RNN API对比介绍</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../howto/optimization/gpu_profiling_cn.html">GPU性能分析与调优</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../api/index_cn.html">API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../api/v2/model_configs.html">模型配置</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/activation.html">Activation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/layer.html">Layers</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/evaluators.html">Evaluators</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/optimizer.html">Optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/pooling.html">Pooling</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/networks.html">Networks</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/config/attr.html">Parameter Attribute</a></li>
</ul>
</li>
155 156 157 158 159 160
<li class="toctree-l2"><a class="reference internal" href="../api/v2/data.html">数据访问</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/data/data_reader.html">Data Reader Interface</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/data/image.html">Image Interface</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/data/dataset.html">Dataset</a></li>
</ul>
</li>
161
<li class="toctree-l2"><a class="reference internal" href="../api/v2/run_logic.html">训练与应用</a></li>
162 163 164 165 166 167 168 169 170 171 172 173 174
<li class="toctree-l2"><a class="reference internal" href="../api/v2/fluid.html">Fluid</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/layers.html">Layers</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/data_feeder.html">DataFeeder</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/executor.html">Executor</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/initializer.html">Initializer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/evaluator.html">Evaluator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/nets.html">Nets</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/optimizer.html">Optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/param_attr.html">ParamAttr</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/profiler.html">Profiler</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/v2/fluid/regularizer.html">Regularizer</a></li>
</ul>
</li>
175 176 177 178 179 180 181 182 183 184
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index_cn.html">FAQ</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../faq/build_and_install/index_cn.html">编译安装与单元测试</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq/model/index_cn.html">模型配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq/parameter/index_cn.html">参数设置</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq/local/index_cn.html">本地训练与预测</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq/cluster/index_cn.html">集群训练与预测</a></li>
</ul>
</li>
185
<li class="toctree-l1"><a class="reference internal" href="../mobile/index_cn.html">MOBILE</a><ul>
186 187 188
<li class="toctree-l2"><a class="reference internal" href="../mobile/cross_compiling_for_android_cn.html">Android平台编译指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mobile/cross_compiling_for_ios_cn.html">iOS平台编译指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mobile/cross_compiling_for_raspberry_cn.html">Raspberry Pi平台编译指南</a></li>
189 190
</ul>
</li>
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
</ul>

        
    </nav>
    
    <section class="doc-content-wrap">

      

 







<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
      
    <li>Design for GAN</li>
  </ul>
</div>
      
      <div class="wy-nav-content" id="doc-content">
        <div class="rst-content">
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="design-for-gan">
<span id="design-for-gan"></span><h1>Design for GAN<a class="headerlink" href="#design-for-gan" title="永久链接至标题"></a></h1>
<p>GAN (General Adversarial Net [https://arxiv.org/abs/1406.2661]) is an important model for unsupervised learning and widely used in many areas.</p>
<p>It applies several important concepts in machine learning system design, including building and running subgraphs, dependency tracing, different optimizers in one executor and so forth.</p>
<p>In our GAN design, we wrap it as a user-friendly easily customized python API to design different models. We take the conditional DC-GAN (Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks [https://arxiv.org/abs/1511.06434]) as an example due to its good performance on image generation.</p>
<p align="center">
<img src="./test.dot.png" width = "35%" align="center"/><br/>
Figure 1. The overall running logic of GAN. The black solid arrows indicate the forward pass; the green dashed arrows indicate the backward pass of generator training; the red dashed arrows indicate the backward pass of the discriminator training. The BP pass of the green (red) arrow should only update the parameters in the green (red) boxes. The diamonds indicate the data providers. d\_loss and g\_loss marked in red and green are the two targets we would like to run.
</p><p>The operators, layers and functions required/optional to build a GAN demo is summarized in https://github.com/PaddlePaddle/Paddle/issues/4563.</p>
<p align="center">
<img src="./dcgan.png" width = "90%" align="center"/><br/>
Figure 2. Photo borrowed from the original DC-GAN paper.
</p><div class="section" id="the-conditional-gan-might-be-a-class">
<span id="the-conditional-gan-might-be-a-class"></span><h2>The Conditional-GAN might be a class.<a class="headerlink" href="#the-conditional-gan-might-be-a-class" title="永久链接至标题"></a></h2>
<p>This design we adopt the popular open source design in https://github.com/carpedm20/DCGAN-tensorflow and https://github.com/rajathkmp/DCGAN. It contains following data structure:</p>
<ul class="simple">
<li>DCGAN(object): which contains everything required to build a GAN model. It provides following member functions methods as API:</li>
<li><strong>init</strong>(...): Initialize hyper-parameters (like conv dimension and so forth), and declare model parameters of discriminator and generator as well.</li>
<li>generator(z, y=None): Generate a fake image from input noise z. If the label y is provided, the conditional GAN model will be chosen.
Returns a generated image.</li>
<li>discriminator(image):
Given an image, decide if it is from a real source or a fake one.
Returns a 0/1 binary label.</li>
<li>build_model(self):
build the whole GAN model, define training loss for both generator and discrimator.</li>
</ul>
</div>
<div class="section" id="discussion-on-engine-functions-required-to-build-gan">
<span id="discussion-on-engine-functions-required-to-build-gan"></span><h2>Discussion on Engine Functions required to build GAN<a class="headerlink" href="#discussion-on-engine-functions-required-to-build-gan" title="永久链接至标题"></a></h2>
<ul class="simple">
<li>Trace the tensor and variable dependency in the engine executor. (Very critical, otherwise GAN can&#8217;be be trained correctly)</li>
<li>Different optimizers responsible for optimizing different loss.</li>
</ul>
<p>To be more detailed, we introduce our design of DCGAN as following:</p>
<div class="section" id="class-member-function-initializer">
<span id="class-member-function-initializer"></span><h3>Class member Function: Initializer<a class="headerlink" href="#class-member-function-initializer" title="永久链接至标题"></a></h3>
<ul class="simple">
<li>Set up hyper-parameters, including condtional dimension, noise dimension, batch size and so forth.</li>
<li>Declare and define all the model variables. All the discriminator parameters are included in the list self.theta_D and all the generator parameters are included in the list self.theta_G.</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DCGAN</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">y_dim</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  
    <span class="c1"># hyper parameters  </span>
    <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span> <span class="o">=</span> <span class="n">y_dim</span> <span class="c1"># conditional gan or not</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="mi">100</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">z_dim</span> <span class="o">=</span> <span class="n">z_dim</span> <span class="c1"># input noise dimension</span>

    <span class="c1"># define parameters of discriminators</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_W0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">128</span><span class="p">],</span> <span class="n">data</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">gaussian_normal_randomizer</span><span class="p">())</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_b0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span> <span class="c1"># variable also support initialization using a  numpy data</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_W1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="mi">784</span><span class="p">,</span> <span class="mi">128</span><span class="p">],</span> <span class="n">data</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">gaussian_normal_randomizer</span><span class="p">())</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_b1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span> <span class="c1"># variable also support initialization using a  numpy data</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_W2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Varialble</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">rand</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">D_b2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">theta_D</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">D_W0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D_b0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D_W1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D_b1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D_W2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">D_b2</span><span class="p">]</span>

    <span class="c1"># define parameters of generators</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_W0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="mi">784</span><span class="p">,</span> <span class="mi">128</span><span class="p">],</span> <span class="n">data</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">gaussian_normal_randomizer</span><span class="p">())</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_b0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span> <span class="c1"># variable also support initialization using a  numpy data</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_W1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="mi">784</span><span class="p">,</span> <span class="mi">128</span><span class="p">],</span> <span class="n">data</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">gaussian_normal_randomizer</span><span class="p">())</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_b1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span> <span class="c1"># variable also support initialization using a  numpy data</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_W2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Varialble</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">rand</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">G_b2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">128</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">theta_G</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">G_W0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_W1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_W2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b2</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="class-member-function-generator">
<span id="class-member-function-generator"></span><h3>Class member Function: Generator<a class="headerlink" href="#class-member-function-generator" title="永久链接至标题"></a></h3>
<ul class="simple">
<li>Given a noisy input z, returns a fake image.</li>
<li>Concatenation, batch-norm, FC operations required;</li>
<li>Deconv layer required, which is missing now...</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DCGAN</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="nf">generator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="bp">None</span><span class="p">):</span>
    <span class="c1"># input z: the random noise</span>
    <span class="c1"># input y: input data label (optional)</span>
    <span class="c1"># output G_im: generated fake images</span>
    
    <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span>
      <span class="n">z</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="n">z</span><span class="p">,</span> <span class="n">y</span><span class="p">])</span>
      
    <span class="n">G_h0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_w0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b0</span><span class="p">)</span>
    <span class="n">G_h0_bn</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">batch_norm</span><span class="p">(</span><span class="n">G_h0</span><span class="p">)</span>
    <span class="n">G_h0_relu</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">G_h0_bn</span><span class="p">)</span>
    
    <span class="n">G_h1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">deconv</span><span class="p">(</span><span class="n">G_h0_relu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_w1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b1</span><span class="p">)</span>
    <span class="n">G_h1_bn</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">batch_norm</span><span class="p">(</span><span class="n">G_h1</span><span class="p">)</span>
    <span class="n">G_h1_relu</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">G_h1_bn</span><span class="p">)</span>
    
    <span class="n">G_h2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">deconv</span><span class="p">(</span><span class="n">G_h1_relu</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_W2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">G_b2</span><span class="p">))</span>
    <span class="n">G_im</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">tanh</span><span class="p">(</span><span class="n">G_im</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">G_im</span>
</pre></div>
</div>
</div>
<div class="section" id="class-member-function-discriminator">
<span id="class-member-function-discriminator"></span><h3>Class member function: Discriminator<a class="headerlink" href="#class-member-function-discriminator" title="永久链接至标题"></a></h3>
<ul class="simple">
<li>Given a noisy input z, returns a fake image.</li>
<li>Concatenation, Convolution, batch-norm, FC, Leaky-ReLU operations required;</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DCGAN</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="nf">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">image</span><span class="p">):</span>
    <span class="c1"># input image: either generated images or real ones</span>
    <span class="c1"># output D_h2: binary logit of the label</span>

    <span class="n">D_h0</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">conv2d</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">w</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_w0</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_b0</span><span class="p">)</span>
    <span class="n">D_h0_bn</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">batchnorm</span><span class="p">(</span><span class="n">h0</span><span class="p">)</span>
    <span class="n">D_h0_relu</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">lrelu</span><span class="p">(</span><span class="n">h0_bn</span><span class="p">)</span>
    
    <span class="n">D_h1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">conv2d</span><span class="p">(</span><span class="n">D_h0_relu</span><span class="p">,</span> <span class="n">w</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_w1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_b1</span><span class="p">)</span>
    <span class="n">D_h1_bn</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">batchnorm</span><span class="p">(</span><span class="n">D_h1</span><span class="p">)</span>
    <span class="n">D_h1_relu</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">lrelu</span><span class="p">(</span><span class="n">D_h1_bn</span><span class="p">)</span>
    
    <span class="n">D_h2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">D_h1_relu</span><span class="p">,</span> <span class="n">w</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_w2</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">D_b2</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">D_h2</span>
</pre></div>
</div>
</div>
<div class="section" id="class-member-function-build-the-model">
<span id="class-member-function-build-the-model"></span><h3>Class member function: Build the model<a class="headerlink" href="#class-member-function-build-the-model" title="永久链接至标题"></a></h3>
<ul class="simple">
<li>Define data readers as placeholders to hold the data;</li>
<li>Build generator and discriminators;</li>
<li>Define two training losses for discriminator and generator, respectively.
If we have execution dependency engine to back-trace all tensors, the module building our GAN model will be like this:</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DCGAN</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">])</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">images</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">])</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">faked_images</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">])</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">z_size</span><span class="p">])</span>
    
    <span class="c1"># step 1: generate images by generator, classify real/fake images with discriminator</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span> <span class="c1"># if conditional GAN, includes label</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">G</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_t</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
        <span class="c1"># generated fake images</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sampled</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">G</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span> <span class="c1"># original version of GAN</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">G</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_t</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
        <span class="c1"># generate fake images</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sampled</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sampler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
    
    <span class="c1"># step 2: define the two losses</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_real</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_t</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_fake</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_f</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">d_loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_real</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_fake</span>
    
    <span class="bp">self</span><span class="o">.</span><span class="n">g_loss</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_f</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_szie</span><span class="p">))</span>
</pre></div>
</div>
<p>If we do not have dependency engine but blocks, the module building our GAN model will be like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DCGAN</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">default_block</span><span class="p">):</span>
    <span class="c1"># input data in the default block</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">])</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">images</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">im_size</span><span class="p">])</span>
    <span class="c1"># self.faked_images = pd.data(pd.float32, [self.batch_size, self.im_size, self.im_size])</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">z_size</span><span class="p">])</span>

    <span class="c1"># step 1: generate images by generator, classify real/fake images with discriminator</span>
    <span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">default_block</span><span class="p">()</span><span class="o">.</span><span class="n">g_block</span><span class="p">():</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span> <span class="c1"># if conditional GAN, includes label</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">G</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_g</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">G</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span> <span class="c1"># original version of GAN</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">G</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_g</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">G</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">g_loss</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_g</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_szie</span><span class="p">))</span>
    
    <span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">default_block</span><span class="p">()</span><span class="o">.</span><span class="n">d_block</span><span class="p">():</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_dim</span><span class="p">:</span> <span class="c1"># if conditional GAN, includes label</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_t</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">G</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span> <span class="c1"># original version of GAN</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_t</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">D_f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">G</span><span class="p">)</span>

      <span class="c1"># step 2: define the two losses</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_real</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_t</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">))</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_fake</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">cross_entropy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">D_f</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">))</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">d_loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_real</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">d_loss_fake</span>
</pre></div>
</div>
<p>Some small confusion and problems with this design:</p>
<ul class="simple">
<li>D_g and D_f are actually the same thing, but has to be written twice; i.e., if we want to run two sub-graphs conceptually, the same codes have to be written twice if they are shared by the graph.</li>
<li>Requires ability to create a block anytime, rather than in if-else or rnn only;</li>
</ul>
</div>
</div>
<div class="section" id="main-function-for-the-demo">
<span id="main-function-for-the-demo"></span><h2>Main function for the demo:<a class="headerlink" href="#main-function-for-the-demo" title="永久链接至标题"></a></h2>
<p>Generally, the user of GAN just need to the following things:</p>
<ul class="simple">
<li>Define an object as DCGAN class;</li>
<li>Build the DCGAN model;</li>
<li>Specify two optimizers for two different losses with respect to different parameters.</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># pd for short, should be more concise.</span>
<span class="kn">from</span> <span class="nn">paddle.v2</span> <span class="nn">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">logging</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="c1"># dcgan class in the default graph/block</span>
    <span class="c1"># if we use dependency engine as tensorflow</span>
    <span class="c1"># the codes, will be slightly different like:</span>
    <span class="c1"># dcgan = DCGAN()</span>
    <span class="c1"># dcgan.build_model()</span>
    <span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">block</span><span class="p">()</span> <span class="k">as</span> <span class="n">def_block</span><span class="p">:</span>
      <span class="n">dcgan</span> <span class="o">=</span> <span class="n">DCGAN</span><span class="p">()</span>
      <span class="n">dcgan</span><span class="o">.</span><span class="n">build_model</span><span class="p">(</span><span class="n">def_block</span><span class="p">)</span>

    <span class="c1"># load mnist data</span>
    <span class="n">data_X</span><span class="p">,</span> <span class="n">data_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load_mnist</span><span class="p">()</span>
    
    <span class="c1"># Two subgraphs required!!!</span>
    <span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">block</span><span class="p">()</span><span class="o">.</span><span class="n">d_block</span><span class="p">():</span>
      <span class="n">d_optim</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">lr</span> <span class="o">=</span> <span class="o">.</span><span class="mo">001</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span> <span class="o">.</span><span class="mi">1</span><span class="p">)</span>
      <span class="n">d_step</span> <span class="o">=</span> <span class="n">d_optim</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">dcgan</span><span class="o">.</span><span class="n">d_loss</span><span class="p">,</span> <span class="n">dcgan</span><span class="o">.</span><span class="n">theta_D</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">pd</span><span class="o">.</span><span class="n">block</span><span class="o">.</span><span class="n">g_block</span><span class="p">():</span>
      <span class="n">g_optim</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">lr</span> <span class="o">=</span> <span class="o">.</span><span class="mo">001</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span> <span class="o">.</span><span class="mi">1</span><span class="p">)</span>
      <span class="n">g_step</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">dcgan</span><span class="o">.</span><span class="n">g_loss</span><span class="p">,</span> <span class="n">dcgan</span><span class="o">.</span><span class="n">theta_G</span><span class="p">)</span>

    <span class="c1"># executor</span>
    <span class="n">sess</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">executor</span><span class="p">()</span>
    
    <span class="c1"># training</span>
    <span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10000</span><span class="p">):</span>
      <span class="k">for</span> <span class="n">batch_id</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span> <span class="o">/</span> <span class="n">batch_size</span><span class="p">):</span>
        <span class="n">idx</span> <span class="o">=</span> <span class="o">...</span>
        <span class="c1"># sample a batch</span>
        <span class="n">batch_im</span><span class="p">,</span> <span class="n">batch_label</span> <span class="o">=</span> <span class="n">data_X</span><span class="p">[</span><span class="n">idx</span><span class="p">:</span><span class="n">idx</span><span class="o">+</span><span class="n">batch_size</span><span class="p">],</span> <span class="n">data_y</span><span class="p">[</span><span class="n">idx</span><span class="p">:</span><span class="n">idx</span><span class="o">+</span><span class="n">batch_size</span><span class="p">]</span>
        <span class="c1"># sample z</span>
        <span class="n">batch_z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="o">-</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="p">[</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">z_dim</span><span class="p">])</span>

        <span class="k">if</span> <span class="n">batch_id</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
          <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">d_step</span><span class="p">,</span> 
                   <span class="n">feed_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">dcgan</span><span class="o">.</span><span class="n">images</span><span class="p">:</span> <span class="n">batch_im</span><span class="p">,</span>
                                <span class="n">dcgan</span><span class="o">.</span><span class="n">y</span><span class="p">:</span> <span class="n">batch_label</span><span class="p">,</span>
                                <span class="n">dcgan</span><span class="o">.</span><span class="n">z</span><span class="p">:</span> <span class="n">batch_z</span><span class="p">})</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">g_step</span><span class="p">,</span>
                   <span class="n">feed_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">dcgan</span><span class="o">.</span><span class="n">z</span><span class="p">:</span> <span class="n">batch_z</span><span class="p">})</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="more-thinking-about-dependency-engine-v-s-block-design">
<span id="more-thinking-about-dependency-engine-v-s-block-design"></span><h1>More thinking about dependency engine v.s. block design:<a class="headerlink" href="#more-thinking-about-dependency-engine-v-s-block-design" title="永久链接至标题"></a></h1>
<ul class="simple">
<li>What if we just want to run an intermediate result? Do we need to run the whole block/graph?</li>
<li>Should we call eval() to get the fake images in the first stage? And then train the discriminator in the second stage?</li>
</ul>
</div>


           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2016, PaddlePaddle developers.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: ".txt",
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="../_static/translations.js"></script>
      <script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>
       
  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  
  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/js/perfect-scrollbar.jquery.min.js"></script>
  <script src="../_static/js/paddle_doc_init.js"></script> 

</body>
</html>