Topics to cover:

  1. Introduction to Keras
  2. Features
  3. Guiding Principles of Keras
  4. Keras Workflow
  5.  Keras vs Tensorflow
  6. Examples for Building Simple Model using Keras
  7. Advantages
  8. Disadvantages

Introduction to Keras

Keras is a deep learning framework for Python Machine Learning and Deep Learning that provides a convenient way to define and train almost any kind of deep learning model. Keras is a high-level neural networks API, written in Python which is capable of running on top of Tensorflow, Theano and CNTK. It was developed for enabling fast experimentation.

Keras does not involve low-level computations so it makes use of Backend library to solve those tasks (high-level computations) such that it minimizes the error loss and optimizes the target function for achieving great accuracy results and can handle multiple input-output models.

Some important features of Keras

  1. Allows for easy and fast prototyping.
  2. Can run seamlessly on CPU and GPU (Often trained on GPU).
  3. Supports both convolutional networks(for computer vision) and recurrent networks(for sequence and time-series), as well as the combination of two.
  4. Supports arbitrary network architectures: multi-input or multi-output models, layer sharing, model sharing and so on. This means Keras is appropriate for building deep learning models.

Keras recommend users to switch to tf.keras in Tensorflow 2.0, who use multi-backend keras with the tensorflow backend.

Guiding Principles of Keras

  • User Friendliness.
  • Highly flexible for training neural network models.
  • Extensive Modularity.
  • Easy Extensibility.
  • Work with Python.

Keras doesn’t handle low-level operations such as tensor manipulations and differentiation. Instead, it relies on a specialized, well-optimized tensor library to do so which serves as the backend engine of Keras. We can use several backend engine for keras, and currently three existing backend implementations are the Tensorflow backend, the Theano backend, and the Microsoft Cognitive Toolkit (CNTK) backend.

The typical Keras workflows looks like:

  • Define your training data: input tensor and target tensor
  • Define a network of layers(or model ) that maps input to the targets specified.
  • Configure the learning process by choosing a loss function, an optimizer, and some metrics to monitor.
  • Iterate your training data by calling the fit() method of your model and evaluate the performance of the model.

Keras vs Tensorflow

  1. Keras uses high-level APIs while Tensorflow uses both high-level and low-level APIs.
  2. Comparatively speed of Tensorflow is fast than Keras.
  3. Compared to Tensorflow architecture Keras is simple, more readable and concise.
  4. Keras supports multiple back-end APIs whereas Tensorflow provides object detection functionality (base).
  5. Keras is built in Python which makes it way more user-friendly than Tensorflow.
  6. Keras offers simple and consistent high-level APIs and follows best practices to reduce the cognitive load for the users.

Examples for Building Simple Model using Keras

Basic model building in Keras as follows:

You can define your model in two ways:

  • Sequential Class: Linear Stack of layers

from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(32, activation='relu', input_shape=(784,)))
model.add(layers.Dense(10, activation='softmax')
  • Functional API: Directed acyclic graphs of layers

input_tensor = layers.Input(shape=(784,))
x = layers.Dense(32, activation='relu')(input_tensor)
output_tensor = layers.Dense(10, activation='softmax')(x)
model = models.Model(inputs=input_tensor, outputs=output_tensor)

Implementing Loss Function, Optimizer and Metrics

from keras import optimizers
model.compile(optimizer=optimizers.RMSprop(lr=0.001),loss='mse',  metrics=['accuracy'])

Finally, passing input and target tensors

model.fit(input_tensor, target_tensor, batch_size=128, epochs=10)

This the basic workflow of implementing Keras neural networks model and based upon the requirements more models can be developed for other neural network model architectures such as VGGNet, InceptionV3, ResNet, etc.

Advantages

  1. User-friendly and has fast deployment production of models based on the model complexity.
  2. Offers consistent and simple APIs.
  3. Keras has the ability to built neural network models with fewer lines of code. It has good support for functions that enable users for fast deployment.
  4. Great community and calibrated documentation which is simple, more readable and concise.

Disadvantages

  1. Does not provide support for low-level APIs compared to Tensorflow.
  2. Only used on small datasets.
  3. Inefficient errors making model ineffective to debug the root cause problem.
  4. Training speed is slow compared to Tensorflow.