Answers.Body: <p>To understand this equation first you need to understand the <a href="https://arxiv.org/pdf/1406.2661.pdf" rel="noreferrer">context in which it is first introduced</a>. We have two neural networks (i.e. <span class="math-container">$D$</span> and <span class="math-container">$G$</span>) that are playing a minimax game. This means that they have competing goals. Let's look at each one separately:</p>
  
  <h3>Generator</h3>
  
  <p>Before we start, you should note that throughout the whole paper the notion of the <a href="https://datascience.stackexchange.com/a/54349/34269">data-generating distribution</a> is used; in short the authors will refer to the samples through their underlying distributions, i.e. if a sample <span class="math-container">$a$</span> is drawn from a distribution <span class="math-container">$p_a$</span>, we'll denote this as <span class="math-container">$a \sim p_a$</span>. Another way to look at this is that <span class="math-container">$a$</span> <em>follows</em> distribution <span class="math-container">$p_a$</span>.</p>
  
  <p>The generator (<span class="math-container">$G$</span>) is a neural network that produces samples from a distribution <span class="math-container">$p_g$</span>. It is trained so that it can bring <span class="math-container">$p_g$</span> as close to <span class="math-container">$p_{data}$</span> as possible so that samples from <span class="math-container">$p_g$</span> become indistinguishable to samples from <span class="math-container">$p_{data}$</span>. The catch is that it never gets to actually see <span class="math-container">$p_{data}$</span>. Its inputs are samples <span class="math-container">$z$</span> from a noise distribution <span class="math-container">$p_z$</span>.</p>
  
  <h3>Discriminator</h3>
  
  <p>The discriminator (<span class="math-container">$D$</span>) is a simple binary classifier that tries to identify which class a sample <span class="math-container">$x$</span> belongs to. There are two possible classes, which we'll refer to as the <em>fake</em> and the <em>real</em>. Their respective distributions are <span class="math-container">$p_{data}$</span> for the <em>real</em> samples and <span class="math-container">$p_g$</span> for the <em>fake</em> ones (note that <span class="math-container">$p_g$</span> is actually the distribution of the outputs of the generator, but we'll get back to this later).</p>
  
  <p>Since it is a simple binary classification task, the discriminator is trained on a binary cross-entropy error:</p>
  
  <p><span class="math-container">$$
  J^{(D)} = H(y, \hat y) = H(y, D(x))
  $$</span></p>
  
  <p>where <span class="math-container">$H$</span> is the cross-entropy <span class="math-container">$x$</span> is sampled either from <span class="math-container">$p_{data}$</span> or from <span class="math-container">$p_g$</span> with a probability of <span class="math-container">$50\%$</span>. More formally:</p>
  
  <p><span class="math-container">$$
  x \sim
  \begin{cases}
  p_{data} \rightarrow &amp; y = 1, &amp; \text{with prob 0.5}\\
  p_g \;\;\;\,\rightarrow &amp; y = 0, &amp; \text{otherwise}
  \end{cases}
  $$</span> </p>
  
  <p>We consider <span class="math-container">$y$</span> to be <span class="math-container">$1$</span> if <span class="math-container">$x$</span> is sampled from the <em>real</em> distribution and <span class="math-container">$0$</span> if it is sampled from the <em>fake</em> one. Finally, <span class="math-container">$D(x)$</span> represents the probability with which <span class="math-container">$D$</span> thinks that <span class="math-container">$x$</span> belongs to <span class="math-container">$p_{data}$</span>. By writing the cross-entropy formula we get:</p>
  
  <p><span class="math-container">$$
  H(y, D(x)) = \mathbb{E}_y[-log \; D(x)] = \frac{1}{N} \sum_{i=1}^{N}{ \; y_i \; log(D(x_i))}
  $$</span></p>
  
  <p>where <span class="math-container">$N$</span> is the size of the dataset. Since each class has <span class="math-container">$N/2$</span> samples we can split this sum into two parts:
  <span class="math-container">$$
  = - \left[ \frac{1}{N} \sum_{i=1}^{N/2}{ \; y_i \; log(D(x_i))} +  \frac{1}{N} \sum_{i=N/2}^{N} \; (1 - y_i) \; log((1 - D(x_i))) \right]
  $$</span></p>
  
  <p>The first of the two terms represents the the samples from the <span class="math-container">$p_{data}$</span> distribution, while the second one the samples from the <span class="math-container">$p_g$</span> distribution. Since all <span class="math-container">$y_i$</span> are equally likely to occur, we can convert the sums into expectations:</p>
  
  <p><span class="math-container">$$
  = - \left[ \frac{1}{2} \; \mathbb{E}_{x \sim p_{data}}[log \; D(x)] + \frac{1}{2} \; \mathbb{E}_{x \sim p_{g}}[log \; (1 - D(x))] \right]
  $$</span></p>
  
  <p>At this point, we'll ignore <span class="math-container">$2$</span> from the equations since it's constant and thus irrelevant when optimizing this equation. Now, remember that samples that were drawn from <span class="math-container">$p_g$</span> were actually outputs from the generator (obviously this affects only the second term). If we substitute <span class="math-container">$D(x), x \sim p_g$</span> with <span class="math-container">$D(G(z)), z \sim p_z$</span> we'll get:</p>
  
  <p><span class="math-container">$$
  L_D = - \left[\; \mathbb{E}_{x \sim p_{data}}[log \; D(x)] + \; \mathbb{E}_{z \sim p_{z}}[log \; (1 - D(G(z)))] \right]
  $$</span></p>
  
  <p>This is the final form of the discriminator loss. </p>
  
  <h3>Zero-sum game setting</h3>
  
  <p>The discriminator's goal, through training, is to minimize its loss <span class="math-container">$L_D$</span>. Equivalently, we can think of it as trying to maximize the opposite of the loss:</p>
  
  <p><span class="math-container">$$
  \max_D{[-J^{(D)}]} = \max_D \left[\; \mathbb{E}_{x \sim p_{data}}[log \; D(x)] + \; \mathbb{E}_{z \sim p_{z}}[log \; (1 - D(G(z)))] \right]
  $$</span></p>
  
  <p>The generator however, wants to maximize the discriminator's uncertainty (i.e. <span class="math-container">$J^{(D)}$</span>), or equivalently minimize <span class="math-container">$-J^{(D)}$</span>. </p>
  
  <p><span class="math-container">$$
  J^{(G)} = - J^{(D)}
  $$</span></p>
  
  <p>Because the two are tied, we can summarize the whole game through a <strong>value function</strong> <span class="math-container">$V(D, G) = -J^{(D)}$</span>. At this point I like to think of it like we are seeing the whole game through the eyes of the generator. Knowing that <span class="math-container">$D$</span> tries to maximize the aforementioned quantity, the goal of <span class="math-container">$G$</span> is:</p>
  
  <p><span class="math-container">$$
  \min_G\max_D{V(D, G)} = \min_G\max_D \left[\; \mathbb{E}_{x \sim p_{data}}[log \; D(x)] + \; \mathbb{E}_{z \sim p_{z}}[log \; (1 - D(G(z)))] \right]
  $$</span></p>
  
  <h3>Disclaimer:</h3>
  
  <p>This whole endeavor (on both my part and the authors' part) was to provide a mathematical formulation to training GANs. In practice there are many tricks that are invoked to effectively train a GAN, that are not depicted in the above equations.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-24T21:15:06.007Z
Answers.LastActivityDate: 2019-06-24T21:15:06.007Z
Answers.UserDisplayName: Djib2011
Body: <p>Here is <a href="https://proceedings.neurips.cc/paper/2014/file/5ca3e9b122f61f8f06494c97b1afccf3-Paper.pdf" rel="nofollow noreferrer">the GAN objective function</a>.</p>
  <p><span class="math-container">$$\min _{G} \max _{D} V(D, G)=\mathbb{E}_{\boldsymbol{x} \sim p_{\text {data }}(\boldsymbol{x})}[\log D(\boldsymbol{x})]+\mathbb{E}_{\boldsymbol{z} \sim p_{\boldsymbol{z}}(\boldsymbol{z})}[\log (1-D(G(\boldsymbol{z})))]$$</span></p>
  <p>What is the meaning of <span class="math-container">$V(D, G)$</span>?</p>
  <p>How do we get these expectation parts?</p>
  <p>I was trying to understand it following this article: <a href="https://danieltakeshi.github.io/2017/03/05/understanding-generative-adversarial-networks/" rel="nofollow noreferrer">Understanding Generative Adversarial Networks <em>(D.Seita)</em></a>, but, after many tries, I still can't understand how he got from <span class="math-container">$\sum_{n=1}^{N} \log D(x)$</span> to <span class="math-container">$\mathbb{E}(\log(D(x))$</span>.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-10T16:12:11.723Z
Comments.Text: Your second question is related to [this](https://ai.stackexchange.com/q/23161/2444).
Comments.UserDisplayName: nbro
Tags: <machine-learning><generative-model><generative-adversarial-networks><notation>
Title: What is the meaning of $V(D,G)$ in the GAN objective function?
UserDisplayName: i_rezic
ThreadText: # What is the meaning of $V(D,G)$ in the GAN objective function?
  
  (Asked by: username_0 on 2019-04-12)
  
  Here is [the GAN objective function](https://proceedings.neurips.cc/paper/2014/file/5ca3e9b122f61f8f06494c97b1afccf3-Paper.pdf).
  
  $$\min \_{G} \max \_{D} V(D, G)=\mathbb{E}\_{\boldsymbol{x} \sim p\_{\text {data }}(\boldsymbol{x})}[\log D(\boldsymbol{x})]+\mathbb{E}\_{\boldsymbol{z} \sim p\_{\boldsymbol{z}}(\boldsymbol{z})}[\log (1-D(G(\boldsymbol{z})))]$$
  
  What is the meaning of $V(D, G)$?
  
  How do we get these expectation parts?
  
  I was trying to understand it following this article: [Understanding Generative Adversarial Networks *(D.Seita)*](https://danieltakeshi.github.io/2017/03/05/understanding-generative-adversarial-networks/), but, after many tries, I still can't understand how he got from $\sum\_{n=1}^{N} \log D(x)$ to $\mathbb{E}(\log(D(x))$.
  
  **Tags:** machine-learning, generative-model, generative-adversarial-networks, notation
  
  > **Comments:**
  > **username_1**: Your second question is related to [this](https://ai.stackexchange.com/q/23161/2444).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-06-24. Score: 10 | Accepted answer
  
  To understand this equation first you need to understand the [context in which it is first introduced](https://arxiv.org/pdf/1406.2661.pdf). We have two neural networks (i.e. $D$ and $G$) that are playing a minimax game. This means that they have competing goals. Let's look at each one separately:
  
  ### Generator
  
  Before we start, you should note that throughout the whole paper the notion of the [data-generating distribution](https://datascience.stackexchange.com/a/54349/34269) is used; in short the authors will refer to the samples through their underlying distributions, i.e. if a sample $a$ is drawn from a distribution $p\_a$, we'll denote this as $a \sim p\_a$. Another way to look at this is that $a$ *follows* distribution $p\_a$.
  
  The generator ($G$) is a neural network that produces samples from a distribution $p\_g$. It is trained so that it can bring $p\_g$ as close to $p\_{data}$ as possible so that samples from $p\_g$ become indistinguishable to samples from $p\_{data}$. The catch is that it never gets to actually see $p\_{data}$. Its inputs are samples $z$ from a noise distribution $p\_z$.
  
  ### Discriminator
  
  The discriminator ($D$) is a simple binary classifier that tries to identify which class a sample $x$ belongs to. There are two possible classes, which we'll refer to as the *fake* and the *real*. Their respective distributions are $p\_{data}$ for the *real* samples and $p\_g$ for the *fake* ones (note that $p\_g$ is actually the distribution of the outputs of the generator, but we'll get back to this later).
  
  Since it is a simple binary classification task, the discriminator is trained on a binary cross-entropy error:
  
  $$
  J^{(D)} = H(y, \hat y) = H(y, D(x))
  $$
  
  where $H$ is the cross-entropy $x$ is sampled either from $p\_{data}$ or from $p\_g$ with a probability of $50\%$. More formally:
  
  $$
  x \sim
  \begin{cases}
  p\_{data} \rightarrow & y = 1, & \text{with prob 0.5}\\
  p\_g \;\;\;\,\rightarrow & y = 0, & \text{otherwise}
  \end{cases}
  $$
  
  We consider $y$ to be $1$ if $x$ is sampled from the *real* distribution and $0$ if it is sampled from the *fake* one. Finally, $D(x)$ represents the probability with which $D$ thinks that $x$ belongs to $p\_{data}$. By writing the cross-entropy formula we get:
  
  $$
  H(y, D(x)) = \mathbb{E}\_y[-log \; D(x)] = \frac{1}{N} \sum\_{i=1}^{N}{ \; y\_i \; log(D(x\_i))}
  $$
  
  where $N$ is the size of the dataset. Since each class has $N/2$ samples we can split this sum into two parts:
  $$
  = - \left[ \frac{1}{N} \sum\_{i=1}^{N/2}{ \; y\_i \; log(D(x\_i))} + \frac{1}{N} \sum\_{i=N/2}^{N} \; (1 - y\_i) \; log((1 - D(x\_i))) \right]
  $$
  
  The first of the two terms represents the the samples from the $p\_{data}$ distribution, while the second one the samples from the $p\_g$ distribution. Since all $y\_i$ are equally likely to occur, we can convert the sums into expectations:
  
  $$
  = - \left[ \frac{1}{2} \; \mathbb{E}\_{x \sim p\_{data}}[log \; D(x)] + \frac{1}{2} \; \mathbb{E}\_{x \sim p\_{g}}[log \; (1 - D(x))] \right]
  $$
  
  At this point, we'll ignore $2$ from the equations since it's constant and thus irrelevant when optimizing this equation. Now, remember that samples that were drawn from $p\_g$ were actually outputs from the generator (obviously this affects only the second term). If we substitute $D(x), x \sim p\_g$ with $D(G(z)), z \sim p\_z$ we'll get:
  
  $$
  L\_D = - \left[\; \mathbb{E}\_{x \sim p\_{data}}[log \; D(x)] + \; \mathbb{E}\_{z \sim p\_{z}}[log \; (1 - D(G(z)))] \right]
  $$
  
  This is the final form of the discriminator loss.
  
  ### Zero-sum game setting
  
  The discriminator's goal, through training, is to minimize its loss $L\_D$. Equivalently, we can think of it as trying to maximize the opposite of the loss:
  
  $$
  \max\_D{[-J^{(D)}]} = \max\_D \left[\; \mathbb{E}\_{x \sim p\_{data}}[log \; D(x)] + \; \mathbb{E}\_{z \sim p\_{z}}[log \; (1 - D(G(z)))] \right]
  $$
  
  The generator however, wants to maximize the discriminator's uncertainty (i.e. $J^{(D)}$), or equivalently minimize $-J^{(D)}$.
  
  $$
  J^{(G)} = - J^{(D)}
  $$
  
  Because the two are tied, we can summarize the whole game through a **value function** $V(D, G) = -J^{(D)}$. At this point I like to think of it like we are seeing the whole game through the eyes of the generator. Knowing that $D$ tries to maximize the aforementioned quantity, the goal of $G$ is:
  
  $$
  \min\_G\max\_D{V(D, G)} = \min\_G\max\_D \left[\; \mathbb{E}\_{x \sim p\_{data}}[log \; D(x)] + \; \mathbb{E}\_{z \sim p\_{z}}[log \; (1 - D(G(z)))] \right]
  $$
  
  ### Disclaimer:
  
  This whole endeavor (on both my part and the authors' part) was to provide a mathematical formulation to training GANs. In practice there are many tricks that are invoked to effectively train a GAN, that are not depicted in the above equations.
  
  
  ---
  
  