In this video, we're actually going to take a look at the generative adverse aerial network training procedures. So diving right in. The first step will be as usual with our neural networks to start with a randomly initialized waits for our generator and discriminator networks. Now to start our generator process, which is meant to find some distribution that does well in representing the actual values in our training set, be those images or whatever it may be. Will be to pass through just a random noise vector. So our input is going to be a random noise vector. And the goal is that our generator network will be able to take this random noise and come up with the appropriate weights to generate the complex distributions of our images of whatever our training set is. And then that generated image should then be the same size as the actual training set images as they will later be fed through our discriminator network. The next step, step 3, will be for the discriminator to predict the probability that the generated image is a real image, or one that our network has just graded. So once again, that is going to pass through our discriminator network, our produce image, and the goal is to discriminate between actual training images and those are generated network is producing. And the output will be the probability that we're working with an actual image and not a generated one. Step 4, we're then going to want to compute the losses, both assuming that the image we just generated is fake. And a loss function, assuming that the image generated was real and we'll get into this in just a second. We're going to call the loss function, assuming it was fake, which it is, of course. Just a reminder we are working here in the realm of that generated image. So we're going to call the loss function, assuming it was fake, L0. As in how far away was our discriminator from predicting there's a zero probability that this generated image is not real. And then we're going to call the loss function, assuming it was real, which again, it's not going to call that loss function L1. Representing how far discriminator was from predicting this was 100% a real image. And then these two combined will produce our total loss function, which we can later leverage to train both our generator and discriminator networks. Now here in step 5, we'll make a little bit clearer why we have these two different loss functions. So our discriminator network is going to only care about correctly predicting that this image is not a real image. So I want to use that loss function L0, we're talking about the discriminator network. So how far off was the output from predicting 0 is what we're trying to get here. So we back propagate in relation to the loss of how far off we were to saying that this is not a real image. And then we update the weights of only are discriminator network accordingly. Now, where does our L1 penalty come into play? Our L1 penalty, again, the L1 being assuming that our image produced was actually real. That's going to be ignored by our discriminator network, coz our discriminator network doesn't want to optimize, assuming that a fake image is real. The goal of computing L1 is to ultimately tell us if we're doing a good job of producing an image that seems realistic. And that's our output from our discriminator is too far from 1. So we compute are gradients in regards to L1 and back propagate through without training the discriminator and pass that out ultimately to our generator. So rather than using that gradient to train our discriminator network, we use it to actually update our weights for our generator network. So we continue to back propagate using that L1 gradient to update the weights of our generator. Now what's missing in this training procedure that we just walked through? The goal of our discriminator network is to learn to classify fake images as fake, real images as real. And for it to actually improve its ability to distinguish between fake an real images. We're also going to have to give it images. From the actual training set. So our next step is for the real images within our training set to be passed through and we want to calculate the probability that the image passed through from our actual training set is real. So we passed that real image XR before we were at working with XG, the X generated. And we want to know the probability that this is actually a real image, and that's going to be the goal of the discriminator now. We then compute a new L1 loss for our real image. So again, when trying to predict a real, we're actually going to want our discriminator to output one. So we're actually going to use this also to update our discriminator function. And the L1 in regards to XR is going to be how far off our probability was, what should be as close to 1 as possible from that value of 1. And we can use that L1 loss to train and update our weights appropriately within that discriminator network. And we repeat this procedure with new random noise from the generator each time. And continue until images from the generator begin to look real. Now we must note that values of losses from discriminator and generator may still be fluctuating when the generator is producing realistic images. So we can't use these alone to determine when to stop training. And there are ways of quantifying image quality within our generative process, such as the inception score, to help us find where we should actually stopped training. That's a bit beyond the scope of the lesson, but I encourage you looking into it if you want to delve a bit deeper into working with GANs. Now, I want to pull out here a quote from the original paper on GANs. The generative model can be thought of as analogous to a team of counterfeiters, trying to produce fake currency and use it without detection. While the discriminative model is analogous to the police trying to detect counterfeit currency. Competition in this game that drives both of these teams to improve their methods until the counterfeits are indistinguishable from the genuine articles. So we think if the police are too lax, then the counterfeiters won't have any incentive to create better bills. If the police are too good, then perhaps they won't have any motivation to build those bills in the first place. So there's going to be this balance that we also want to take into account. Now, training GANs effectively is highly dependent on both the generator and discriminator learning at the same rate. So the ability of two networks to learn is affected by network architectures, different learning rates, different loss functions, different optimization techniques. And in general, GANs are going to be more sensitive than traditional neural networks to choices on any of these dimensions. So if you think about what I just discussed in relation to the dangers of overfitting. In regards to that balance when we talked about the counterfeiters and the police. We know that in regards to overfitting, we now have that compounded by the fact that we are working with adversarial networks with competing goals. So if the discriminator is too good, for example, wouldn't be able to train at all as fake examples will have too high of an error with the discriminator. And if the discriminator is too lenient, on the other hand, there would not be much learned in respect to creating realistic images. So what you should be doing to train GANs yourself. Since GANs are fairly new, it'll be that much more important to actually take a deeper dive into the research and original papers on the subject, such as Ian Goodfellow's original paper. And some examples of where GANs are used currently are deepfakes, which you may have seen in some spoof videos where you can take an existing photo or video and replace it with someone else's likeness. Age interpolation or making people in images look older or younger than they actually are, and even taking text and producing images related to that text. And here we have some links and some examples of GANs being used for generating fake images of people and learning lip sync from audio. Now, just to recap. In this section, we discussed an overview of Generative Adversarial Networks or GANs and the idea of how we can leverage adversarial examples to generate more realistic samples. We discussed the training procedure for GANs and how we use specific loss functions to update the weights. Of our discriminator, and generator networks, to optimize our model. Then finally we discuss some key hyperparameters, that govern how GANs train. And how we need to be careful, when fine tuning them due to the problem of stability, with working with both generator and discriminators within the same network. Now that closeout our video here on GANs, in the next video we're going to discuss some other topics that you should be aware of, when working with deep learning models in general.