<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Bin Usman on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Bin Usman on Medium]]></description>
        <link>https://medium.com/@muhammadbinusman03?source=rss-34a665ce9796------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BBfdFCTOTx4Bgot9TveH7w.jpeg</url>
            <title>Stories by Muhammad Bin Usman on Medium</title>
            <link>https://medium.com/@muhammadbinusman03?source=rss-34a665ce9796------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 01:54:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muhammadbinusman03/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[GANs Uncovered: Part I — Understanding Vanilla GANs, Training Challenges and Improved Techniques]]></title>
            <link>https://medium.com/@muhammadbinusman03/gans-uncovered-part-i-understanding-vanilla-gans-training-challenges-and-improved-techniques-385a88b37254?source=rss-34a665ce9796------2</link>
            <guid isPermaLink="false">https://medium.com/p/385a88b37254</guid>
            <category><![CDATA[gans]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Muhammad Bin Usman]]></dc:creator>
            <pubDate>Mon, 04 Mar 2024 12:04:51 GMT</pubDate>
            <atom:updated>2024-03-04T16:16:45.454Z</atom:updated>
            <content:encoded><![CDATA[<h3>GANs Uncovered: Part I — Understanding Vanilla GANs, Training Challenges and Improved Techniques</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VZ_gjzXCUUDGtkyQSXnD4Q.png" /></figure><p><em>Generative Adversarial Networks</em> are generally implicit generative models that learn the statistical properties of training data to generate new samples identical to the same distribution. The term <em>‘Adversarial’</em> indicates, at its core, GAN is a dynamic interplay between two agents, a <em>generator</em> and a <em>discriminator</em> competing against each other. The discriminator estimates the probability of a given sample belonging to either the original training distribution or the generator-learned distribution. On the other hand, the generator aims to fool the discriminator by iteratively producing samples close to real distribution and eventually capturing the training distribution. This adversarial training process enables both agents to grow stronger with the generator capable of producing increasingly realistic outputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5bR7G6llYWst0cRk" /></figure><h3>Generator</h3><p><a href="https://arxiv.org/abs/1406.2661">Ian Goodfellow et al, 2014</a> described a generator as a differentiable function G represented as a multilayer perceptron with parameters g. Given a real distribution <strong><em>p_{data}</em></strong><em> </em>, the generator implicitly learns a distribution pg through a mapping <strong><em>G(z; g)</em></strong>, where <strong><em>z</em></strong> is sampled from prior input noise <strong><em>p_z</em></strong> (uniform or gaussian). In principle, a generator can be any kind of differentiable function with parameters <strong><em>g</em></strong> that can be learned through gradient descent (backpropagation) but generally, it is merely a neural network, often a deep one. In the ancestral sampling of generator output from <strong><em>p(G(z) | z)</em></strong> , it is ensured that <strong><em>dimension(G(z)) ≥ dimension (z)</em></strong> , so we are not forced to represent only a low dimensional manifold with the output space.</p><h3>Discriminator</h3><p>The original GAN paper explained the discriminator as another differentiable function <strong><em>D</em></strong> represented as a multilayer perceptron with parameters <strong><em>θ_d</em></strong>. Given any sample, the discriminator <strong><em>D(x;d)</em></strong> outputs a probability <strong><em>D(x)</em></strong> that the input sample x belongs to the original distribution rather than the generator distribution <strong><em>p_g</em></strong>. Like a generator, a discriminator can also be any differentiable function with learnable parameters but usually, it’s a deep neural network. The discriminator guides the generator to imitate real distribution, without which the generator will produce random noise only.</p><h3>GAN Objective</h3><p>As we have seen the discriminator inspects each input and tries to make <strong><em>D(x)</em></strong> near 1 for inputs from training data and make <strong><em>D(G(z))</em></strong> near 0 for inputs from the generator output. Meanwhile, the generator indirectly tries to make <strong><em>D(G(z))</em></strong> near 1 to fool the discriminator. Thus in combination, both functions <strong><em>G , D </em></strong>aim to optimize the following objective:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*Oj3Aa-HWDdVAemM_9S-gQA.png" /></figure><p>This corresponds to a <strong>minimax</strong> <strong>game </strong>in which player <strong><em>D</em></strong> is maximizing <strong><em>Ez[log(1-D(G(z))</em></strong>] while <em>G</em> is minimizing it and <em>Ex[log D(x)]</em> has no impact on G while optimizing the functions. Hence the cost of each discriminator and generator can be explained as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*v3O4_TMquikxZuzPTQUwdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*2UFc_zDVRXFjKqQjBPHtVw.png" /></figure><p>Early in learning, with the generator producing poor outputs that are easily distinguishable from the original distribution, the discriminator can reject these fake results with high confidence, hence saturating <strong><em>Ez[log(1-D(G(z))]</em></strong>, so modifying the objective, <strong><em>G</em></strong> can maximize <strong><em>Ez[log D(G(z)]</em></strong> instead for stronger gradients at the beginning of training. Hence generator cost for a non-saturating game would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*S10wbEOoitF_APBxN3YMJw.png" /></figure><h4>Optimal Discriminator Function</h4><p>As discussed above, <strong><em>D(x) </em></strong>will tend to maximize the objective <em>V(G, D)</em> , so we are interested in finding the optimal value of <strong><em>D(x)</em></strong> that maximizes the objective or more precisely, the cost <strong><em>J_d</em></strong>. Thus we need to set:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/97/1*9HqzBtbDhgw-VmhKG9HSGA.png" /></figure><p>Then a straightforward algebra leads us to :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*qJeIYYCNAJ24Y_PNCXeguw.png" /></figure><p>It’s evident that <strong><em>D(x)</em></strong> will approach <em>1/2</em> when generator is trained to imitate <strong><em>p_data(x)</em></strong> i.e <strong><em>p_g(x)~</em></strong> <strong><em>p_data(x)</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/0*Zk12mN5h9RIHYRs3" /><figcaption><em>Image source : </em><a href="https://arxiv.org/abs/1406.2661"><em>Ian Goodfellow et al, 2014</em></a><em> .pdata (dotted black line) , pg( green line), discriminator distribution (blue-dashed line). Shows how D(x) starting from partially accurate classifier (left) converges to the optimal value pdata/(pdata + pg) upon training (right).</em></figcaption></figure><h4>Global optimum</h4><p>At sufficient training, when <strong><em>p_g(x)= p_data(x) </em></strong>and <strong><em>D(x)=1/2</em></strong> , we achieve the global optimum for the training objective <strong>V(G,D*)</strong>, plugging these values in its equation, we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/217/1*puiw9LBLejOXdRTWZQm2MA.png" /></figure><h4>Representation as JS Divergence</h4><p>The training objective is just a <a href="https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence">JS (Jensen-Shanon) divergence</a> between data distribution <strong><em>p_data</em></strong> and model distribution pg. In the compact form , it can be represented as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*vZFVZfC3KwNUWFdCpRhg-w.png" /></figure><p>JS — Divergence is non-negative always except when the contrasting distributions are both equal, it becomes 0. Thus when <strong><em>p_data=p_g</em></strong> , we will obtain the optimized generator <strong>G*</strong> which will lead to the minimized objective <strong><em>V(G*, D*) = -log 4</em></strong>.</p><h3>Training Procedure</h3><p>Once both training objectives are defined, the training procedure is a minibatch stochastic gradient descent algorithm applied alternatively and independently on the generator and discriminator, and an optimization algorithm is chosen for e.g. Adams. Essentially, for each training epoch, we carry out backpropagation for <strong>k</strong> steps on discriminator and a single step on generator. Here k is an exclusive hyperparameter to GANs (usually kept 1) for controlling discriminator convergence.</p><h4>Discriminator Step</h4><ul><li>Sample a minibatch of noise vectors from prior pz and synthesize them using a generator to create fake samples.</li><li>Sample minibatch of data from training distribution <strong><em>p_data</em></strong></li><li>Use a discriminator to classify both samplings.</li><li>Backpropagate on discriminator parameters according to :</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*sXvkblXUCuyu8QbNoQejNg.png" /></figure><h4>Generator Step</h4><ul><li>Sample a new minibatch of noise vectors from prior pz and synthesize them using generator to create fake samples.</li><li>Use discriminator to classify this sampling only.</li><li>Backpropagate on generator parameters according to either strategy (consider saturation scenarios as well) :</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*a5mERfR6eNWiLbYtsUVCAQ.png" /></figure><h3>Challenges in training GANs</h3><p>Even though GANs can generate realistic samples but training them comes with several challenges.</p><h4>Training Instability</h4><p>As discussed earlier, Nash equilibrium signals GAN convergence but in practice, gradient descent which finds a minimum value of a cost function, may not be able to converge such a non-convex game with high dimensional parameter space. <a href="https://proceedings.neurips.cc/paper_files/paper/2016/file/8a3363abe792db2d8761d6403605aeb7-Paper.pdf">Salimans, et al.</a> explain this problem with an example, let’s consider two players optimizing a function xy where one tries to minimize the function with respect to<em> x</em> and another minimizes <em>-xy</em> with respect to <em>y</em> , gradient descent instead of converging the function to Nash equilibrium when <em>x=y=0</em> , will update<em> x </em>and<em> y </em>with huge oscillations, worsening the instability with further steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8j2F5JpfGFFm1nCe" /><figcaption><em>Image source: </em><a href="https://lilianweng.github.io/posts/2017-08-20-gan/#kullbackleibler-and-jensenshannon-divergence&#39;"><em>From GAN to WGAN</em></a><em>. Simulates x minimizing xy, y minimizing -xy with a learning rate of 0.1</em></figcaption></figure><p>This doesn’t imply GANs don’t converge but rather indicates that simultaneous gradient descent does not guarantee the convergence of game algorithms like GANs.</p><h4>Mode Collapse</h4><p>When the generator fails to generalize all modes(distinct examples or classes) from the training data and rather repeatedly generates only a few modes or misses entire modes from the data even though it may be fooling the discriminator by producing realistic samples indistinguishable by discriminator, it signals the GAN has undergone mode collapse. Network architectures and choice of objective function are very crucial to avoid mode collapse, especially for a data distribution with multiple modes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*yC7KnDW58RazAtRf" /><figcaption><em>Image source: </em><a href="https://www.researchgate.net/publication/329705388_A_Probe_into_Understanding_GAN_and_VAE_models/figures?lo=1"><em>GAN and WGAN</em></a><em>. MNIST dataset has a probability distribution of 10 modes (for 10 digits). The discriminator classifies all digits correctly initially but struggles with digit ‘1’ and ‘9’. This is informed to generator as feedback, the generator collapses to the single mode and begins generating a whole distribution of ones.</em></figcaption></figure><h4>Vanishing Gradient</h4><p>Suppose the discriminator learns faster than the generator or reaches its optimal state when it can distinguish between real and synthetic samples with high confidence. In this situation, the generator is stuck with a certain loss due to negligible updates to the gradient (almost zero) and generator learning becomes super slow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZEINVfnYjhsetmFW" /><figcaption><em>Image source: </em><a href="https://arxiv.org/pdf/1701.04862.pdf"><em>Arjovsky and Bottou</em></a><em>. Shows as the discriminator gets better, the generator gradient vanishes. First, a DCGAN is trained for 1, 10 &amp; 25 epochs, then the discriminator is trained from scratch while keeping the generator fixed and measuring the gradients with the original cost function. The gradient norm decays quickly, in the best case, 5 orders of magnitude after 4000 iterations.</em></figcaption></figure><h4>Lack of Evaluation Metrics</h4><p>The selection of the right evaluation metric is important to signal GAN convergence or to compare performances across several models. GAN evaluation is generally qualitative (visual examination of samples by humans) which can be time-consuming, subjective and unable to capture distributional statistics.</p><h3>Improvements</h3><p>Improving GAN performance had been a popular research subject and various techniques have been studied so far. In order to address above mentioned challenges while training gans, <a href="https://proceedings.neurips.cc/paper_files/paper/2016/file/8a3363abe792db2d8761d6403605aeb7-Paper.pdf">Salimans et al, 2016</a> have proposed 5 major techniques to overcome these training challenges.</p><h4>Feature Matching</h4><p>Feature matching can help tackle training instability in GANs by redefining the responsibilities of both Networks. Instead of fooling the discriminator, the generator is required to match the statistics of the real data or more specifically, match the expected value of an intermediate layer of the discriminator whereas the discriminator is only needed to extract the most relevant feature vector from its intermediate layer that can best discriminate between real and synthetic data. This brings a new objective for the generator, which aims to minimize the following L2-Norm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*6sgEy3pYF7xjCg4LRrgBtQ.png" /></figure><p>Where <strong><em>f(x)</em></strong> denotes activations of a discriminator’s intermediate layer.</p><h4>Minibatch discrimination</h4><p>During mode collapse, the disriminator has no mechanism to evaluate the similarity between the repeated results by generator as it processes each example independently and hence the discriminator’s gradient starts pointing in the same direction for many inputs. To overcome this challenge, the discriminator is allowed to observe a minibatch of training samples and determine similarity of each generator result across the minibatch to increase diversity in outputs.</p><p>To avoid collapse of generator, the similarity <strong><em>o(x_i)</em></strong> between a generator output and a minibatch of data is computed by summing the closeness between each pair of minibatch samples <strong><em>(x_i , x_j)</em></strong> which is computed by the negative exponentials of the L1 distance between the resulting matrices <strong><em>M_i (BxC) </em></strong>and <strong><em>M_j</em></strong> found by the product of a transformation tensor<strong> <em>T</em></strong> <strong><em>(AxBxC)</em></strong> and activations <strong><em>f(x) </em></strong>of an intermediate layer of the discriminator for a sample <strong><em>x</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*6voOWqVhycWNUeHRltgqOQ.png" /></figure><p>This similarity of a given minibatch is concatenated with the feature vector of a discriminator’s intermediate layer and sent as input to the next layers in discriminator for a more aided classification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/0*Kgdmp99Olj4-Jtyl" /><figcaption><em>Image source: </em><a href="https://proceedings.neurips.cc/paper_files/paper/2016/file/8a3363abe792db2d8761d6403605aeb7-Paper.pdf"><em>Illustration of mininbatch discrimination</em></a></figcaption></figure><h4>Historical averaging</h4><p>To better converge the model towards equilibrium when the model parameters oscillates quickly, we can update a running average of the parameters by adding the following L2 term to the cost function of both networks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/126/1*4GbHDoecuOc2movV39oAcw.png" /></figure><p><strong><em>θ[i] </em></strong>tells the parameters at past training time<em> i</em></p><h4>One-Sided Label Smoothing</h4><p>To reduce the network’s vulnerability against adversarial examples, we can feed the discriminator more smoothened values like 0.9 and 0.1 instead of the usual 0 and 1 as classifier targets for training data samples. But we don’t smooth negative labels (for generator samples), to avoid reinforcing generator’s behaviour when producing erroneous examples.</p><h4>Virtual Batch Normalization</h4><p>Typical batch normalization is very effective for optimizing neural networks , but it creates dependency among samples of the current mininbatch. As a result each batch’s aggregated statistics may have a significant unwanted impact on the individual outputs.</p><p>Virtual Batch Normalization overcomes this by preselecting and fixing a <em>reference batch </em>early in the training and use its normalization parameters (mean and variance) for normalizing the rest of minibatches. However this can overfit the generator to this reference batch and to avoid this overfitting, the current sample is combined with the reference batch to compute different normalization parameters.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=385a88b37254" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>