Introduction to Keras

Video Tutorial
FREE
1. What is Keras? thumbnail
This video belongs to
Keras & TensorFlow for Deep Learning
11 modules
Certificate
Topics Covered

Overview

Deep Learning, a subset of Machine Learning, involves training an Artificial Neural Network (ANN) with large datasets to make intelligent decisions in classification and regression tasks. The main idea behind Deep Learning is to allow the network to learn on its own using vast amounts of relevant training data. Thus, Deep Learning applications are highly promising when used in specialized areas such as computer vision, natural language processing, speech recognition, etc. To build these Deep Learning models, we can use a framework called 'Keras.' To answer 'what is Keras,' we need to be familiar with Deep Learning and TensorFlow.

The TensorFlow framework is an extremely popular, industry standard, and open-source end-to-end Machine Learning platform. The Keras framework is a high-level API for Neural Networks that runs on top of TensorFlow. It allows us to build complex ANN architectures to test and experiment on big data. Keras also supports GPU hardware when processing large datasets and developing highly accurate Machine Learning models.

What is Keras?

Keras is a Deep Learning high-level API written in Python that runs on top of open-source Machine Learning libraries such as TensorFlow, Theano, or Cognitive Toolkit (CNTK). Developed as part of the project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System), Keras was initially created by François Chollet, a software engineer and an artificial intelligence researcher at Google, and released in 2015.

TensorFlow is a very popular symbolic math library for creating neural networks and Deep Learning models. Although TensorFlow is very flexible and advantageous in distributed computing, it is difficult to understand for creating neural networks. Similarly, Theano is a Python library used for fast numerical computation tasks, while CNTK is a Deep Learning framework from Microsoft which uses Python, C#, C++ libraries, or standalone Machine Learning toolkits. Overall, Keras is preferred for Deep Learning tasks due to its minimal structure that makes it easy to create Deep Learning models based on TensorFlow or Theano. Keras has been designed to quickly define Deep Learning models making it an optimal choice for Deep Learning applications. As Keras is a high-level API for Deep Learning, users need to have some hands-on Python programming experience and basic knowledge of neural networks.

Features of Keras

Keras leverages different optimization techniques to make high-level API for neural networks easier and better performing. These are the main supported features of Keras −

  1. Consistent, simple, and extensible API: Designed for human beings, the Keras API follows best practices to reduce cognitive load. The result is simple and consistent APIs, a reduced number of user actions for common use cases, and the availability of clear & actionable error messages for troubleshooting. It also provides extensive documentation and developer guides.
  2. Minimal structure - easy to achieve the result without any frills: Keras enables users to develop models with fewer lines of code, and fewer functions to deal with, and thus, saves time.
  3. It supports multiple platforms and languages: Keras supports both Python and R. This makes it easier to run a code with TensorFlow, Theano, CNTK, or MXNet depending upon the requirement.
  4. It is a user-friendly framework that runs on CPU, GPU, and TPU: Keras also provides support for different hardware such as CPUs, NVIDIA or AMD GPUs, TPUs, etc. This is convenient as the models developed with Keras can be run with TensorFlow serving, GPU acceleration (WebKeras, Keras.js), Android (TF, TF Lite), iOS (Native CoreML) and Raspberry Pi.
  5. Highly scalable with technologies like XLA: Now, Keras has become an industry-strength framework built on top of TensorFlow 2. It is possible and easier to scale Keras models to large clusters of GPUs or an entire TPU pod.

How much Importance on Usability is Sought after in Keras?

Since Keras has been designed for humans, a significant focus is on user experience. Some notable features for user experience are -

  • Keras development follows the best practices to consciously decrease cognitive load, also ensuring consistency in the models and keeping the corresponding APIs simple.
  • Keras is not designed for machines which are why the emphasis is on providing clear feedback for any error that requires a minimum number of user actions for most common use cases.
  • Keras has been made easy to learn and use.
  • Keras is highly flexible as it allows all of its developers to integrate low-level Deep Learning languages (TensorFlow or Theano). This makes it possible to implement code written in the base language to be implemented in Keras.

How are the Modules Organized in Keras?

The Keras modules are organized as follows -

  1. Models: We can create a Keras model in three ways -
  • Sequential API: As the name suggests, there is a sequential arrangement of Layers in the Keras model, and a sequential model can include all the existing neural networks.
  • Functional API: This is an easy-to-use, fully-featured API that helps to create complex Keras models with arbitrary model architectures and is mostly used in the industry.
  • Model Subclassing: Alternatively, it is also possible to use Model subclassing to implement everything from scratch for complex, out-of-the-box research use cases. We can use the tf. Keras.Modelfor overriding the call() method.
  1. Layers

Layers are the fundamental units required for building any neural network. A layer consists of a ‘tensor-in tensor-out calculation function’ and some state for the same. The function indicates the layer's call method while the state shows the weight of the layers stored in TensorFlow variables. Keras provides layers for - * Stacking or Pooling layers, * Convolution Layers, * Core layers, and * Recurrent layers.

