Deep Learning tutorial with Keras

Esther Vaati
Analytics Vidhya
Published in
6 min readMay 18, 2020

--

Deep learning is a machine learning methodology that studies networks with many layers. In this tutorial, you will build a neural network with Keras.

Objectives

  1. Introduction to Keras
  2. The relationship between Tensor flow, Keras, and Theano
  3. Advantages of using Keras and how it makes everything easy
  4. Train a neural network with Keras

Prerequisites

Before going forward, you should have some knowledge of the following technologies

  • Machine learning
  • Python

Requirements

The following libraries are required, so ensure that you have them installed:

  • TensorFlow
  • Keras

Installing Keras

To install Keras, use the pip package as follows:

Introduction to Keras

Keras is a high-level Neural network. It makes it possible to implement deep learning models in a fast and efficient way. The simplest model in Keras is known as the Sequential model and is defined as follows.

TensorFlow, Keras, Theano.

Keras is a high-level library used to build deep neural networks. When you talk of high-level, it means simplicity and human-readable. You will not dive deep into all the mathematical concepts of neural networks, but you will only focus on building deep learning networks.

Keras makes use of either Tensor flow (by Google) or Theano backend (by Montreal’s Theano) to give it computational capabilities; this means that the Keras library can run on both Tensor flow and Theano frameworks. The choice of which framework to use depends on your taste. However, in this tutorial, you will use Tensor flow as our backend.

Why Keras?

Keras has gained popularity over the years because it leverages the use of both Tensor flow and Theano. Other advantages include:

  • It makes use of python, which is very easy to learn and use.
  • It is beginner-friendly.
  • Easy to build networks — Compared to other frameworks, you can quickly build a simple or deep network in Keras with only a few lines of code.
  • Keras also provides support for both GPU’s and CPU’s.

Metrics

A metric is a function that is used to judge the performance of your model. Metrics greatly influence the performance of deep learning algorithms; therefore, it’s crucial to choose the right metrics to ensure your model performs as expected.

Some of the metrics available in Keras include:

  • binary_accuracy
  • categorical_accuracy
  • sparse_categorical_accuracy
  • top_k_categorical_accuracy
  • sparse_top_k_categorical_accuracy
  • custom metrics

Different models give different results when evaluated with different metrics. For example, a metric like “accuracy” will perform better only if your training set has an equal number of samples belonging to each class.

So it is not enough to truly judge your model with only one type of metric.

Train/Test Split

Train/ Test Split is a concept that involves splitting data between training and testing sets and then fitting a model on the train data. Fit model to make predictions on the test data and then evaluate the skill of those predictions.

Train/ Test Split is mostly used to avoid overfitting.

Overfitting occurs when a training model gives an accuracy that is almost too close to the training dataset. Even though the aim is always to get a good prediction, overfitting will result in an inaccurate indication when it starts training on new data.

The imdb Dataset

According to the website

“Dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a sequence of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer “3” encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: “only consider the top 10,000 most common words, but eliminate the top 20 most common words”.

As a convention, “0” does not stand for a specific word, but is instead used to encode any unknown word.

The imdb dataset will form the basis of our discussion throughout this tutorial, and you will use it to build a neural network and make predictions with it.

Build and train a Neural Network

You will do the following:

  • Import the imdb dataset.
  • Prepare the dataset for processing.
  • Create the model.
  • Training.
  • Evaluation.
  • Prediction.

1.Import the imdb dataset

Go ahead and import the dataset.

The data comes prepackaged in such a way that each integer represents a specific word in a dictionary.

https://gist.github.com/c28614e24aee74bf94ed91fc6ad58abf

Then load the reviews as shown:

You can confirm that each set of the dataset contains 25,000 movie reviews by looking at the shape of the dataset.

Keras requires all the reviews to be of the same length, so you need to make them the same length. In this case, you have the option of choosing the longest review to be your default length, or you can choose a standard length. This means that the reviews of a longer length than the standard will be truncated.

You will use a standard length of 100:

Let’s see the result of padding by printing the result to the console:

As you can see, the arrays are of equal length.

2. Create the model.

Now that the data is ready, you can proceed to build our model. A model is the core data structure of Keras and is how layers are organized in Keras. The simplest type of model is the Sequential model, and it is defined as follows:

3. Adding Layers

A neural network consists of mainly three types of layers which are, input layers, the hidden layers, and the output layer. The input layer is used to feed raw data to the network, and it then communicated it to the hidden layers for processing.

The hidden layers will process the data by learning different aspects of the data. When processing data, the data may be broken down into smaller units so that its easier for different components of the data to be processed like the way the human brain does it. The output layer is where you get your results.

There are different types of layers, and the most commonly used layers are:

  • Dense layers
  • Recurrent layers
  • Normalization layers
  • Convolutional layers
  • Others

Different layers perform different transformations on data, and some layers are better suited for certain tasks than others. For example, a convolutional layer is best suited for data that contain images while an Embedding layer is best suited for data that contains text.

Let’s start stacking up layers.

The first layer will be the embedding layer, which converts each word into a word vector.

Next, add the LSTM layer found in keras.layers.LSTM. LSTM layers are preferred because they can learn long-term dependencies of data and thus make accurate predictions. The LSTM will transform the vector sequence into a single vector.

Let’s go ahead and add the LSTM layer.

Lastly, you will add a simple dense layer from keras.layers.Dense. A dense layer is a type of layer that connects each input to each output within its layer.

4. Compiling the Model

When compiling the model, you must define some metrics which determine the performance of your model. Here you set the loss (cost function) to binary_crossentrophy and the metric to accuracy. This is meant to maximize the accuracy of the algorithm.

5. Training

Now you are ready to train the model. Call the fit function on the model and pass additional arguments i.e.

  • epoch — represents the number of loops you go through your training set.
  • batch_size -represents the size of your training sample used to train the network during its learning process.

The result is as shown below. The model performs with an accuracy of 83% and a loss of 0.4, which is a good thing.

Model accuracy

Conclusion

As you have seen, Keras provides an easy and efficient way to build a neural network without all the bottlenecks that come with other Low-level libraries.

--

--