Hi and welcome back.
In this section, we'll take a look at using carrots to build a one or two encoder.
So open Notebook 29 and we'll get started.
So firstly, this lesson was taken from the official Keros blog tutorial on building also incluidas
in Keros.
And here's a nice little diagram to basically show what's happening and also include this here we have
a bottleneck layer here.
And basically what we're doing, we're just trying to encode the input in a smaller dimensionality similar
to principal components analysis and generating an output that looks a lot like the input and try to
try to encode it.
So basically, let's get started with this lesson.
So here are some definitions of what an encoder and decoder is.
Remember an encoded compresses of dung samples, the input image and to a lesser number of bits or lower
dimensionality.
And this last symptom of this is called a living in space or bottleneck.
And what the decoder does, the decoder takes this lesson number of bits or the smaller dimensional
representation of the input and then tries to reconstruct the input using only the including of that
input.
So that's basically a summary of what an auto encode is right here.
So now let's get started with building a very simple, basic auto encoder.
So firstly, let's import some packages that we'll be using.
Those are the Keros TensorFlow packages, as well as MATLAB, and NumPy and knowThis will be using the
amnesty dataset.
So we'll be including we'll be treating auto encoder on the M this handwritten digit dataset.
So the helper functions we're creating here is firstly a pre-processed function that normalizes the
supplied array and then reshapes it into the appropriate format so that we can pass the input to our
neural network.
Then we have a function that adds random noise to an image.
And then lastly, by the way, we'll be using this later and later in this section.
So don't worry about it.
If you're wondering why we're adding noise, don't worry.
I'll explain later.
And then we have a display function that takes these two areas and displays 10 random images from each
one of the supplied arrays.
So that's it.
So let's run this and we'll be importing our functions and declaring our helper functions.
So there we go.
So it's going to be run now.
Next, we have to load and pre process our endless data set.
So we just use the amnestied load function to get only to set up the training data and test it to notice.
We're not using the limo's here because we just assign into this dummy variable, but an auto encoder.
We don't need the variables at all.
We just need to learn the representation, the underlying representation of the data.
That's why we just look at the data itself.
So then we normalize and reshape the data using the preprocessor pre-processed helper function we created
above.
And then we add some noise to the data because what we'll be doing here will be training an auto encoder
that basically can remove noise from images.
So let's take a look and see how we do that.
So we just use a noise helper function to add random noise to the training data and test data, and
then we can display it here.
Let's display those sample images here.
So it's downloaded data set first.
That's what it's doing here.
And there we go.
So we have the the clean images on top and then the noisy ones below.
So now we're ready to build or to include a model.
So let's take a look at firstly, the encoder model you can see it's quite simple.
We just have to convolutional is and each one followed by a maximal.
So what you can imagine as we build this model, as the input goes through the network, it gets smaller.
In fact, yes, it's getting a lot smaller right now.
And then that's what we used to convert 2D transport layers.
These are basically upsampling layers, but they're smart upsampling layers.
And because they learn the feature representation of the image of this, their previous input here.
So we have the two clung to the upscaling layers to produce basically back the output right here, and
that's what we get.
So that's how we can create or to encode a model, which is basically the input here and X, which basically
strings along all of these sequential operations here.
And that gives us the final or to encode a model where we use Adam as the optimizer and binary cross
entropy as the lost function.
So let's run this and we can take a look at the architecture here so you can see how it gets bottlenecked
as we go down here and then suddenly it gets back bigger.
That's because of the continuity transport layers here.
So that's it.
And then we are now ready to trino quarter on quarter.
So notice labels?
No, the training data.
That's because the output of this network is a 28 by 28 dimension effective and.
No, previously it used to be at one by 10, and those were the outputs for probabilities of which classes.
That's when we were building classifiers.
Now we're building a neural net to learn the image and output the image back again.
So and that's why we have separate kind of separate encoder and decoder models.
So we're not ready to try this.
And also notice to test data is what we'll be evaluating one after to see how well it performs.
So let's train this and let's see how long it takes to train.
I actually don't remember we're training for 50 epochs with a bad set of 128.
OK, it's pretty quick, but we'll have to wait a bit still.
So let's wait it out.
OK, so we're done training now, and as you can see, we trained 50 bucks and examining the validation
laws here.
You can see 50 folks was probably a good choice.
It seemed to have.
It seemed to actually keep decreasing here, so we could have actually trained us for a bit longer.
But nevertheless, this is a fairly low validation loss here.
So let's take a look at our predictions now.
So to get to predictions here, what we're doing, we're going to get all of the predictions out by
just feeding into test data here into the quarter to include a model.
Don't predict and we get all the predictions here and then we just display some random turnaround and
predictions here.
So this is this display as the input data here and this display as the output.
So remember what we're doing here, but the output is it's going to be the input that was reduced in
dimensionality.
And then we're going to we going to blow that back up to the full representation of the full decoding
model so we can compare how well it did.
So let's take a look at that and you can see this is actually quite good, actually really good.
You can see the snow.
Remember, let's take a look at the auto included here.
We basically shrunk this data down all the way to a seven by seven by 282 representation at a time.
And now we can use that input to blow back using the decoder, and you can see it gives us almost the
exact image.
Looking at it carefully, it seems like a disk encoded the exact image itself, so that is actually
pretty -- good.
So now let's take a look at this.
Remember, we also created a noisy version of our dataset and we said we're going to use our auto encoder
to clean that up.
Well, let's take a look at how we do that.
So now we have the noisy data here as input.
However, notice what's different here, we don't use a noisy version of the training data here.
We used a clean vision because what we want to auto included to do is to clean that image data set up.
So we are taking noisy inputs and we're going to feed it into the network and then we're going to learn
how to decode a clean vision out of it.
So let's run that.
No, let's put this for 50 bucks here.
So it won't take that long, and let's wait for our results.
OK, so we're done training, that's about three minutes of training this network.
And you can firstly notice that a loss function here isn't as low as a previous one, and that's understandable
because given how noisy our input images were.
So let's go back to this again, and you can see we're training with noisy test data.
So this is a validation data set here.
And this is a target data here says, remember that.
So now let's evaluate its performance on some noisy data.
So let's see how well our auto encoder can clean up images and it's remarkably well, it gives it.
We can take these inputs that these noisy inputs and clean them up and get these almost perfect images
here.
The only one that doesn't look too great is perhaps maybe this tree, because it's a bit of a streak
right here and the five has some noise added to it.
Maybe this eight as well, but I mean, this is quite good.
So I hope you enjoy this lesson and know what we're going to do.
We're going to build an auto encoder in PyTorch, so stay tuned for that.
Thank you.