Back to Index

Functional API - TensorFlow Essentials #2


Chapters

0:0
0:2 Functional Api
1:30 Input Layer
2:42 Densely Connected Neural Network Layers

Transcript

Hi, welcome to this video on the Functional API, which is the second of the two approaches for building models in TensorFlow Keras. So we're going to go ahead and start our notebook. I'm just going to import TensorFlow. So the Functional API can produce the same architectures as the sequential model, plus a few more, thanks to a high level of flexibility.

So the Functional API is ideal for when we have multiple inputs or outputs, different streams of information between layers, or anything else that is more complex than a simple linear model, where each input and output is feeding into the next one, and there's nothing fancy going on. An example of what this might look like is something like this.

So you can see at the bottom here, we have two inputs. And then in the middle here, we also have this stream of data being split into this one layer, and then also being fed forward into the next layer as well. This is something that you could not do with the sequential model.

You would have to use Functional API, which we're going to go through now. So rather than defining the model first, we instead define the input layers, the network layers, which is the same as what we did with the sequential model. And then we initialize the model with something called tf.keras.model.

Let's go ahead and define the input layer first. So here, we use something different. We don't use a dense layer. We're going to use a input layer, which is obviously specifically for inputs. Whereas before, we use input_shape to define the input shape, here we are using shape. And we're going to build the exact same model that we did before, but using the functional model instead.

So the only difference here is that we also need to define the data type going into our inputs, which in this case will be industry of 32. If we don't define this, it will default to float with 32. And then we can execute that. And then we need to set up our network layers, which is exactly the same as what we did before.

So if we go over to this image again, we can see that we have this sort of network architecture. So we have the 10 inputs, which is what we've already just defined. And then we have our 32 units in the middle here and 2 units on the output layer.

So we need to build that using densely connected neural network layers. And we do that just like we did before. The only difference is that here, we are passing the previous layer into our new layer. So our previous layer, we have placed into this variable here, input sensor. And we also want to pass it to our new layer here.

And then we are also putting this new layer or new transformation into a new layer. So we're just going to pass it into our new layer here. And we're going to pass this new transformation into another variable, which is x. I mean, you can use these names as you want.

But the inputs and the output layer, they need to have names which are not overwritten by any of your other layers, because we need to pass those layers into our model initializer in a moment, which you will see. So we will call our output layer y. And then we're going to use the softmax activation here again.

And we pass x, which is our previous layer. Now, if we had more than just these layers, we could use x in the middle and overwrite. And that wouldn't cause any issues, just as long as we don't overwrite the variables which define our output and input. And the reason that we don't overwrite those is because we need them to initialize our model, which we do like this.

So inputs, we have our input tensor. And outputs, we have y. So this is where, if we did have multiple inputs or outputs, we would be able to put them together here. So rather than having y, we could have y and y2, for example. But in our case, we are only using one.

So now we initialize the model. And if we print our model summary, we get the same output as we did with the sequential model with a few small differences. So at the top here, rather than saying sequential, it says model, which obviously, this is because we're using the sequential model or the functional API.

It will change what we have up here. And then for the functional API, we also have a specific definition for the input layer as well, which is just because we have actually explicitly defined it here rather than it being an input shape to one of the other layers. And that is everything for the functional API.

So I hope that has been useful and I will see you in the next one. Thanks for watching.