How to create Models in Keras?
Last Updated :
28 Nov, 2021
Keras is an open-source API used for solving a variety of modern machine learning and deep learning problems. It enables the user to focus more on the logical aspect of deep learning rather than the brute coding aspects. Keras is an extremely powerful API providing remarkable scalability, flexibility, and cognitive ease by reducing the user’s workload. It is written in Python and uses TensorFlow or Theano as its backend.
Models in Keras
A typical model in Keras is an aggregate of multiple training and inferential layers. There are two broad methods of creating models using Keras.
The Functional API
The Functional API handles non-linear models with diverse functionalities. These models are extremely scalable and flexible. You can specify your neural network’s forward pass starting right from the input and all the way down to the output to create personalized models. It provides a resilient architecture wherein pairs of layers can connect to multiple layers in any fashion. The functional API can be said to be a way to build graphs of layers and ad-hoc acyclic network graphs. This helps users tailor complex networks like the Siamese network with extreme ease. Creating a model with the functional API is a multi-step process that is defined here.
1.) Define Inputs
The first step in creating a Keras model using the functional API is defining an input layer. The input layer accepts the shape argument which is actually a tuple. This is used to define the dimensionality of the input.
Python3
from keras.layers import Input
visible = Input (shape = ( 10 ,))
|
The last dimension of the aforementioned one-dimensional input is always left hanging so as to compensate for the shape of the mini-batch size used when splitting the data for training, testing, and validation. Hence, the vacancy after the ‘comma‘.
2.) Connecting Layers
After we have created the input layer, we shall now create a dense, hidden layer. This hidden layer shall be connected to our input layer and receive input from it.
Python3
from keras.layers import Input
from keras.layers import Dense
visible = Input (shape = ( 10 ,))
hidden = Dense( 2 )(visible)
|
This is how the hidden layer is connected to the visible input layer. The Dense layer is used for passing the outputs from one layer of neurons to a separate layer of neurons as inputs.
Note that in this case inputs from only one layer are passed into the output layer due to personal demands. However, functional models allow for multiple inputs to be fed into the neural network to reap multiple outputs also. Functional models are extremely popular due to the ease with which connections could be established. You can now add as many layers as you want.
3.) Create your Model
The desired model can now be created in the easiest way by using the Model class. Only the input and output layers need to be defined.
Python3
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
visible = Input (shape = ( 10 ,))
hidden = Dense( 2 )(visible)
model = Model(inputs = visible, outputs = hidden)
|
The model is now created with the input being the visible layer and the output being the hidden layer. This is how the functional API helps users create neural network models without any hassle.
The Sequential API
The Sequential API is a slightly less refined API for modelling simpler neural networks. Each layer in the network accepts only one input and passes on a single output. It is important to note that sequential models don’t work when the model requires multiple inputs or outputs or when layers need to be shared. A sequential model can only be used in a network that has a linear topology.
Create a Sequential Model
Let us create a sequential model with three layers. After all the imports have been made, we start by creating the layers.
Python3
model = keras.Sequential(
[
layers.Dense( 2 , activation = "relu" ),
layers.Dense( 3 , activation = "relu" ),
layers.Dense( 4 ),
]
)
|
This is how we create a model with three layers. The activation function used in this case is ‘ReLu’, or rectified linear activation unit. The integral arguments passed into Dense indicate the number of neurons in each layer.
Note that the add() method can also be used to add layers into a model.
Python3
model = keras.Sequential()
model.add(layers.Dense( 2 , activation = "relu" ))
model.add(layers.Dense( 3 , activation = "relu" ))
model.add(layers.Dense( 4 ))
|
References
- https://keras.io/guides/sequential_model/
- https://machinelearningmastery.com/keras-functional-api-deep-learning/
Please Login to comment...