What is Keras?

Keras is a high-level neural networks API. It is written in Python and can run on top of Theano, TensorFlow or CNTK. It was developed with the idea of:

Being able to go from idea to result with the least possible delay is key to doing good research.

Keras is a user-friendly, extensible and modular library which makes prototyping easy and fast. It supports convolutional networks, recurrent networks and even the combination of both.

Initial development of Keras was a part of the research of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).

Why Keras?

There are countless deep-learning frameworks available today, but there are some of the areas in which Keras proved better than other alternatives.

Keras focuses on minimal user action requirement when common use cases are concerned also if the user makes an error, clear and actionable feedback is provided. This makes keras easy to learn and use.

When you want to put your Keras models to use into some application, you need to deploy it on other platforms which is comparatively easy if you are using keras. It also supports multiple backends and also allows portability across backends i.e. you can train using one backend and load it with another.

It has got a strong back with built-in multiple GPU support, it also supports distributed training.

Keras Tutorial

Installing Keras

We need to install one of the backend engines before we actually get to installing Keras. Let’s go and install any of TensorFlow or Theano or CNTK modules.

Now, we are ready to install keras. We can either use pip installation or clone the repository from git. To install using pip, open the terminal and run the following command:

In case pip installation doesn’t work or you want another method, you can clone the git repository using

Once cloned, move to the cloned directory and run:

Using Keras

To use Keras in any of your python scripts we simply need to import it using:

Densely Connected Network

A Sequential model is probably a better choice to create such network, but we are just getting started so it’s a better choice to start with something really simple:

Now that you have seen how to create a simple Densely Connected Network model you can train it with your training data and may use it in your deep learning module.

Sequential Model

Model is core data structure of Keras. The simplest type of model is a linear stack of layers, we call it Sequential Model. Let’s put our hands in code and try to build one:

Let’s run the program to see the results:

Let’s try a few more models and how to create them like, Residual Connection on a Convolution Layer:

Shared Vision Model

Shared Vision Model helps to classify whether two MNIST digits are the same digit or different digits by reusing the same image-processing module on two inputs. Let’s create one as shown below.

Visual Question Answering Model

Let’s create a model which can choose the correct one-word answer to a natural-language question about a picture.

It can be done by encoding the question and image into two separate vectors, concatenating both of them and training on top a logistic regression over some vocabulary of potential answers. Let’s try the model:

If you want to learn more about Visual Question Answering (VQA), check out this beginner’s guide to VQA.

Training Neural Network

Now that we have seen how to build different models using Keras, let’s put things together and work on a complete example. The following example trains a Neural Network on MNIST data set:

Let’s run this example and wait for results:

The output shows only the final part, it might take a few minutes for the program to finish execution depending on machine

Conclusion

In this tutorial, we discovered that Keras is a powerful framework and makes it easy for the user to create prototypes and that too very quickly. We have also seen how different models can be created using keras. These models can be used for feature extraction, fine-tuning and prediction. We have also seen how to train a neural network using keras.

Keras has grown popular with other frameworks and it is one of the most popular frameworks on Kaggle.

By admin

Leave a Reply

%d bloggers like this: