Fundamentals of Layers and Models in KERAS
In this tutorial, we are going to learn about different layers and models provided in Keras library. We will be using python language for implementing these layers and models.
Intuition will be provided on the models and layers, different types of layers and activation functions.
Keras:
Keras is a Deep Learning API written in python language. It supports multiple backend engines like Tensorflow, Theano, etc. It’s easy to understand and user – friendly library which provides us with different functions involved in Deep Learning.
There will be two sections in this tutorial :
- Layers in Keras
- Models in Keras
- Activation Functions
Layers and Models – Keras
1. LAYERS in KERAS
Layers are the basic building blocks of models. Each layer will receive some information as an input, do the required computation and then output the result as required. If it’s more than a two-layered network then, the output of the previous layer will be fed as an input to the next layer.
Following are some of the layers used in neural networks :
- Core layers like the Dense layer.
- Convolution layers like the Conv2D layer.
- Pooling layers like the MaxPooling2D layer.
- Recurrent layers like LSTM(Least Short Term Memory) layer.
Keras layers require some minimum parameters or details to create a complete layer. These parameters or details are as follows :
- Input Shape:- This represents the shape of the input data that we are providing. We need to mention the input shape of our data in the first layer. After that, the output of the previous layers will be taken as input for the next layer.
- Number of Units:- Number of units is equal to the number of neurons to be present in that layer.
- Regularizers:- Regularization is used for checking penalties during optimization.
- Activations:- These are functions will tell us if the neuron is activated or not.
- Initializers:- Initializers provide functions to initialize the weights of the input data.
You can also make a customized layer as per your requirements.
2. MODELS in KERAS
Keras model is the representation of the actual neural network model. The Keras layers are connected to make the model as per the requirements.
There are two APIs present in Keras:
- Sequential API:- It arranges the Keras layers in sequential order, i.e the data will flow from one layer to the next layer. And this will continue till it reaches the final output layer.
- Functional API:- It is used for more complex models.
Keras model provides functions for different methods like :
- compile function:- This function will configure the learning process of the model.
- fit function:- This function is used to train our model using the training data.
- predict function:- This is to predict the results for any new input given.
- evaluate function:- To evaluate the model using the test data.
3. ACTIVATION FUNCTIONS
An activation function defines the output of the layer.
These are some of the activation functions generally used :
- relu function:- Rectified Linear Unit Activation Function.
- tanh function:- Hyperbolic Tangent Activation Function.
- sigmoid function:- For small values, it returns a value close to zero and for large values, the return is close to zero.
sigmoid = 1 / (1 + (exp ^ -x)). - softmax function:- It converts a real vector to a vector of categorical probabilities.
Now, let’s go for an example:
First, we need to import the necessary modules. We will import sequential model and the dense layer. We will be creating a model containing three layers – one input layer, a hidden layer, and an output layer.
from keras.models import Sequential from keras.layers import Dense
Now, we will create the model:
model = Sequential() model.add(Dense(16, input_shape=(16,), activation = 'relu')) model.add(Dense(8)) model.add(Dense(1, activation = 'sigmoid'))
So, a three-layer network model is created. The activation function of the first layer is relu and that of the output layer is sigmoid.
Now, we can access the model using model.layers.
layers = model.layers layers
The output of the above code is:
[<keras.layers.core.Dense at 0x7f2884bf2c88>, <keras.layers.core.Dense at 0x7f2884bf2358>, <keras.layers.core.Dense at 0x7f2884b354e0>]
You can also use other types of layers and make a model.
Thus, we have learned the basics of the layers and models in Keras.
For further information on Keras documentation, you can click here
Also, you may visit: Getting Started with Keras
Thank you for reaching till here.
Leave a Reply