# Week3 : Programming assignment

**URL:** <https://community.deeplearning.ai/t/week3-programming-assignment/31528>\
**Category:** Build Basic Generative Adversarial Networks\
**Tags:** week-module-3\
**Created:** [August 27, 2021, 9:32am UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528 "2021-08-27T09:32:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![starboy](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/starboy/32/5499_2.png) [@starboy](https://community.deeplearning.ai/u/starboy)\
**Post date:** [August 27, 2021, 9:32am UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/1 "2021-08-27T09:32:58Z")

</div>

![1](https://global.discourse-cdn.com/dlai/original/2X/6/6cdea737afd0d918eadc7868b0b08bcb2e97276b.jpeg)

why do we have to multiply -1 and do mean of crit\_fake \_pred.

---

<div class="post-metadata">

**Author:** ![fangyiyu](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/fangyiyu/32/6547_2.png) [@fangyiyu](https://community.deeplearning.ai/u/fangyiyu)\
**Post date:** [August 27, 2021, 3:09pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/2 "2021-08-27T15:09:51Z")

</div>

Hi @starboy,

To recap, the critic is the equivalent of a discriminator, the difference is the critic tries to maximize the distance between its evaluation on a fake and its evaluation on a real, so the output is no longer between 0 and 1 but can be any real number.

For the generator, the loss is calculated by maximizing the critic’s prediction on the generator’s fake images.

The W-loss can be represented by this formula:

 ![Screen Shot 2021-08-27 at 10.57.35 AM](https://global.discourse-cdn.com/dlai/original/2X/7/779e5c37116bcd27767040357269b20a35942aa6.jpeg)  
and you are now calculating the second half of the formula which is to maximize the critic’s prediction on the generator’s fake images. Since the critic has the scores for all fake images in the batch, you will use the mean of them.

You can refer to the first minute of the video [Condition on Wasserstein](https://www.coursera.org/learn/build-basic-generative-adversarial-networks-gans/lecture/9Fiua/condition-on-wasserstein-critic) for more information.

Hope it helps!

Fangyi Yu

---

<div class="post-metadata">

**Author:** ![pedrorohde](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/pedrorohde/32/6548_2.png) [@pedrorohde](https://community.deeplearning.ai/u/pedrorohde)\
**Post date:** [August 27, 2021, 3:25pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/3 "2021-08-27T15:25:02Z")

</div>

You can think about it this way:

We want to **maximize** the critic’s scores on the generator’s fake images. Usually, what we do is we **minimize** a loss function. But, if you think about it, **maximizing** a `value` is equivalent to **minimizing** `-value` (negative that `value`).

Taking the mean, as @fangyiyu said, is just to average over all examples in the batch, so as to have a single scaled value for the loss (if we did a sum, it would have different scales for different batch sizes).

---

<div class="post-metadata">

**Author:** ![sohonjit.ghosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/sohonjit.ghosh/32/6619_2.png) [@sohonjit.ghosh](https://community.deeplearning.ai/u/sohonjit.ghosh)\
**Post date:** [August 27, 2021, 5:43pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/4 "2021-08-27T17:43:45Z")

</div>

I would just add in something to the answers given by @fangyiyu and @pedrorohde. The reason we are multiplying by -1 is because in the deep learning framework like Pytorch the optimization works as minimization of the objective function. As such we place the minus sign in front!

---

<div class="post-metadata">

**Author:** ![shreyasvedpathak](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/shreyasvedpathak/32/6592_2.png) [@shreyasvedpathak](https://community.deeplearning.ai/u/shreyasvedpathak)\
**Post date:** [August 28, 2021, 3:39am UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/5 "2021-08-28T03:39:56Z")

</div>

As mentioned by @sohonjit.ghosh, in computer science when we want to convert a maximization problem into a minimization problem (like knapsack problem), we multiply the loss/cost function with a minus sign. Your question is one of such cases.

---

<div class="post-metadata">

**Author:** ![Elemento](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/elemento/32/854_2.png) [@Elemento](https://community.deeplearning.ai/u/Elemento)\
**Post date:** [September 5, 2021, 2:24pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/6 "2021-09-05T14:24:11Z")

</div>

Hey @pedrorohde @sohonjit.ghosh,  
I have a small doubt regarding this. Just after this function, we are told to calculate the loss for the critic, and it is written that

> For the critic, the loss is calculated by maximizing the distance between the critic’s predictions on the real images and the predictions on the fake images while also adding a gradient penalty. The gradient penalty is weighed according to lambda.

In this case, as well, we need to maximize the distance, so why aren’t we minimizing the negative of the expression in this case as well? In other words, why are we using

> crit\_loss = torch.mean(crit\_fake\_pred - crit\_real\_pred + c\_lambda \* gp)

and not

> crit\_loss = -1\* torch.mean(crit\_fake\_pred - crit\_real\_pred + c\_lambda \* gp)

I have submitted by notebook, and it is showing me an out-of score. Have I done anything wrong?

---

<div class="post-metadata">

**Author:** ![pedrorohde](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/pedrorohde/32/6548_2.png) [@pedrorohde](https://community.deeplearning.ai/u/pedrorohde)\
**Post date:** [September 5, 2021, 11:46pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/7 "2021-09-05T23:46:13Z")

</div>

Hi @Elemento

Maybe the assignment’s phrasing is a little confusing because the term “distance” between the two could go either way, meaning it could be either `crit_fake_pred - crit_real_pred` or `crit_real_pred - crit_fake_pred`.

If you think about it, this is the **critic** ’s loss. We want the critic to predict fake images as being fake (zero) and real images as being real (one). That means we want `crit_fake_pred` to be very small ( **minimize** it), and `crit_real_pred` to go up ( **maximize** it).

Putting it in terms of optimization, this leads our minimization loss function to be `crit_fake_pred - crit_real_pred`, which amounts exactly to minimizing `crit_fake_pred` and maximizing `crit_real_pred`.

As for the gradient penalty (`c_lambda * gp`), it should be **minimized** : we want the gradient norm to be as close to 1 as possible, and our penalty function measures the distance from the norm to 1. Minimizing it makes the gradient norm get closer to 1.

Hope that made sense for you. Cheers

---

<div class="post-metadata">

**Author:** ![Elemento](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/elemento/32/854_2.png) [@Elemento](https://community.deeplearning.ai/u/Elemento)\
**Post date:** [September 6, 2021, 6:12am UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/8 "2021-09-06T06:12:15Z")

</div>

Thanks a lot @pedrorohde, it makes complete sense to me now!

---

<div class="post-metadata">

**Author:** ![pedrorohde](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/pedrorohde/32/6548_2.png) [@pedrorohde](https://community.deeplearning.ai/u/pedrorohde)\
**Post date:** [September 6, 2021, 10:03pm UTC](https://community.deeplearning.ai/t/week3-programming-assignment/31528/9 "2021-09-06T22:03:29Z")

</div>


