Hi there and welcome to the session.
In this session we are going to look at the Gan loss function and also how Gans have been trained.
Then finally we'll look at common Gan training problems.
So for a brief summary of what we've seen already, we'll consider that we have this two distributions
right here.
This distribution or this one, uh, and this black dotted lines represents the real data that's similar
to what we had here.
So similar to, uh, this real data right here.
And then this other distribution in green represents the fake data which is going to be generated by
our generator.
So, um, getting back here, we have this, uh, we see this here, which is our discriminator.
So right here we have the discriminator D then we have, as we've said already, the real and the fake
distributions.
Now, once we start training, we have this discriminator which sees that most most of the real data
is, uh, getting a score of one, or it classifies with a probability of one that this data is real.
And then as we approach this generated data, uh, as we get samples from our generated distribution,
we find that the discriminator now outputs a zero.
Now, as we keep on training, the generation gets better and then the generator samples now look a
bit more like the real data.
See, they come, they get closer.
This, uh, distance here becomes smaller as compared to compared to what we had before.
So, uh, we've trained and we've gotten to this point where now this discriminator here still sees
the real data to be one, but now confuses or sometimes classifies this fake data to, to be one.
So you see data around here, see this samples we consider to be ones or classified as one, though
we'll still have some samples classified as zero.
And then once we get to convergence, we have this, uh, half here.
So we have this, um, classifier now which is unable to differentiate between the real and the generated
or fake data because the distributions look very much alike.
And so that's it for the recap of the previous session.
Now we're going to dive into the Gan loss function.
So right here we have this, um, training algorithm which we could see here.
Let's expand this.
We have this training algorithm and if you notice, we have this two loss functions here, one for the
discriminator and the other for the generator.
But this could be combined into one equation right here.
Let's take this here.
And now we have our two player Minimax game with this value function V.
Now, when we talk about this Minimax game right here, it makes allusion to what goes on between the
generator and the discriminator.
So our two players in this case are our generator and the discriminator.
Now one thing you could also do with this Gan lab is you could put out your own, uh, distribution.
So let's say, for example, this distribution we apply and we start the train and the generator now,
together with the discriminator, start to play this game where at the end or at Convergence, we expect
the generator to produce outputs which are similar to that of the discriminator.
Now, coming back to this equations, you'll notice that we have mean and G and Max and Z.
So to understand this notation, you can consider that we are minimizing this expression right here
by updating the parameters of G, and then we maximize this expression by updating the parameters of
Z, which is our discriminator, where G is our generator.
And then if we try to separate these two, that's to to get for the two to start with minimizing, for
example, for the generator.
You'll find that given that in this first expression, you see this, this expression right here, let's
have that.
So in this expression, there is actually no G.
So we could make use of only this when we're trying to minimize this whole expression with respect to
parameters of G.
So that's why if you, if you, if you check in the, um, algorithm given to.
Us right here.
You find that for a loss for G, you have only that second expression.
Now for the D, Let's get back here for the d.
D is in the both sides.
Or this both this expression and this other expression.
So that's why we have the combination of the two in this, our algorithm right here.
Now, to better explain or better understand this in depth, let's consider this here.
So we've extracted for the D and for the G.
You should also note that let's get back here.
When we talk about updating the discriminator or updating the generator is basically our gradient descent
step.
Remember, if we have, for example, Tedder and or rather, if we have let's take this off.
If we have Tedder at a particular step, let's say step I or to get Tedder at I plus one step, then
we would have tedder I as a previous step or let's say tedder I minus one.
Um let's take this off so we want to get the I which is equal to the I minus one minus the learning
rate times the partial derivative of the loss with respect to our Tedder I minus one.
So this is basically what we have in here.
And this expression here where we have this, um, reversed triangle that's, uh, this right here is
actually this section of our gradient descent.
And so this means that we're finding the partial derivative of our loss.
This is the loss with respect to the parameters.
Tedder Then we have the same for the generator.
Now, let's get back here where we're going to, um, understand in depth what all these different expressions
actually mean, right here we have this real sample and this fake sample.
So these two here, we have real and fake.
And then, uh, with this real sample, we pass it into the discriminator, which we hope or wish we
will train to see that this is a one.
And then for the, the fake sample, because this is a person who doesn't exist, whereas this person
actually exists.
And we train again, uh, which we'll see later on in this course, uh, to produce this type of outputs.
So, um, let's get back here.
We have our discriminator, then we, we from here.
Let's take, let's instead shift this, um, let's move this, this way.
So let's take this right here.
Yeah.
Okay, so we have that.
And then here we have this right here.
Now this is our fake sample.
And then we have our generator.
Let's change the color for the generator.
So our generator produces this.
And then we have some random noise which we send in here because we just we just want to be able to
produce this from some random noise.
So we have this random noise right here which produces this, our G, which produces that, um, fake
sample.
And then we have our discriminator who is able to classify or say whether an input is fake or not.
Okay, so getting back here, let's take this off.
Getting back here.
When we want to train our discriminator, we're going to have an input X right here.
You see this input x?
Now this input X is this real sample right here.
So this is our x I, which is which is this here.
And this is our discriminator, which takes in this input.
Then once the discriminator takes this input is expected to output a one.
And if you get back your let's get back here you find you're told update the discriminator by ascending
its stochastic gradient and then you update the generator by descending stochastic gradient.
Now generally when we're trying to ascend or ascending or descending actually is different, like what
we've seen already, where we had theta equal theta minus learning rate, partial derivative of the
loss with respect to theta is gradient descent.
Which is actually what we're doing for the generator.
But when we talk about gradient ascent, it's actually this.
Instead, we have a plus instead of a minus.
See that?
So, um, if we have for, uh, let's plot our loss function here with respect to theta for our gradient
descent, that's classical gradient descent.
What we want to do is to minimize this loss.
But for our gradient ascent, we want to instead maximize this loss.
And then getting back here before we move on, we'll consider this plot of the log function.
So here if we have X and we have log of x, then we have a plot which looks like this.
Oops, let's have this.
There we go.
And this is one.
So when x equal one, the log is zero.
And then as X takes smaller values approaching zero, the log goes towards negative infinity.
So as we as X goes towards zero, obviously from the right in this direction, going in this direction,
then.
The log of X goes towards negative infinity.
So that's it.
Now let's get back here.
For the discriminator, as we've seen, we're having a gradient ascent.
So we're trying to maximize this.
Remember, from our minimax loss function, we we try to maximize our discriminator.
So when we when the discriminator takes in a real sample, um, it, it gives us an output of one and
with an output of one, the log of one, as we'll see here, is going to give us zero.
So the log of one is zero.
So this will give us a zero.
Now, you should note that all the outputs of our discriminator range between 0 and 1, so our discriminator
is our usual classifier.
So it's going to output values between 0 and 1.
And so getting a value of zero is the highest you could get as an output after passing through the log.
So let me explain.
We have this discriminator which outputs a value between 0 and 1 or let's plot it out.
Let's replot it again here.
So the discriminator outputs values between 0 and 1.
See that?
So this is what the discriminator outputs.
And so when, when you when you have the log of this values between 0 and 1, then the maximum value
for the log will be a zero.
You see that the maximum value for all these values between 0 and 1 will be a zero.
So when you have a zero, it means you've maximized this and that falls in line with what you expect
because we want to maximize this, um, expression we have right here.
Okay?
So we get back here for the reals we want to output a one and the log of one will give us the maximum
possible value, which is in this case is zero.
Now, for this year we have a Z.
Remember this X, Is this real image here.
The Z here is our random noise, which we have seen already here.
So when this random noise passes through our generator, we output a fake sample.
See that the generator takes in the random noise and then the generator.
Let's have this here.
This generator outputs this fake sample.
And once he outputs this, our fake sample, we now take this fake sample and pass it into our discriminator.
And we expect our discriminator to produce instead of a one this time around.
A zero.
See that?
And in our case, the log, the log of one minus a zero is a log of one and the log of one is zero.
That's the highest possible value we could get when we're dealing with logs in the range 0 to 1.
So we're maximizing this.
You see that?
Now, if that's understood, we could move on to the generator.
For the generator, We want to instead minimize this expression.
So since we were trying to minimize this expression, we would expect the output from this to be negative
infinity as a lowest possible value.
But let's get let's put in these values and see how we obtain this negative infinity.
Now we have Z, which is our random noise.
When the random noise passes through G, it outputs this fake sample again.
And now this time around, we expect the.
To consider this, for example, this time around to be like a real sample.
So what we're saying here is at one instance, we want our discriminator to see this as real.
Another instance, we want the discriminator to see this as fake.
And depending on the instance we're going to get or we're going to update our parameters of the corresponding
network.
So in the case where we want to see this as a fake, we want to update the parameters of the discriminator
such that it sees this as fake.
And then the case where we want this to be seen as a real by the discriminator, one will be updating
the parameters of the generator.
So in fact, um, what's going to be happening here is let's change this color when training the discriminator,
we're going to freeze the generator.
So we're not going to update these parameters.
We're going to update the parameters of the discriminator.
Now, when training, when training.
Let's get back when training our generator, we're going to freeze this here.
Oops when training the generator, we're going to freeze this and then update its parameters so that
it's able to fool the discriminator to think that this is a real sample.
And when it thinks it's a real sample, it's going to output a one.
Now log of one minus one is log of zero and log of zero is negative infinity.
See that?
And this is the minimum possible value.
So we're minimizing this expression.
So getting back here, we have for a number of training iterations that's basically for the number of
epochs.
Uh, we're going to do K steps.
We're going to update the discriminator via K steps.
See, for K steps do this.
We take we sample a mini batch of noise.
We get the noise, uh, mini batch of real samples.
Then from here we obtain, uh, we get the generator outputs and then we obtain the output loss which
we use to update the discriminator parameters.
And then for this k steps, I think in here, let's say we use K equals one, the number of steps we
apply to use K equal one.
So you could modify this, although in practice k equals one is fine.
So, um, getting back here after going through k steps for this, we're going to update now the generator.
So sample mini batch of M noise again.
So just like this, then we obtain this output from the generator.
This time around, we expect it to fool the discriminator.
So we're going to update the generator by descending this stochastic gradient such that it fools the
discriminator.
And that's it for this section.
In the next section, we are going to get into some practice and see how to get this kind of results
we had here.
So this are the real samples and then what we will obtain will be something like this.
See, we're able to generate this fake outputs or these people who do not actually exist and they look
pretty realistic.
Then one point we have to note before we move on is that the type of neural network used in this original
Gan paper is a classic artificial neural network, and this way the kinds of outputs they got.
But in the practice, we're going to be making use of the Gan.
So instead of the simple gan, we'll make use of the DC gan.
The DC actually stands for Deep Convolutional.
Um, and that's it.
DC Deep Convolutional So this again means deep convolutional generative adversarial neural networks.
Okay, so here we're going to be looking at this, um, neural networks which are convolution based.
And so with that, see you in the next section.