Introduction to Deep Learning with Keras (Part 2): Keras API


Banner Image

Keras makes approaching deep learning easy, especially for those who are just starting out. In this article, we will learn some of the most important features and functions of Keras along with the Sequential API.

This is the second part of the series Introduction to Keras Deep Learning.
Part 1: Getting Started with Keras.

Part 2: Learning about the Keras API.

What will You Learn in this Article?

  • Using Keras Sequential model.
  • How to stack layers?
  • Compiling of a model in Keras.
  • Training in Keras.
  • Different types of loss functions and optimizers in Keras.

Note: Always remember to look into the official docs when getting stuck.

Keras Sequential Model

To build a neural network model in Keras, there are two ways. You can either use the Sequential model or the functional model. In this article, we will focus on the Sequential model which is just perfect for beginners.

Concerning the Keras Functional API, it is used for building complex models, where we need to have more control over the structure, inputs, and outputs of the model. We will get there once we are comfortable with the Sequential model.

To use the Sequential model, we first need to import and initialize it. The following code shows how to do it.

# import the `Sequential` model
from keras.models import Sequential

# initialization
model = Sequential()

Now, whenever we will need to build/stack the layers of our neural network, we can easily do that by referring to model and calling the add() method. We will get to that very shortly.

Adding/Stacking of Layers

Before we move to the code part of stacking layers, you should first know some of many types of layers that Keras provides.
1. Core Layers: Input(), Dense(), Dropout(), Flatten(), Dropout(), Activation().
2. Convolutional Layers: Conv1D(), Conv2D(), SeparableConv1D(), SeparableConv2D().
3. RNN Layers: LSTM(), GRU().

The above list does not cover all the layers, as there are many more. But these should be enough for starting out. Also, if you are new to deep learning, these may not make much sense. We will get more familiar with these as we use them in practice.

Now, let’s see how to add Dense() layers in Keras using add().

# import the Dense layer
from keras.layers import Dense

# stack layers on top of each other
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

If you observe the code, you will see three new terms, units, activation, and input_dim. So, let’s see what each of them signify.

units: This parameter signifies the output dimensionality of the layer. So, in the above code, the first Dense() has 64 output dimensionality and the second one has 10.
activation: The activation function signifies under what conditions the neurons in the network will be fired or activated. Basically, they are mathematical functions having a particular range in which they work. To know more, see my previous article on activation functions.
input_dim: This signifies the number of dimensions of features, which in most cases if the number of feature columns.

Again, all of these will start to make more sense as you start using them while programming. Now, moving on to the model compilation part.

Model Compilation

After you build your model, you need to compile it. Compilation is basically configuring the model for training. We will use the compile() method for it. The following code shows how to compile a model in Keras.

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

Covering each of the parameters, let’s see what each of them does:
loss: The loss function is also known as the objective function. The loss function value is always minimized during training. You can choose the required loss function based upon the number of outputs. Whether the output is binary or multiple outputs. Some of the frequently used loss functions are mean_squared_error, mean_absolute_error, categorical_crossentropy, binary_crossentropy. To know more about loss functions click here.
optimizer: The optimizer in Keras helps to minimize the loss function during training. In short, while training neural networks, we need to minimize the loss function. To do so, we need to find the right parameters. And to find the right parameters we use the optimizer. There are some optimizers which give faster results than others and therefore, we need to choose the optimization algorithm wisely for the problem at hand. adam, sgd, RMSprop are some of the well-known optimizers.
metrics: Here, we pass the metric that is to be evaluated during model training and testing. It can be the error rate or it may be accuracy. For example, in the above code snippet, we are using accuracy as the metric to monitor the model training and testing. Similarly, if you would like to monitor the error rate, then you can use mae as the metric, which stands for ‘mean absolute error‘.

Training, Evaluating and Predicting in Keras

Training a neural network model in Keras is very simple. It is just one line of code using the fit() method.

model.fit(x_train, y_train, epochs=10, batch_size=64)

x_train and y_train: The x_train holds the training data and y_train holds the training labels or target.
epochs: With the epochs parameter we can control for how many iterations will our model run over the whole dataset.
batch_size: This defines the number of samples that the algorithm takes at a time to feed into the network. For example, if you have 100 samples and define the batch_size as 20, then the algorithm takes 20 samples at a time and this will continue for 5 times.

Next, to evaluate our model on test data, we use the evaluate method. This method returns a list containing the loss and the metric that we have defined during the model compilation.

evaluation = model.evaluate(x_test, y_test, batch_size=64)

We can also predict the class labels or targets using the predict method if we have the x_test only.

predictions = model.predict(x_test, batch_size=64)

Buiding models and training them with Keras is really easy. Once you get more familiar with the environment, you will start to enjoy using Keras a lot more and deep learning specifically.

In the next article, we will see the working of the Sequential API along with code examples.

Summary and Conclusion

In this article, you got to know about the basics of Keras along with its layers, optimizers, losses, and activation functions. You also learned to train and evaluate your model. If you want to learn more then you can start by implementing some simple examples. Go to Kaggle, and start by looking into some beginner tagged kernels on deep learning. You will learn a lot.

If you liked this article then comment, share and give a thumbs up. If you have any questions or suggestions, just Contact me here. Be sure to subscribe to the website for more content. Follow me on Twitter, LinkedIn, and Facebook to get regular updates.

Liked it? Take a second to support Sovit Ranjan Rath on Patreon!
Become a patron at Patreon!

3 thoughts on “Introduction to Deep Learning with Keras (Part 2): Keras API”

  1. Pingback: Callbacks in Keras

Leave a Reply

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