Keras Models – Types and Examples

Free Keras course with real-time projects Start Now!!

A model is the basic data structure of Keras. Keras models define how to organize layers. In this article, we will discuss Keras Models and its two types with examples. We will also learn about Model subclassing through which we can create our own fully-customizable models.

Keras Models

Types of Keras Models

Models in keras are available in two types:

  • Keras Sequential Model
  • Keras Functional API

1. Sequential Model in Keras

It allows us to create models layer by layer in sequential order. But it does not allow us to create models that have multiple inputs or outputs. It is best for simple stack of layers which have 1 input tensor and 1 output tensor.

This model is not suited when any of the layer in the stack has multiple inputs or outputs. Even if we want non-linear topology, it is not suited.

Here is an example for Sequential model:

from keras.models import Sequential
from keras.layers import Dense

model=Sequential()
model.add(Dense(64,input_shape=8,))
mode.add(Dense(32))

2. Functional API in Keras

It provides more flexibility to define a model and add layers in keras. Functional API allows us to create models that have multiple input or output. It also allows us to share these layers. In other words. we can make graphs of layers using Keras functional API.

As functional API is a data structure, it is easy to save it as a single file that helps in recreating the exact model without having the original code. Also its easy to model the graph here and access its nodes as well.

Below is the Example for Functional API:

from keras.models import Model
from keras.layers import Input, Dense

input=Input(shape=(32,))
layer=Dense(32)(input)
model=Model(inputs=input,outputs=layer)

//To create model with multiple inputs and outputs:

model=Model(inputs=[input1,input2],outputs=[layer1,layer2,layer3])

Model Subclassing in Keras

Sequential model does not allow you much flexibility to create your models. Functional API also only has a little of customization available for you. But you may create your own fully-customizable models in Keras. This is done by subclassing the Model class and implementing a call method.

For example:

from keras.layers import Dense,Dropout,BatchNormalization

Class Subclass(keras.Model):

    def __init__(self, use_bn=False, use_dp=False, num_classes=10):
        super(Subclass, self).__init__(name='mlp')
        self.use_bn = use_bn
        self.use_dp = use_dp
        self.num_classes = num_classes

        self.dense_1 = Dense(32, activation='relu')
        self.dense_2 = Dense(num_classes, activation='softmax')
        if self.use_dp:
            self.dp = Dropout(0.5)
        if self.use_bn:
            self.bn = BatchNormalization(axis=-1)

    def call(self, inputs):
        x = self.dense_1(inputs)
        if self.use_dp:
            x = self.dp(x)
        if self.use_bn:
            x = self.bn(x)
        return self.dense2(x)

model =Subclass()

Summary

In conclusion, this article explains about Keras Models and how to define your own model in Keras. We saw its two types that are Sequential and Functional API. We also discussed the flexibility above Models provide. After this, we saw how to create our own fully-customizable Model using Model Subclassing.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *