In this session, we shall be looking at modeling and how to build simple machine learning models with
TensorFlow.
Now, in our specific case, our model turns out to be this um function y equals mx plus c.
So here's our model.
This is actually what's um in this model block we have here.
And what we want to do is put in or pass in this data into the model such that the model learns the
right values for M and C, which best represent our data.
So let's suppose that we have, um we randomly pick m to be zero.
Let's, let's let's randomly initialize m to be zero.
And then we'll also randomly initialize c to be zero.
In that case uh, if if here we have zero then we shall take off this line.
Let's take off this line.
Um, and now we will have a new line which will instead be something like this.
Well, actually, this is zero.
This is the line y equals zero because m is zero, C is zero, so y equals zero.
So what we'll be saying is um, this line should represent our data.
But looking clearly at our data, this line actually doesn't, um, represent our data properly.
Now let's change this again.
Let's say m is um for example one.
So let's get back and set M to be one.
So here we have M now which is one.
If m is one then y is equal one times x plus zero.
So y equal x.
The line y equals x actually looks something like this would have a line like this one, which now does
a much better job as compared to this other line when it comes to modeling the data we have at hand.
Now, this doesn't mean this is the best possibility as we could have, um, other possibilities.
We could have a line like this.
We could even have lines like this.
Um, and so on and so forth.
So the the idea here is that we want to get those values of M and C, which best represent our data.
Now it turns out that the M and C we're talking about here are what we'll call the weights and the biases
of the model.
So the in this case our m is the weight.
And then C is the model's bias.
And so if you get that a model has for example 7 billion parameters, then it means that we have seven
billions of this um kinds of weights, um, and biases which are being tuned such that the model represents
the data properly.
That said, let's get back to the code and create a simple model.
So here we have our model TensorFlow.
Um, Keras um sequential sequential.
And then we have this list.
So the first thing we'll put in this list is the normalizer.
We have our normalizer because or whatever value of X or whatever inputs we're passing in we want them
to be normalized.
First.
Remember the normalizer was a layer.
So you could remember you could check this.
From here we import a normalization from our layers.
And then we um adapted our normalizer to our data.
Remember we had done this here and then we had adapted our normalizer to our inputs.
And so when we get back here, you see we have this normalizer which is already adapted to our inputs.
And then now from here we have a dense layer.
It turns out that this dense layer is simply the y equal, or it's simply the m x mx plus c, which
we spoke of.
So you don't need to write mx plus C or some complicated math formula.
All you need to do is specify you just have this dense and that's fine.
Now dense is a layer.
So here you also include dense dense on that.
And that should be fine.
So let's get back.
We have our sequential API.
This is sequential API takes a normalizer and dense.
And then we could um run model summary.
So we check out our model summary.
You see we have our inputs.
We have our dense layer.
We have some parameter numbers a number of parameters.
Here we have total parameters.
We have non trainable parameters.
And then the trainable parameters.
We also have um the amount of memory space this occupies.
So this is just in bytes.
Now it should be noted that um using the sequential API is not the only way in which you could create,
um, models or machine learning models.
With TensorFlow you have the sequential sequential API.
You also have the functional API.
And then you will have um, you could.
Go through.
Model.
Subclassing.
So subclassing.
Now getting back to documentation, you could head over to TensorFlow and then Keras.
Then you click here on sequential.
And basically what this um sequential API takes in is some layers.
That's it and its name.
So you could get back here and then give this a name.
Let's call this um yeah.
As I said we're taking the layers and the name so we could call this our first sequential API.
Run that.
And you notice how this this name here changes.
So let's get back to documentation.
We have the layers text the layers and name.
And then you'll also notice that your the way is defined is a bit different from what we just saw.
So this is another way in which we could um define or or build up a sequential, uh, model based on
the sequential API.
So let's create a new code cell down.
And then here we have our model, which is a sequential uh, based model.
Then we have our normalizer.
So our normalizer let's take this off.
Then we have the dense layer.
And then number of outputs of the dense layer is one.
Actually the reason why we have this number of outputs to be one is simple.
You remember we have um this inputs and then we have a single output.
So here we have eight inputs.
Let's say one.
Well we have one up to eight inputs.
So we go from 1 to 8 inputs.
But the output or the number of outputs here is just one.
So because um, it's just one, when you're creating the dense layer you just need to specify the number
of outputs your model is expected to produce.
And so getting back here, we just um, after adding all this up we will do model summary and we get
that model summary.
So that's fine.
You see we have the exact same model using the add method.
So instead of putting all these layers in this list you could make use of the add method.
So if you're wondering why we chose the sequential API instead of maybe the functional API or the model
subclassing the answer is simple.
We have a model which is made from stacking up different, um, layers.
So here we could consider this to be our normalized layer.
Let's let's enlarge.
This takes up more space.
So our model is made of stacking um up our normalizer layer.
Let's copy this and paste this out I'll stick back that.
So we have our normalized layer.
And then we also have our dense layer.
So because our model is made of um or is composed of these two layers stacked, we could uh make use
of the sequential API as this API suits these kinds of model configuration.
So if we have let's say n layers, let's reduce this now.
So we could have so many layers actually stacked so far as they're stacked um sequentially.
Then you could make use of the sequential API for more complex configurations.
You may want to, uh, work with a functional or model subclass, but we're going to look at those in
some subsequent sections.
So we have this up to let's say n layers.
See we have all those different layers which could be stacked.
And now we could make use of our sequential API.
Now if you look at this, uh, model summary, you'll notice that we have for the dense layer nine parameters.
The reason why we have this nine parameters is simple.
We have an input here which has eight.
We are with eight different features.
Our input was 1000 by eight.
And so here we have eight features.
Now with this eight features getting into our model we have uh this actually X we'll look we'll look
at that as x.
We're going to ignore the batch dimension here.
We ignore this dimension.
We focus more on on on this one uh where we have eight features.
And then now when we, uh, taking this x, you would find that we, we would have a weight for each
and every x here.
Remember this is x.
This x we have here could be rewritten as X1X2 up to x eight.
You see that you could put this in a in a vector where we have x one up to x eight.
And so when we have this this input we could simply do M1X1 plus M2X2.
This is a.
This is the x1.
This is x2 which is from our inputs.
So um m2 x2 then um, right up to um
M8X8M8X8.
So all this here represents our m x.
And then the plus c.
Here is the bias.
So we'll have plus c.
And now um for our output we have our y where y is simply equal um M1X1 plus up to M8X8 plus c.
So one thing you could notice already is the fact that we have, um, one, two, up to eight weights
plus this bias giving us nine parameters.
And that's why when you got back here, you see, we have uh, when you get back here, we have nine
parameters for the normalization layer since it's already been adapted or with the data set, you have
known trainable parameters.
So this represents a non trainable parameters as we don't need to update this as we train because the
those parameters are that's the mean and standard.
And the standard deviation or the variance have already been adapted um on the data set.
That's why they are non trainable.
While uh these are trainable meaning that we want to update them as we um train our data.
Another nice utility function which we could use is the TensorFlow plot model.
So we could do TensorFlow.
TensorFlow.
Um Keras plot model or actually Keras utils um plot model.
We specify the model.
Then once we specify the model we also specify the output shape.
So here we have two file um my first model dot png.
And then we set the show shapes to true.
So we have to file equals that.
Take that off and there we go.
So let's run that again and see what we get.
See we have this output now which is this um nice looking figure which is the summary of our model.
We could now we have this image right here which we could download.
So that's it.
We have seen how to, um, create these kinds of summaries.
Uh, we understand what each and every, um, statement we have here means.
And then we've seen how to make this beautiful plots.
Now, in the case where we didn't have a normalizer which already understands the data, given that
it has been adapted on it, we could, uh, we need to specify our input layer.
So here we have the input layer and then we'll specify its shape.
So here we give it eight.
Uh notice how we ignore the batch dimension.
See we ignoring this because this could take any value as we could have.
Um, or we could work with data, um, or in batches of eight.
We could work in data of or way more larger batches and so on and so forth.
And so because this actually varies or changes depending on whatever situation we're having to work
with, we focus on the other dimensions.
In this case we have eight.
So there we have our input layer.
Let's go ahead and import that um input layer.
Run that again.
And then let's run this and then see what we get.
So we have here input layer.
We put that comma.
We separate the different layers because that's a list.
Uh we get an error unrecognised keyword argument shape.
Well this actually input shape.
So let's change this to input shape.
And that's fine.
So you see that?
That works fine.
Now let's change this to nine.
Run that again.
And you see we have an error because the normalization layer expects our input shape to be eight.
Run that again and we see our plot.
Now notice how this plot has changed.
Now we have eight um instead of known as before.
So let's take let's take off the input layer run that.
You see before we had known known, but now we have known shape when we, when we, when we add this
input layer.
So um, this um utils function understands that now we have inputs which are of shape known by eight
or batch by eight.
So that's it for the section on modeling.
We shall now move on to our next section which is that of error measurement.