Hello, everyone, and welcome to this new section in which we'll look at different ways of creating
models other than the sequential API which we've seen so far in the section.
We'll look at the functional API, we'll look at building callable models, we'll look at building model
of your subclasses, and we'll also look at building our own custom layers.
Previously in this class we said that there are three ways in which models are built in TensorFlow,
that is the sequential API using the functional API.
And then finally model subclasses.
As of this point, we have been using the sequential API.
As you could see right here.
Now you may ask yourself why do we need to use a different method in creating TensorFlow models when
so far we've achieved close to 99% train accuracy and around 95% accuracy.
Now, as you may have noticed so far, all the models we've been building have taken up this kind of
structure where we have an input.
We have the first layer, the next layer, which has been stacked in this sequential manner right up
to this very last layer here.
And then we have the output.
So the question we could ask ourselves is what if we have a model which takes in, say, two inputs
and has three outputs?
These kinds of models are very popular in deep learning and we shall look at them subsequently.
But before getting there, you could just imagine a problem where instead of classifying whether we
have a non parasitic or a parasitic cell, we want to.
Know the exact position of that parasitic cell or in general that cell in the image, you would find
that you would have one output which classifies whether it's a parasitic or not.
So we have this first output, parasitic or uninfected, and then this other output, which gives us
the position of the cell or the exact position of the cell in the image.
So here we see already how we could get to outputs from this.
Let's take this third output out.
So here we see we could have let's let's even take this one.
So here we have this one output to output model.
And with a sequential API, we can't really do this.
So that's why working with a functional API is very important.
The next point is we'll be able to create more complex models with a functional API.
So there is this model known as the rest net, which is very popular in deep learning computer or deep
learning for computer vision.
Now a rest not like structure will look like this.
We have this this layer of outputs I've been passing to this next layer and then we have.
The outputs of this, which are going to be concatenated with these outputs.
And then after this concatenation, we are going to pass this to the next layer right here.
So if we want to add this layer, we could have a layer here and have that.
So as we're saying, we take this output and then concatenate it with this next output before passing
to this next layer.
And so these kinds of structures or those kinds of models could not be built with the sequential API
and hence the need for the functional API.
And then the last reason why we are going to be using the functional API is the fact that we could use
shared layers with shared layers.
We could have a layer or a particular layer in our model, which has already a predefined way of encoding
information.
So when we pass information, let's say we have this input, let's say input one when we pass this input
one, this layer right here or this encoder produces an output which is going to be different from when
we pass in another input I to.
But the way it produces outputs, it's in a very thoughtful manner.
So we could have either one eye to eye three, which all share this layer.
And then we have all the layers of the model which follow on that set.
We'll look at how to create the functional API.
So here we have the sequential and then just below we are going to create this functional API.
Before starting with the creation, we are going to impart some classes.
So start by importing the input class right here, which is a layer.
We impart input and then we have from TensorFlow cross layer or rather models.
We're going to import model.
So we import the model right here and we impart the input.
We run this.
That should be fine.
We now have this func input since we use the functional API.
Just a way of calling that we have the input and then we have input which we just called and this takes
in the shape.
So here we are going to copy this exact shape we use in the sequential API.
We have the shape right here, There we go, we copy that shape and then we reuse it.
You're creating this input layer.
So here we have that and then we have your shape.
So we have the shape.
This point, you could start stacking up all these different layers we had stacked up in the sequential
or with this when we're using the sequential API we started with this year, this comes to DX right
up to this dance layer.
So there we go.
We're going to make use of this, so we copy that and then we are going to paste this out right here.
Now, first, since first we have an output.
So first, since we have this layer that we have this convert to DX, which we've defined, and then
we pass in the output from this input layer.
So here we have this func input, we copy that and then we pass this into this, into this conf layer
right here.
Now once we pass this into this conf layer, we have an output and that output is this x and then you
should get that right.
We pass this X into this back norm layer.
So here we have X as you could see, and then we have an output of X, there we go from here we pass
in the X into the max, pull max pull to the layer.
So we have this, we cut that and then we have this X right here.
So we'll just repeat this same process right up to the end.
And there we go.
You see that we haven't done much changes or as compared with the sequential API.
So that's it.
We pass in this input right here.
We have X, we pass it in, we have this, we pass in and right up to this end.
Now, once we get to the end, we are now going to create the net model from this.
So we have learned model, equal model which will imported and then we have the func input.
Now let's say we have func output, so we pass this last and then our last output is from output.
So we have the input and then we have the output.
So there we go.
We can now give it a name, we have name new net model.
If you look up we had Mr. Right within this right here.
So this would be the input image here.
We have our input image and then we've created our model, learn that model.
And then from here you could simply do net model dot summary.
Now you will notice that we should have exactly the same summary as we had with the sequential API.
So let's run that and see what we get.
Yeah, we have how many parameters.
We have
4,668,297 parameters.
There we go.
We see we have exactly the same number of parameters, the same number of trainable and non trainable
parameters.
So basically what we've done here is we've we created this model we created with a sequential API.
Now we've gotten this.
We'll see that we have to change absolutely nothing from our code.
So here we are just going to compile our model without changing anything.
We have the same net model.
Now.
We could also change this let's say net func So you see clearly that we are actually using this functional
model right here.
So we have this func.
There we go.
Funk.
And that's it.
We could run that, and then we recompile right here.
So we are not changing any parts of this.
We recompile that and then we train the model.
We are getting this error because of the way we named this model right here.
So let's have this learn that model.
That's fine.
We recompile and then we run.
So that's it.
We're training our model, and here is what we get is results.
Now, coming back to our model, we'll see that we have this feature extraction unit right here.
So this Conv layers are responsible for extracting useful features from the images, and then this last
layers are responsible for correctly classifying whether the image is parasitic or not.
That said, we could build a model known as feature extractor.
And so your would add this.
We have our model feature extractor which is going to be like similar in construction as what we've
done so far.
So we just have that copied and then we have this.
But the difference is we are not going to include this other this final layers right here.
We'll only end up this point and then we'll have this output this year.
So this is our functional.
Yeah, we'll call this extract all.
Let's just say we have this output, so we have this output and there we go.
So here we have our functional input and then we have this output and then here is the feature extractor.
So we have our feature extractor model right here.
We could do this feature extractor and then we summarize this.
So let's run this and see what we get.
So that's it.
We have our input and then we have this output right here.
At this point, instead of writing all this we wrote here, we're just going to call oh, let's, let's
take from this point.
So we have our feature.
Let's look at the name we gave it.
We gave it the name feature extractor model.
So here we have feature extractor model.
So that's our fixture extractor model.
So we take all this off and then in here we pass in our input.
So notice how we are making this model load like a function.
So TensorFlow models are callable just like the layers.
And as you could see here, this feature extractor model could be seen as a layer just like the dense
layer, the batch nom layer and all other layers.
So we've gotten this X from this input which has been passed in our model.
And then from here you see we pass this X into this flatten and we have the rest.
So that's it.
Let's now rerun this again.
So you could see what we get is output.
And as you could see, we get exactly what we expected.
We have the same number of parameters and there is this difference here where we have this feature extractor.
So unlike before where we had the components like the conf 2d batch norm max pulling and the same like
let's go up here.
There's actually a feature extractor.
So unlike before where we had this and then this now has been replaced with this feature extractor right
here that said we've just built this model using the functional API, and then subsequent sections will
build even more complex models using this functional API models where we're going to use shared layers,
we're going to have multiple inputs, multiple outputs and models.
We are going to have even more complicated model configurations.
It's important to note that you could mix up the functional API model creation style with that of the
sequential API.
So you're instead of creating this, So instead of having this our feature extractor created like this,
we are going to create it using the sequential API.
Let's add that and then we copy out this from your copy of this full model with a sequential API with
PCs out and we take all of the feature extraction part here.
We see we take this off and then we'll have only with this feature extraction part.
Now let's call this feature.
Her extractor.
So feature extractor, sequential model.
There we go.
We pay this out right here and we find so we have our feature extractor model.
We run that.
That's okay.
Let's take this off and then we'll just make sure we put exactly the same here.
So there we go.
We piss it out and we, we run this.
We see we should be able to get exactly the same output.
See, we have exactly the same output.
And you're instead of our feature extractor model we have here sequential layer.
So that's it.
This shows us that we could mix up these different ways of creating models.
From this point, we'll look at the models of class.
So right here we have our model subclass.
There we go.
It's important to note that models of class and permits us to create recursively composable layers and
models.
Now, what does that mean?
This means I could create a layer where its attributes are auto layers, and this layer tracks the weights
and biases of the sub layers.
Before taking an example, let's make this in part.
So we're going to import layer from layers.
We have TensorFlow that Keras layers.
We're going to import layer, we run that and then we move on to create our model using the model subclass.