Keras also provides several pre-processing layers that help to process the input (raw structured data in different formats such as categorical, numerical, text and images). Additionally, it allows to performance of feature normalization on input data. This makes it easier to develop and export Machine Learning models in a single tool i.e., end-to-end workflow.

  1. Losses

Loss functions compute the value that a model seeks to minimize during training. Available losses are –

  • Probabilistic losses: The probabilistic losses include classes for different losses such as BinaryCrossentropy, CategoricalCrossentropy, SparseCategoricalCrossentropy, etc.
  • Regression losses: There are different classes available for regression losses, e.g., MeanSquaredError, MeanAbsoluteError, etc.
  • Hinge losses for "maximum-margin" classification while training classifiers.
  1. Callbacks

A callback is an object used to perform different actions at various stages of model training. Some of the available callbacks are – * BackupAndRestore allows the user to back up and restore the model training state. * TensorBoard to enable visualizations for TensorBoard. * EarlyStopping callback stops training when a monitored metric such as accuracy or loss stops improving.

  1. Schedulers

Keras offers a LearningRateScheduler that receives the updated learning rate value from the schedule function to adjust and control the learning rate of an optimizer over time. 6. Datasets The tf. Keras.datasets module offers a few small NumPy datasets which are already available in vectorized formats (NumPy arrays). This helps debug or test a Machine Learning model and create simple code examples.

Compatibility with Other Ecosystem Tools

Keras is the basis of development for a wide range of Machine Learning frameworks. Thus, Keras is a central part of the tightly-connected TensorFlow 2 ecosystem that covers end-to-end Machine Learning workflows.

  1. TFX TFX is a well-known production-scale Machine Learning platform based on TensorFlow for efficient ML pipelines suited to different targets like web, mobile or IoT devices. The TFX Pipeline supports native Keras models for production.

  2. TF Serving The TF Serving is a flexible, high-performance serving system for ML models that are designed for production environments. With TF serving, it is possible to deploy new algorithms and experiments while keeping the same server architecture and APIs. TF Serving easily exposes a trained Keras model via a model server.

  3. Neural Structured Learning Keras offers an API for Neural Structured Learning (NSL) which is a relatively new and open-source framework that allows training neural networks with structured signals in addition to features and helps to improve the model against corrupt or perturbed data.

  4. TFLite Keras supports the conversion of Keras models into TensorFlow Lite, an open-source, production-grade, cross-platform Deep Learning framework for pre-trained models to a special format optimized for speed or storage. TFLite models are suitable for deployment on edge devices such as Android or iOS mobiles or Linux-based embedded devices like Raspberry Pi or Microcontrollers.

  5. TFJS We can convert Keras models to TFJS format. TensorFlow.js is a JavaScript-based library that is used for developing ML models and allows using the ML models directly in the browser or Node. js.

Installing Keras

The recommended approach to install Keras is to install TensorFlow 2.0+, where Keras will be automatically installed. An easy way to install TensorFlow is by using the binary version of the official releases available on The Python Package Index (PyPI). Python 3.6+ is required for TensorFlow and Keras, along with the latest version of pip. To check the installed Python version, use this command -

and to upgrade the pip version, use -

Next, run the following code to install TensorFlow for CPU and GPU processors.

Once the installation is completed, we can check if Keras has been correctly installed by importing the library using -

If the above command gives an error "modulenotfounderror: no module named 'keras'" or "no module named keras." It is likely that the package has not been correctly installed, i.e., the Keras module is unreachable by Python, or the installed versions do not match the requirements to run Keras. A possible issue could be that Keras and TensorFlow are installed in different environments. We can retry installing these or use the following code to upgrade the packages.

Common Legacy Methods of a Keras Model

A Keras model provides several functions for training, evaluating, predicting, and saving the model. A few important ones are discussed below -

  1. compile: The compile() function configures the model for training. The most commonly used default syntax for compile() is

We can also specify a learning rate for the optimizer here. Eg:

  1. fit: This Keras method fit() trains the Deep Learning model for a defined number of epochs or iterations on a given dataset. To train the model, we generally use the following lines of code -
  1. predict: The predict() method generates predictions as output for the input samples in a given dataset.
  1. evaluate: This function returns the loss value & metrics values for the model for the test dataset and is performed in batches. Here is the commonly used syntax for evaluate ()
  1. save: Calling save() saves the model to TensorFlow SavedModel or a single HDF5 file. To call this function, we invoke it using the following lines of code.

All the above methods consist of multiple arguments which allow higher customization on the functions, so we have discussed only the common ones in each for ease of understanding.

Conclusion

That’s it! We have covered the basics about “what is Keras?” and why is it important as well as the features it offers users for Deep Learning. In this article, we also covered –

  • Keras installation
  • Keras Modules
  • Compatible Ecosystem Tools
  • A few legacy methods i.e. functions invoked while developing a model in Keras

So, now that you have started your Keras learning journey with this lesson, follow along with the remaining ones to build your Deep Learning skills with the Keras framework.