Back to Index

Sequential Model - TensorFlow Essentials #1


Chapters

0:0
0:7 start a new notebook
2:54 add another layer
3:44 using the softmax activation
4:27 use the softmax activation function
4:36 print out our model with the model summary
5:23 building a sequential model

Transcript

Hi, welcome to this introduction to the sequential model in TensorFlow. So we're just going to go ahead and start a new notebook here. Now what I want to do is import TensorFlow. Now, the sequential model is one of two different approaches that we can use for building our models in TensorFlow.

Those are the functional API and then the sequential model that we're going to go through here. So the sequential model is better for simple sequential stacks of layers where each layer has just one input and just one output. So if our model looks something like this, then the sequential approach is probably better.

So we access the sequential model using tf.keras.sequential so it looks like this. And then that initializes the sequential model. And now there are two methods for adding layers with this model. The first of those which I'm going to show you is using the add method. So all we do is take model, add, and then we add our layer here.

So one of the layers that we'll be using quite a lot is a densely connected neural network layer. So we just write tf.keras.layers.dense. And then we're going to add in 32 units here. And our input shape will be 10 units. So here we have 10 inputs and they are each connected to 32 units within our dense layer here.

And they will use a ReLU activation function which is a rectified linear unit. Now you don't need to know all of this right now but what I do want you to focus on is the shape that we are producing. So if we go over to here, this is the shape of the network that we're building.

So we have 10 inputs. So that was represented by the input shape. And then we have 32 neurons in the middle, so 32 units. If you look, because it's a densely connected neural network, every single one of these is connected to every single one. So all of our 10 inputs are connected to this unit here, this one, this one, this one, and so on.

And then in our output, I want to put 2 units. So we'll go ahead and do that now. So if we want to add another layer, we just call add again. So we can run that. Okay, sorry, this is input shape. And if we want to add another layer, all we do is model add again.

Now, we only need to define the input shape in our first layer with the sequential model. Otherwise, we don't need to define it because it will assume that our input shape matches the shape of the previous layer, which in this case is 32. So we don't need to do anything there.

So we just put 2, and then we add in our activation. Now, because this is a sequential model, we don't need to define the input shape. We just use our activation. Now, because this is the output layer, and we have more than one output, we will be using the softmax activation.

So in the output layer, you will usually have either sigmoid or softmax. If you just have one output, so if your output is just one value between 0 and 1, you use sigmoid. If you have multiple outputs, so we have two units here, that means our output can look something like this.

And what we will want to do is take the maximum value as the answer. So in this case, it would be index 0. So the output label would be 0. And when we're doing that, we want to use the softmax activation function. So we can execute that. And then we can print out our model with the model summary.

And here we can see, okay, we have a sequential model, and it has two layers. So this doesn't include the input layer. So our input layer is actually 10 units. And then it goes into the actual model, which is 32 units. This is our hidden layer. And then our output layer is two units at the end there.

And we see that there are a total of 418 parameters and all those trainable. So those are the number of numbers that the model will adjust in order to learn patterns within the training data. Okay, so that is our first option for building a sequential model. But we also have another way of building it.

So rather than using the add method, we can actually define our model all within the single initialization function like this. We just add a list. And then we take what we had here, just the layer, and add it in. And you just add each layer as a item within the list.

And then we just execute that. Print the model summary. And we have the exact same model. So that is everything on the sequential model. I hope you've enjoyed and I hope it's been useful. So thank you for watching and I will see you again soon. Bye. and I will see you again soon.

Bye!