Keras is a deep learning API that simplifies the process of building deep neural networks. Initially it was developed as an independent library, Keras is now tightly integrated into TensorFlow as its official high-level API. It supports multiple backend engines like TensorFlow, Theano and Microsoft Cognitive Toolkit (CNTK). Keras makes it easier to train and evaluate deep learning models without requiring extensive knowledge of low-level operations.
Installing Keras
1. Since Keras is now part of TensorFlow, it can be installed easily using pip:
pip install tensorflow
This command installs TensorFlow 2.x, which includes Keras.
2. To check the installation, open Python and run:
import tensorflow as tf
print('TensorFlow Version: ', tf.__version__)
print('Keras Version ', tf.keras.__version__)
Output:
TensorFlow Version: 2.18.0
Keras Version 3.8.0
History of Keras
Keras was developed by Google engineer named François Chollet. It was developed as part of research project called ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System) and it was released in March 2015.
The goal of Keras was to enable experimentation with deep neural networks. Later, Keras was tagged into TensorFlow as 'tf.keras', which made it an official high-level API of TensorFlow while having its own version that could interface with other computational backends like Theano or CNTK.
Key Features of Keras Library
| Aspect | Key Points |
|---|---|
| Simplicity | High-level API for the full ML workflow and reduces manual effort with clear, productive interfaces. |
| Customizability | Easily extendable with custom layers, losses and preprocessing, It supports both functional API and low-level model building. |
| Compatibility | Runs on TensorFlow, Theano and CNTK allowing consistent performance across CPU and GPU platforms. |
| Scalability and Performance | Uses scalable backends like TensorFlow |
| Fast Experimentation | Uses simple tools to build models quickly, making it great for research and testing |
How to Build a Model in Keras?
Keras provides two main ways to build models:
1. Building Model using Sequential API
The Sequential API are easy to work with models with a single input and output and a linear stack of layers
Here’s how you can define a Sequential model:
- We create a Sequential model.
- Add a fully connected (Dense) layer with 64 units and ReLU activation.
- Add another Dense layer with 10 units (for classification) and a Softmax activation.
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()
model.add(Dense(units=64, input_dim=100))
model.add(Activation('relu'))
model.add(Dense(units=10))
model.add(Activation('softmax'))
2. Building Model using Functional API
Functional API allows more flexibility in creating complex architectures. You can create models with shared layers, multiple inputs/outputs and skip connections.
For example:
- We define two input layers (input1 and input2).
- Create separate hidden layers for each input.
- Merge the hidden layers using the concatenate function.
- Finally, add an output layer with SoftMax activation.
from keras.layers import Input, Dense, concatenate
from keras.models import Model
input1 = Input(shape=(100,))
input2 = Input(shape=(50,))
hidden1 = Dense(64, activation='relu')(input1)
hidden2 = Dense(32, activation='relu')(input2)
merged = concatenate([hidden1, hidden2])
output = Dense(10, activation='softmax')(merged)
model = Model(inputs=[input1, input2], outputs=output)
Applications
- Image and Video Processing: Supports image classification, object detection, and video analysis using CNNs for applications like medical imaging and quality control.
- Natural Language Processing (NLP): Helps build models for tasks such as sentiment analysis, translation, and text summarization.
- Time Series Forecasting: Uses LSTM and GRU models to predict data in areas like finance and weather forecasting.
- Autonomous Systems: Processes sensor data for robotics and autonomous vehicles to support real-time decision making.