GAN (General Adversarial Net [https://arxiv.org/abs/1406.2661]) is an important model for unsupervised learning and widely used in many areas.
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.
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.
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>
The operators, layers and functions required/optional to build a GAN demo is summarized in https://github.com/PaddlePaddle/Paddle/issues/4563.
Figure 2. Photo borrowed from the original DC-GAN paper.
</p>
## The Conditional-GAN might be a class.
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:
- DCGAN(object): which contains everything required to build a GAN model. It provides following member functions methods as API:
- __init__(...): Initialize hyper-parameters (like conv dimension and so forth), and declare model parameters of discriminator and generator as well.
- 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.
- discriminator(image):
Given an image, decide if it is from a real source or a fake one.
Returns a 0/1 binary label.
- build_model(self):
build the whole GAN model, define training loss for both generator and discrimator.
## Discussion on Engine Functions required to build GAN
- Trace the tensor and variable dependency in the engine executor. (Very critical, otherwise GAN can'be be trained correctly)
- Different optimizers responsible for optimizing different loss.
To be more detailed, we introduce our design of DCGAN as following:
### Class member Function: Initializer
- Set up hyper-parameters, including condtional dimension, noise dimension, batch size and so forth.
- 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.
Some small confusion and problems with this design:
- 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.
- Requires ability to create a block anytime, rather than in if-else or rnn only;
## Main function for the demo:
Generally, the user of GAN just need to the following things:
- Define an object as DCGAN class;
- Build the DCGAN model;
- Specify two optimizers for two different losses with respect to different parameters.
<liclass="toctree-l2"><aclass="reference internal"href="../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/docker_install_en.html">PaddlePaddle in Docker Containers</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/build_from_source_en.html">Installing from Sources</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_en.html">Paddle On Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_aws_en.html">Distributed PaddlePaddle Training on AWS with Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/build_en.html">Build PaddlePaddle from Source Code and Run Unit Test</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/new_layer_en.html">Write New Layers</a></li>
<spanid="design-for-gan"></span><h1>Design for GAN<aclass="headerlink"href="#design-for-gan"title="Permalink to this headline">¶</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>
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>
<spanid="the-conditional-gan-might-be-a-class"></span><h2>The Conditional-GAN might be a class.<aclass="headerlink"href="#the-conditional-gan-might-be-a-class"title="Permalink to this headline">¶</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>
<ulclass="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>
<spanid="discussion-on-engine-functions-required-to-build-gan"></span><h2>Discussion on Engine Functions required to build GAN<aclass="headerlink"href="#discussion-on-engine-functions-required-to-build-gan"title="Permalink to this headline">¶</a></h2>
<ulclass="simple">
<li>Trace the tensor and variable dependency in the engine executor. (Very critical, otherwise GAN can’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>
<spanid="class-member-function-initializer"></span><h3>Class member Function: Initializer<aclass="headerlink"href="#class-member-function-initializer"title="Permalink to this headline">¶</a></h3>
<ulclass="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>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="o">=</span><spanclass="n">y_dim</span><spanclass="c1"># conditional gan or not</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">D_b0</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">D_b1</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">G_b0</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">G_b1</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanid="class-member-function-generator"></span><h3>Class member Function: Generator<aclass="headerlink"href="#class-member-function-generator"title="Permalink to this headline">¶</a></h3>
<ulclass="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>
<spanid="class-member-function-discriminator"></span><h3>Class member function: Discriminator<aclass="headerlink"href="#class-member-function-discriminator"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li>Given a noisy input z, returns a fake image.</li>
<spanid="class-member-function-build-the-model"></span><h3>Class member function: Build the model<aclass="headerlink"href="#class-member-function-build-the-model"title="Permalink to this headline">¶</a></h3>
<ulclass="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>
<spanclass="c1"># step 1: generate images by generator, classify real/fake images with discriminator</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<p>Some small confusion and problems with this design:</p>
<ulclass="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>
<spanid="main-function-for-the-demo"></span><h2>Main function for the demo:<aclass="headerlink"href="#main-function-for-the-demo"title="Permalink to this headline">¶</a></h2>
<p>Generally, the user of GAN just need to the following things:</p>
<ulclass="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>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># pd for short, should be more concise.</span>
<spanid="more-thinking-about-dependency-engine-v-s-block-design"></span><h1>More thinking about dependency engine v.s. block design:<aclass="headerlink"href="#more-thinking-about-dependency-engine-v-s-block-design"title="Permalink to this headline">¶</a></h1>
<ulclass="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>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.
GAN (General Adversarial Net [https://arxiv.org/abs/1406.2661]) is an important model for unsupervised learning and widely used in many areas.
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.
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.
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>
The operators, layers and functions required/optional to build a GAN demo is summarized in https://github.com/PaddlePaddle/Paddle/issues/4563.
Figure 2. Photo borrowed from the original DC-GAN paper.
</p>
## The Conditional-GAN might be a class.
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:
- DCGAN(object): which contains everything required to build a GAN model. It provides following member functions methods as API:
- __init__(...): Initialize hyper-parameters (like conv dimension and so forth), and declare model parameters of discriminator and generator as well.
- 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.
- discriminator(image):
Given an image, decide if it is from a real source or a fake one.
Returns a 0/1 binary label.
- build_model(self):
build the whole GAN model, define training loss for both generator and discrimator.
## Discussion on Engine Functions required to build GAN
- Trace the tensor and variable dependency in the engine executor. (Very critical, otherwise GAN can'be be trained correctly)
- Different optimizers responsible for optimizing different loss.
To be more detailed, we introduce our design of DCGAN as following:
### Class member Function: Initializer
- Set up hyper-parameters, including condtional dimension, noise dimension, batch size and so forth.
- 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.
Some small confusion and problems with this design:
- 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.
- Requires ability to create a block anytime, rather than in if-else or rnn only;
## Main function for the demo:
Generally, the user of GAN just need to the following things:
- Define an object as DCGAN class;
- Build the DCGAN model;
- Specify two optimizers for two different losses with respect to different parameters.
<spanid="design-for-gan"></span><h1>Design for GAN<aclass="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>
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>
<spanid="the-conditional-gan-might-be-a-class"></span><h2>The Conditional-GAN might be a class.<aclass="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>
<ulclass="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>
<spanid="discussion-on-engine-functions-required-to-build-gan"></span><h2>Discussion on Engine Functions required to build GAN<aclass="headerlink"href="#discussion-on-engine-functions-required-to-build-gan"title="永久链接至标题">¶</a></h2>
<ulclass="simple">
<li>Trace the tensor and variable dependency in the engine executor. (Very critical, otherwise GAN can’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>
<spanid="class-member-function-initializer"></span><h3>Class member Function: Initializer<aclass="headerlink"href="#class-member-function-initializer"title="永久链接至标题">¶</a></h3>
<ulclass="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>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="o">=</span><spanclass="n">y_dim</span><spanclass="c1"># conditional gan or not</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">D_b0</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">D_b1</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">G_b0</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">G_b1</span><spanclass="o">=</span><spanclass="n">pd</span><spanclass="o">.</span><spanclass="n">Variable</span><spanclass="p">(</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">zeros</span><spanclass="p">(</span><spanclass="mi">128</span><spanclass="p">))</span><spanclass="c1"># variable also support initialization using a numpy data</span>
<spanid="class-member-function-generator"></span><h3>Class member Function: Generator<aclass="headerlink"href="#class-member-function-generator"title="永久链接至标题">¶</a></h3>
<ulclass="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>
<spanid="class-member-function-discriminator"></span><h3>Class member function: Discriminator<aclass="headerlink"href="#class-member-function-discriminator"title="永久链接至标题">¶</a></h3>
<ulclass="simple">
<li>Given a noisy input z, returns a fake image.</li>
<spanid="class-member-function-build-the-model"></span><h3>Class member function: Build the model<aclass="headerlink"href="#class-member-function-build-the-model"title="永久链接至标题">¶</a></h3>
<ulclass="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>
<spanclass="c1"># step 1: generate images by generator, classify real/fake images with discriminator</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<spanclass="k">if</span><spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">y_dim</span><spanclass="p">:</span><spanclass="c1"># if conditional GAN, includes label</span>
<p>Some small confusion and problems with this design:</p>
<ulclass="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>
<spanid="main-function-for-the-demo"></span><h2>Main function for the demo:<aclass="headerlink"href="#main-function-for-the-demo"title="永久链接至标题">¶</a></h2>
<p>Generally, the user of GAN just need to the following things:</p>
<ulclass="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>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="c1"># pd for short, should be more concise.</span>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.