Introduction
Keras is a high-level python deep learning API used for easy implementation of Neural Networks. It has multiple low-level backends like TensorFlow, Theano, PyTorch, etc., which are used for fast computation.
Keras offers a python user-friendly front end while maintaining reliable computational power by using a low-level API like TensorFlow, Pytorch, etc., which uses a computation graph as backend. This allows for the abstraction of complex problems while specifying control flow.
Computation graphs are used to calculate derivatives by backpropagation.
Keras Model
Keras Models
Keras provides two types of models: Sequential and Functional models.
Sequential Model
Source: researchget.net
Sequential models are linear stacks of layers where one layer leads to another. We have to ensure that the previous layer is the input to the next layer. Each one of the nodes is a Tensor, and the layers are also considered the Tensor of values. It is used for simple classifiers and declassified models, and regression models. The term sequential is used in different areas and notations in Data Science. So when we talk about time series, we speak about sequential things, which is very different. Sequential, in this case, means it goes from the input to layer1 to layer2 to the output. So it is very directional. We will focus on the basic Keras model with the Sequential model.
This is how we can invoke a sequential model through Keras.
- First, we have to import Sequential from Keras models.
- Then we have to import whatever layers are required.
- We create the instance of the model and keep on adding the layers.
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential([
Dense(32, input_shape=(784, )),
Activation('relu'),
Dense(10),
Activation('softmax')
])
Functional models
Source: Link
Functional models help us to create complex models. The problem with sequential models is that we can give inputs only at the beginning stage. If I wish to add new information for the second layer, that wouldn't be possible. We can not add anything else in between. This is where the functional model differs. In the Functional model, it is unnecessary to follow the same sequence. In a Functional model, any layer can be connected to any other layer. There are three steps to define a Functional model.
- Defining the Input
- Connecting the layers
- Creating the model
Once we define the input, we start by building a set of layers. These layers can be connected anyways. I can connect the first layer with the fourth layer and the second layer with the tenth layer.
Let's understand each of the steps correctly.
When it comes to sequential models, we have to create and define a standalone input layer that specifies the shape of the input data. I am importing input methods and representing the form of input data; in this case, the figure is 2.
from keras.layers import Input
visible = Input(shape=(2,))
This is extremely important when it comes to Functional models. Once we create the input layer, we can add the other layers. I am adding a Dense layer.
from keras.layers import Input
from keras.layers import Dense
visible = Input(shape=(2,))
hidden = Dense(2)(visible)
This Dense layer has two nodes, and it is connected with the visible layer. So layers in Functional models are connected pairwise.
So after that, we have to create the model. First, we have to import the Model method from Keras models. Using this Model method, we have to pass in the inputs and the outputs. Input is the visible layer, and the output is the hidden layer.
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
visible = Input(shape=(2,))
hidden = Dense(2)(visible)
model = Model(inputs=visible, outputs=hidden)
Keras already has several predefined layers. Some of them are as follows:
1) Core Layers 6) Normalization Layers
2) Convolutional Layers 7) Noise Layers
3) Pooling Layers 8) Embedding Layers
4) Locally-connected Layers 9) Merge Layers
5) Recurrent Layers 10) Advanced Activational
You can learn more about these layers by visiting: Link.