Getting Started with Keras

Artificial Intelligence Deep Learning Keras

In this blog, we will discuss flexibility of using Keras and get our hand dirty by building a simple CNN model. Keras is high level python library for building networks in deep learning. Keras runs on the low level deep learning library such as “TensorFlow”, “Theano” and “CNTK”. Keras is easy to use and simple in implementation. There are two kinds of frameworks for building models: sequential API and functional API. We will discuss how to deploy Sequential API of Keras.

There are eight steps to the deep learning process in the Keras:

  1. Load the Data.
  2. Preprocess the Data.
  3. Define the Data.
  4. Compile the Model.
  5. Fit the Model.
  6. Evaluate the Model.
  7. Make Prediction.
  8. Save the Model.

Let’s discuss first four steps one by one or Sequentially. The first step will be importing and loading the data.

Now we will preprocess the data imported from Keras of cifar10.

Now we will convert our classes one hot vector.

Now it’s the time to define Our model in Keras. Our model will be a Sequential model. First hidden layer will contain 512 hidden neurons with activation function “relu”. Then we will add dropout layer with 0.2, Then we will add a layer of hidden units containing 120 neurons with activation “relu”. Then will add our output layer with 10 units as we have 10 labels. Size of out image is 32 x 32 with RGB channels so when we flatten it. Then it will have 32 x 32 x 3 = 3072 features. So, Architecture of our model will be

3072 features   ⟾ 512 nodes ⟾ 120 nodes  ⟾ 10 nodes

Code in Keras.

Now we will define optimizer and loss for our model. Popular loss functions used are binary cross entropy, Categorical cross entropy, mean_sqaured_lagarthmic_error, mse, hinge loss. Popular optimizers are SGD, Adam, RMSprop, and adadelta. Popular evaluation metrics are accuracy, recall, F1 score.

Compiling Model:

So, we have discussed how to load data, preprocess data, build a model and compile the model. Last four step will be covered in next blog. There is some problem we face while defining our model.

What should be our architecture?

The answer to our problem is Trial and error mixed with some intuition. There is no paper which will give you an general and exact idea what will be your model architecture. You have to guess it and check it by running it on your dataset.

This article is taken from the book “Deep Learning with Applications using Python”

Leave a Reply

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