Keras is an open-source neural-network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, R, Theano, or PlaidML. Designed to enable fast experimentation with deep neural networks, it focuses on being user-friendly, modular, and extensible. It was developed and is maintained by Francois Chollet.

Keras is easy to use and one can start his deep learning journey from it because of its ease of use.

To use Keras in your program, we have to write the following line:

import keras

We will understand it by taking an example. Our task is to predict the number drawn in an image. We would be using a simple Neural Network for this.

So we start by importing some functions that we are going to use to make our neural network.

from keras.datasets imprt mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten


Like we did in the last blog, we would in this blog too make a simple MNIST detector using Keras.

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

The input is a 28×28 image, so we need to transform it to feed in our neural network.

We would convert each input image into a 784 dimensional vector. Since there are 6000 training example and 1000 test examples, so we have X_train and X_test of dimensions 60000×784 and 10000×784 respectively.

X_train = X_train.reshape(60000,784)
X_test = X_test.reshape(10000,784)

We also need to scale the data. As the data values lie between 0 and 255 so we would divide it by 255.

X_train /= 255
X_test /= 255

Now we have to take care about the values we have to predict or the Y values. Since we have output in the range of 0 to 9. We would one-hot encode the values. The result is a vector with a length equal to the number of categories. The vector is all zeroes except in the position for the respective category. Thus a ‘5‘ will be represented by [0,0,0,0,1,0,0,0,0].

n_classes = 10
Y_train = np.utils.to_categorical(y_train,n_classes)
Y_test = np.utils.to_categorical(y_test,n_classes)

Now the pre-processing is over and time build our neural net architecture.

model = Sequential()
model.add(Activation('softmax'))

Where Dropout is the function to add dropout to the neural network layer. Its input parameter is a percentage which tells how much percent to neglect. The Activation is the activation function applied to the layer. These things may not be clear to you right now, but don’t worry. We will discuss it effectively in forthcoming blogs.

Now we need to compile our model.

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

We choose “categorical_crossentropy” as our output is of varied range (0-9). We choose the adam as the optimizer.

Now we need to fit or train the model.

model.fit(X_train, Y_train, batch_size = 128,
epochs = 20,verbose = 2,
validation_data = (X_test,Y_test)
)

We trained it for 20 epochs with a batch size pf 128. Verbose is a parameter which tells keras how to show us the training process. It can have three parameters 0, 1 and 2.

Thus we were able to achieve a 99 percent train accuracy within 20 epochs.

## Keras vs TensorFlow

KERAS                            TENSORFLOW

Categories: Deep Learning

$${}$$