Introduction to Keras — Deep Learning Library

Paras Patidar
MLAIT
Published in
3 min readDec 16, 2019

What You Will Learn?

  • Introduction to Keras
  • Example Code, explaining Keras

Introduction to Keras

Keras is a deep learning framework for Python 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.

Being able to go from idea to result with the least possible delay is key to doing good research.

Keras has the following features :

  • Allows for easy and fast prototyping
  • Run seamlessly on CPU and GPU
  • Supports both convolutional networks(for computer vision) and recurrent networks(for sequence and time-series), as well as the combination of two.
  • It 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, from generative adversarial networks to a neural Turing machine.

Keras is compatible with versions of Python from 2.7 to 3.6 till date.

Keras is used by around 200,000 users, ranging from academic researchers and engineers at both startups and large companies to graduate students and hobbyist. Keras is used at Google, Netflix, Uber, Microsoft, Square and many startups working on the wide variety of machine learning problems.

Deep Learning Book by Francois cholloet

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

Guiding principles

  • User Friendliness
  • 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.

Example Code, explaining Keras

Install Keras

pip install keras

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 our targets.
  • 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.

You can define your model in two ways :

  • Sequential Class: Linear Stack of layers
from keras import modelsfrom keras import layersmodel = 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 optimizersmodel.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 is the basic overview of keras and we will be making more tutorials on keras. Want to learn about neural networks? , check this article :

Thank You!

Stay Tuned & Stay Connected with #MLAIT

Follow Us for more tutorials on ML, AI and Cloud and join telegram group of MLAIT

--

--