Keras Models(CNN): Functional Vs. Sequential(MNIST DATA SET)

I assume that you have basic knowledge of Convolutional Neural network layers(Con2d,Con1d,Maxpooling etc.).

WHAT IS KERAS MODELS?

Keras model is a data structure about how we stack our neural network layers.We can stack our neural network layers mainly in three different ways – 1)Sequential 2) Functional 3)Subclass.

Sequential model(API) : It is a very simple and straight forward architecture,were we arrange the layers one after another in an orderly manner but it is limited to single-input, single-output stacks of layers.Sequential model can be created by simply calling Sequential().

from keras.models import Sequential
model = Sequential()
Keras Sequential Api – mc.ai
Sequential API

Functional model(API) :

The Keras functional API is a way to create models that are more flexible than Sequential API. The functional API can handle models with non-linear topology, shared layers, and even multiple inputs or outputs.This supports arbitrary model architectures. For most people and most use cases, this is what you should be using. This is the Keras “industry strength” model.In the functional API, models are created by specifying their inputs and outputs in a graph of layers. That means that a single graph of layers can be used to generate multiple models.In fact, you can connect layers to (literally) any other layer. As a result, creating complex networks such as **siamese networks and residual networks become possible.

The Functional API
Functional API fig 1
How to give labels of multiple inputs in Keras to model.fit ...
Functional API fig 2

Subclass model :

Inside of Keras the Model class is the root class used to define a model architecture. Since Keras utilizes object-oriented programming, we can actually subclass the Model class and then insert our architecture definition.

Model subclassing is fully-customizable and enables you to implement your own custom forward-pass of the model.

However, this flexibility and customization comes at a cost — model subclassing is way harder to utilize than the Sequential API or Functional API and mainly used by the researchers.

**A Siamese networks consists of two identical neural networks, each taking one of the two input images. The last layers of the two networks are then fed to a contrastive loss function , which calculates the similarity between the two images.

Code : https://github.com/Antikadas1/Deep_Learning/blob/master/MNIST_FUNC_SEQ.py