Learn Deep Learning using Keras | EduGrad

Deep Learning Definition –

Keras is a high-level neural networks API, which is capable of running on top of Tensorflow, Theano, or CNTK. Deep Learning with Keras is used for fast experimentation through a high level, user-friendly, modular and extensible API.Learn Deep Learning with Python | EduGrad

Keras can be made to run on both CPU and GPU.

Due to the complexity of the tasks that most neural networks have to accomplish, most people would expect that it’s hard to set up a neural network but with the help of libraries such as Keras it is very easy to execute neural networks by writing only a few lines of codes.

Keras can be installed using pip or conda:

pip install keras
conda install keras

Keras provides seven different datasets, which can be loaded in using Keras directly. These include image datasets as well as a movie review and house price datasets.

In this deep learning tutorial, we will understand what is deep learning and use the MNIST dataset of handwritten images, which contains 70000, 28×28 grayscale images with 10 different classes.

With the help of Deep Learning with Python using Keras, we can split it in a training set with 60000 instances and a testing set with 10000 instances.

We will begin by loading the dataset and necessary libraries

import numpy as np
import pandas as pd
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

To feed the images to a convolutional neural network we transform the data frame to four dimensions. This can be done using NumPy reshape method. We will also transform the data into floats and normalize it.

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

We will also transform our labels into a one-hot encoding using the to_categorical method from Keras. It basically converts categorical values to 0 and 1 form which is easier for the machine to process. Since, there are 10 categories hence we have written data, 10 in to_categorical function.

from keras.utils import to_categorical

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

The easiest way of creating a model in deep learning with Keras is by using the sequential API, which lets one stack one layer after the other.

The problem with the sequential API is that it doesn’t allow models to have multiple inputs or outputs, which are needed for some problems.

To create a convolutional neural network and deep learning, we only need to create a Sequential object and use the add function to add layers.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', 
input_shape=x_train.shape[1:]))
model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(rate=0.25))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(rate=0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(10, activation='softmax'))

Before we can start training our CNN deep learning model we need to configure the learning process. For this, we need to specify an optimizer, a loss function and optionally some measurement metrics like accuracy.

The loss function is a measure of how good our model is at achieving the given objective. An optimizer is used to minimize the loss function by updating the weights using the gradients.

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

Augmentation is a process of creating more data from existing once. For images we can perform little transformations like rotating the image, zooming into the image, adding noise and many more.

This helps to make the model more robust and solves the problem of having not enough data. Deep Learning neural network with Keras has a method called ImageDataGenerator which can be used for augmenting images.

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
  rotation_range=10,
  zoom_range=0.1,
  width_shift_range=0.1,
  height_shift_range=0.1
)

As we defined and compiled our model it’s ready for training.

To train a model we would normally use the fit method but because we are using a datagenerator we will use fit_generator function and pass it our generator, x data (features), y data (labels) as well as the number of epochs and the batch size.

We will also pass it a validation set so we can monitor the loss and accuracy on both sets as well as steps_per_epoch which is required when using a generator and is just set to the length of the training set divided by the batch_size.

epochs = 4
batch_size = 32
result = model.fit_generator(datagen.flow(x_train, y_train, batch_size = 
         batch_size), epochs=epochs,validation_data=(x_test, y_test), 
         steps_per_epoch = x_train.shape[0]//batch_size)

Epoch 1/4
1875/1875 [==============================] – 432s 230ms/step – loss: 0.3289 – accuracy: 0.8949 – val_loss: 0.0410 – val_accuracy: 0.9861

Epoch 2/4
1875/1875 [==============================] – 387s 206ms/step – loss: 0.1139 – accuracy: 0.9660 – val_loss: 0.0272 – val_accuracy: 0.9914

Epoch 3/4
1875/1875 [==============================] – 396s 211ms/step – loss: 0.0933 – accuracy: 0.9739 – val_loss: 0.0199 – val_accuracy: 0.9936

Epoch 4/4
1875/1875 [==============================] – 402s 214ms/step – loss: 0.0784 – accuracy: 0.9774 – val_loss: 0.0175 – val_accuracy: 0.9942

We can visualize our training and testing accuracy and loss for each epoch so we can get intuition about the performance of our model. The accuracy and loss over epochs are saved in the history variable we got whilst training and we will use Matplotlib (python library used for static visualization) to visualize this data.

import matplotlib.pyplot as plt

plt.plot(result.history['accuracy'], label='training accuracy')
plt.plot(result.history['val_accuracy'], label='testing accuracy')
plt.title('Training and Testing Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()

<matplotlib.legend.Legend at 0x7f3c58eb4128>

Introduction to Baye's theorem for Data Science - Visualizing the training and testing accuracy | EduGrad

Hence, we can say that Keras is a high-level API and can be extensively used for writing deep learning codes in shorter syntax.

Check-out our best-rated On-Demand courses – 

Data Science course using Python | EduGradLearn web scraping using Python - EduGradMaster Python for Data science and Machine Learning - EduGrad

Instructor-led Live Sessions – 

Learn Deep Learning with Python | EduGrad Python for Data Science Certification course - EduGrad

Industrial Dataset Data Science and Machine Learning Projects –

Build Predictive Regression Models in Machine Learning - EduGrad Build Recommendation system in Python - EduGrad Sentiment Analysis and Topic Modeling using NLP - EduGrad

LEAVE A REPLY

Please enter your comment!
Please enter your name here