Keras Model Wars: Functional vs Sequential [EN]
Hello,
In this article, I will try to explain the 2 types of models that we encounter while developing in Keras.
Happy readings🙂
WHAT IS MODEL?
Let’s take the first article of Başarabilirsin: Makine Öğrenmesi’ne Giriş serie as example:
Imagine a high school student who has the university exam two days later. This student prepares herself/himself for the exam and solves practice exams. When student takes the university exam, encounters similar questions, not the same ones in practice exams. The student studied by solving the practice exams and was able to answer the questions in the university exam through the question model in the practice exams.
Now let’s turn this scenario into the Machine Learning side.
We create a model (student) while solving Machine Learning problems. This model is the model which our Machine Learning algorithm works. We first feed this model with information, train it (student studies for the exam), and then we want this trained model to predict a new result with the information we have not given before. (Student taking an exam for which he has not seen the questions before.)
In this case, we can see our model as a structure that learns the information and makes predictions with certain data based on the information it has learned.
When we implement Machine Learning in Keras, we can create our model in two ways: Sequential and Functional.
I suggest you to read this LSTM article 🙂
Below you can see a simple model diagram:
To put it briefly:
- Input Layer:It is the input layer of our model. In this layer, we give our model the information that we want the prediction to be made on, namely our features.
- Hidden Layers: All the other layers between the input and output layers of our model are hidden layers.
- Output Layer: It is the output layer of our model. Our label value comes out of this layer.
No problem so far, right? Beautiful.
SEQUENTIAL MODEL
In this model variant, the implementation of the model is simple. For a model to be simple it should:
- Not having multiple input sources
- Not having multiple output targets
- Not reusing layers (reusability)
Accordingly, we can say that a model is simple if its input comes from one place, its output goes to one place, and the layers are not used in other models.
In the Sequential Model, we first create our model, then add our layers to our model. The output of one layer becomes the input of the next layer. Let’s get our hands dirty🙂
One of the things you should pay attention to here is that we add layers with the add function of our model. When you examine the layers carefully, we see that the first layer takes the input_dim parameter. From here, we can understand: Sequential models have only one input layer, and this layer is always the first layer.
You can see more information about Dense through here, and more information about Activation through here.
FUNCTIONAL MODEL
This type of model offers us greater flexibility in implementation. It allows the existence of multiple input and output sources that share the same layers. In this implementation, we first create the layers, connect them, and then create our model.
RESULT
Although the Sequential Model provides us simplicity, it does not provide flexibility on a layer basis. We build our model and add our layers in order. There is only one input layer.
The Functional Model is more complex but correspondingly more flexible. First we create our layers, if necessary, we process them with each other, then we create our model using one or more input and output layers.
CLOSURE
In this article, I tried to explain Sequential and Functional models. I hope it was an explanatory post. Congratulations and thanks if you made it this far 🙂 I wish you healthy, happy and productive times, good luck in your run in the Machine Learning marathon 🙂