Keras is a deep-learning open-source framework that is written in Python. The framework is designed to make building, training, and deploying neural networks simple, flexible, and intuitive. Keras, a multi-framework API, can be used for developing modular components that are compatible with JAX, TensorFlow, and PyTorch.
Initially developed as an independent library, Keras later became TensorFlow’s official high-level API via tf.keras. As of Keras 3, it is also available as a standalone, multi-backend library that runs on top of TensorFlow, JAX and PyTorch. Keras API, designed for humans (not machines), focuses on debugging speed, code maintainability, code conciseness, and easy deployability.
Keras leverages GPUs (and, where supported, TPUs) via its backends. With Keras 3, GPU/TPU acceleration is provided by the chosen backend—typically TensorFlow, JAX or PyTorch.
In this detailed Keras guide, we look at the various aspects of the Keras deep learning framework and how cloud GPUs for Keras can be harnessed to accelerate training and deployment of deep learning workloads.
What is Keras?
As stated earlier, Keras is an open-source Python-based deep learning framework used for building and training neural networks. Since Keras is a high-level, user-friendly library, it enables fast experimentation and iteration, thereby lowering the barrier to deep learning.
Starting with TensorFlow 2.x, you can use tf.keras directly in the code without installing Keras separately. With this, Keras is primarily used for model building and training, whereas TensorFlow handles GPU acceleration, model deployment and execution under the hood!
At the time of writing this blog, the latest version of Keras is v3.13.0. The complete source code of Keras can be found in the below repositories:
| Keras Component | GitHub Link |
|---|---|
| Keras (Core Deep Learning Framework) | https://github.com/keras-team/keras |
| Keras Hub (Pre-trained model repository for Keras 3) | https://github.com/keras-team/keras-hub |
| Keras RS (Multi-backend recommender systems with Keras 3) | https://github.com/keras-team/keras-rs |
Until Keras 2.x, the Keras API could run on several different backends, including Theano and CNTK, with additional community integrations for frameworks such as MXNet and PlaidML. With Keras 3, those older backends are no longer supported. Instead, Keras 3 officially targets TensorFlow, JAX and PyTorch as backends, and you select the backend by setting the KERAS_BACKEND environment variable (for example, “tensorflow”, “jax” or “torch”).
Keras Use Cases and Real-World Applications
Keras is commonly used in a wide range of deep learning applications, from rapid prototyping and production deployment to time-series forecasting and computer vision. Flexibility and adaptability of the framework help with faster iteration and accelerated prototyping in model development.
Here are some of the prominent use cases of the Keras deep learning framework:
Picking an architecture suitable for a problem
Developers can use Keras to choose and implement neural network architectures that best suit their requirements. For example, CNNs are ideal for image-related tasks, whereas transformer-based models are ideal for text and sequential data.
The Sequential API class in Keras is best-suited for basic CNNs (e.g., image classification) or simple RNN-based models for sequence tasks. Since the model is a simple, linear stack of layers, the Sequential API class makes stacking, modifying, and experimenting with the layers very easy!
On the other hand, the Functional API class in Keras is more appropriate when the model architecture is non-linear (or complex). Models with multiple inputs (or outputs), skip connections, or shared layers can make use of Keras Functional API class.
Developers can leverage Keras to rapidly adapt model architectures based on dataset size, accuracy needs, and performance constraints.
Image recognition using weights trained on ImageNet
Keras provides easy access to built-in datasets through its keras.datasets API for experimentation. Large-scale datasets like ImageNet are not included (by default), but can be used indirectly via pre-trained models (e.g., ResNet, VGG, EfficientNet, and MobileNet) available in keras.applications.
Model weights are downloaded automatically during the instantiation stage of the model. They are normally stored in ~/.keras/models. Using ImageNet-trained weights results in significant reduction in training time. Along with this, it also provides the flexibility to developers for fine-tuning and adapting the model as per their specific image recognition use cases.
Time-series analysis and forecasting
Keras, as a deep learning library, can be used for a variety of time series tasks. A time-series library, such as Pandas or numerical libraries like NumPy can be leveraged for underlying array operations.
Time-series analysis and forecasting in Keras are handled using Recurrent Neural Networks (RNNs), specifically Long Short-Term Memory (LSTM) networks, and Transformer models. Time-series analysis and forecasting in Keras can be used in a range of use cases like demand forecasting in retail, capacity planning, to name a few.
As mentioned in the official Keras time-series documentation, anomaly detection is also supported by Keras whereby normal time-based behavior is learnt and deviations are identified from the expected patterns. Some of the domains in which anomaly detection with Keras can help identify anomalies and suggest corrective steps (or actions).:
- Fraud detection in financial transactions
- Energy consumption monitoring
- Healthcare monitoring systems
- Predictive maintenance in manufacturing
- IoT & smart homes
Rapid prototyping in research and production workflows
With Keras, the same codebase (with minimal changes) can be used for experimentation, training, and deployment. Rapid prototyping and faster experimentation with high-level APIs of Keras makes it easy to prototype different model architectures.
All of this makes it easy to maintain a readable and maintainable source code. Scaling from a local setup to large clusters comprising GPUs/TPUs can be realized using underlying backend’s distribution strategies like tf.distribute of TensorFlow.
Simplification of the development of deep-learning models, flexibility to deploy ML models across different backends (e.g., PyTorch, Jax, and TensorFlow), and rapid prototyping capabilities makes Keras an ideal framework for training and deploying neural networks.
How to install Keras?
There are two options for installing Keras on the local machine (i.e, macOS, Windows, Linux). Keras can be installed either as a standalone package using pip or by installing TensorFlow (which includes Keras out of the box). Let’s look at each of them in more detail.
Option 1: Install Keras via TensorFlow (Recommended)
A stable way to install Keras is through TensorFlow which is shipped with a compatible Keras version. An important point to note is that TensorFlow only supports the following Python versions:
- Python 3.9
- Python 3.10
- Python 3.11
- Python 3.12
In case you already have Python versions > 3.12 installed on your machine, it is best recommended to keep them intact. You can manage multiple Python versions by installing the pyenv package on a macOS machine.
1. Install pyenv (via Homebrew)
Run the command brew install pyenv on the terminal to install the pyenv package. Run the command pyenv –version to check the version of the installed package.
2. Add pyenv to the shell
Add the following to the .zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
As seen below, the pyenv v2.6.17 is installed on my macOS machine.
3. Install Python 3.12

Run the command pyenv install 3.12.7 on the terminal to install Python v3.12. Post installation, run the command python3.12 –version for checking if the installation was successful (or not)
4. Create and activate a virtual environment
In order to keep the global Python installation clean and isolate project dependencies, create and activate a venv by running the following commands on the terminal:
- ~/.pyenv/versions/3.12.7/bin/python -m venv tf-blog-env
- source tf-blog-env/bin/activate

5. Install TensorFlow
With Python 3.12 all setup, install TensorFlow (which has built-in Keras) on the machine by triggering the following commands on the terminal:
- python -m pip install –upgrade pip setuptools wheel
- pip install tensorflow keras

6. Verify TensorFlow/Teras installation
Now that the Tensor installation is completed, run the following Python code on the terminal:
python - <<EOF
import tensorflow as tf
import keras
print(tf.__version__)
print(keras.__version__)
EOF
Post execution, you would see that Keras v3.13.0 is installed on the machine.

Option 2. Install Keras via pip
Keras can also be installed as a standalone package using pip. You can prefer this approach if you want to use Keras independently (or work with multiple backends apart from TensorFlow).
Since TensorFlow with Keras has the largest ecosystem and the most comprehensive documentation, Approach 1 is generally preferred. On the other hand, installing Keras as a standalone package works on Python v3.12 (and above)
Follow the below mentioned steps to install Keras via pip:
Step 1. Create and activate a virtual environment
Run the following commands on the terminal:
- python -m venv keras-env
- source keras-env/bin/activate
You can also update pip by triggering the command pip install –upgrade pip on the terminal. However, updating pip is not mandatory for installation of Keras.
Step 2. Install Keras via pip
Now that venv is created, run the command pip install keras in the virtual environment to install the Keras framework.

Step 3. Install Keras back-end
The standalone Keras package does not ship with a backend by default. To use the standalone keras package with TensorFlow as backend, install tensorflow as in Option (1) and set KERAS_BACKEND=tensorflow before importing keras in your code.
Alternatively, you can install another supported backend, such as JAX, to verify that the Keras installation was successful. To install JAX, run the following command in the terminal:
- pip install jax jaxlib

Set the environment variable KERAS_BACKEND=jax to set JAX as the default backend for the Keras deep learning framework. This can be done by running export KERAS_BACKEND=jax on the terminal.
Step 4: Verify Keras installation
Now that the backend is installed, run the earlier Python code on the terminal.
python - <<EOF
import keras
print("Keras version:", keras.__version__)
EOF
As seen below, the installation of standalone Keras v.313.0 is successful.

Types Of Keras Models
A Keras model is a high-level abstraction defining how a neural network is structured, trained, and used for inference.
The model defines aspects related to layer connectivity, how data flows through the network, and how learning is performed using an optimizer and a loss function. In a nutshell, a Keras workflow encapsulates the entire lifecycle of a deep learning workflow.
Note: Since TensorFlow is used as the back-end, we used the Install Keras via TensorFlow (Recommended) approach mentioned earlier to install TensorFlow (and Keras deep learning framework).
Here are the three major ways to create Keras model APIs:
Sequential Model
This is a straightforward model that comprises a simple list of layers. The model is limited to single-input and single-output stacks of layers.
The Sequential model is primarily used for development of a model that has a plain stack of layers where each layer has only one input tensor and one output tensor. Shown below is a simple example demonstrating the Sequential model:
FileName – keras-sequential-model.py
import keras
from keras.models import Sequential
from keras.layers import Dense, Input
model = Sequential([
Input(shape=(10,)),
Dense(16, activation="relu"),
Dense(1)
])
print("Keras backend:", keras.backend.backend())
model.compile(optimizer="adam", loss="mean_squared_error")
model.summary()
– After importing the required Keras components (i.e., keras.models, keras.layers, etc.), a linear stack of layers is created. In this case, the model takes a fixed-size input of 10 features and produces a single output

– Next, we configure how the model learns by specifying the optimizer and loss function. The summary() method of the model class displays the model architecture, layer shapes, and the number of parameters
Run the command python keras-sequential-model.py in the terminal to execute the model. The output shown below is the model summary produced by the model.summary() method. It describes the structure of the Keras Sequential model.

The output shows details about the layers used, their output shapes, and the number of parameters in each layer. There are 193 (i.e., 176 + 17) trainable parameters and zero non-trainable parameters present in the model.
Figure 1 – Types of Keras Models [Image Source]
Functional Model
Functional model, also termed as the Keras industry strength model, lets you build complex deep learning models. The Functional API provides an easy-to-use API that supports arbitrary model architectures.
In contrast to the Sequential model, the Functional model supports more than one input and output, thereby providing more flexibility when building models with Keras.
For demonstration, we convert the Sequential model demoed earlier to its Functional equivalent. The example shows how Sequential and Functional APIs can represent the same model.

FileName – keras-functional-model.py
import keras
from keras.layers import Dense, Input
from keras.models import Model
# Define input
inputs = Input(shape=(10,))
# Define layers
x = Dense(16, activation="relu")(inputs)
outputs = Dense(1)(x)
print("Keras backend:", keras.backend.backend())
# Create model
model = Model(inputs=inputs, outputs=outputs)
# Compile and inspect
model.compile(optimizer="adam", loss="mean_squared_error")
model.summary()
– After importing the required Keras components (i.e., keras.models, keras.layers, etc.), model’s input and shape which is required in the Functional API is defined in the code

– Next, the Dense layer is applied to the input tensor. outputs = Dense(1)(x)
creates the output tensor, completing the forward path of the model.

– A full network graph is formed with Model class tying the inputs and outputs together

Run the command python keras-functional-model.py in the terminal to execute the model. The output shows details about the layers used, their output shapes, and the number of parameters in each layer.
Since the overall architecture is the same as the Sequential model, the Functional model also contains 193 trainable parameters (176 + 17) and no non-trainable parameters.

Model SubClassing
Model SubClassing is used for more complex research-oriented use cases. The model’s behavior and forward pass are fully customized by extending the Model class.
Demonstrating Model SubClassing is beyond the scope of the blog and is typically relevant for advanced or research-focused workflows.
In summary, the Keras deep learning framework offers multiple model types (e.g., Sequential, Functional, SubClassing, etc.) to choose from, so that developers can opt for the one that meets their technical and product requirements.
Migrating from Keras 2 to Keras 3
Keras 3.0 was a milestone release since the introduction of the Keras framework. Officially released in November 2023, Keras 3.0 is a full rewrite of Keras and enables developers to create workflows on top of either JAX, TensorFlow, PyTorch, or OpenVINO (for inference-only).
Here are some of the salient benefits of the new multi-back Keras 3 in comparison to its predecessor (i.e., Keras 2):
Get best performance for the models
Unlike Keras 2 which only supported TensorFlow, Keras 3 supports multiple back-ends, including TensorFlow. In the internal benchmarks performed by the Keras team, it was found that the JAX framework outperformed other Keras-supported frameworks as far as training and inference on GPU, TPU, and CPU is concerned.
As stated in the official documentation, non-XLA TensorFlow is also occasionally faster on GPU. Keras 3 makes it easy and seamless to switch backends without any significant changes in the code or any compromise on the model performance. You can train the model and achieve the best performance and maximum efficiency by making a switch to the Keras 3 deep learning framework.
Provides ecosystem flexibility for the models
A Keras 3 model can now be instantiated as a PyTorch module or exported as a TensorFlow SaveModel or instantiated as a stateless JAX function.
This flexibility empowers developers, owing to which they create one model using Keras APIs and deploy it across the broader machine learning ecosystem.
Decoupled Data Pipelines
With Keras 3, developers can reuse existing datasets and input pipelines, without having to rewrite them when switching between backends like TensorFlow, PyTorch, or JAX.
This results in reduced development efforts, faster iterative cycles, and improved interoperability when shifting backend(s) by allowing models to be trained using data sources from different ecosystems. All of this is agnostic of the backend being used with the Keras deep learning model.
Data and Model Parallelism behind a unified API
The new multi-device distribution API in Keras 3 is used for realizing data parallelism and model parallelism in a backend–agnostic way. Though data and model parallelism existed in Keras 2, developers had to rely on TensorFlow-specific APIs.

Figure 2: Data Parallelism and Model Parallelism in Keras 3 [Image Source]
As data and model parallelism are backend-agnostic in Keras 3, a Keras model can be scaled across TensorFlow, PyTorch, or JAX using the respective backend’s native distribution capabilities. All of this can be achieved with minimal changes in the code, with zero rewriting of the model implementation.
Here are the APIs to achieve data parallelism in Keras 3 via which Keras distributes batches of data across multiple devices. To speed up the execution, a copy of the complete model is kept on each device.
| Data Parallelism in Keras 3 | |
|---|---|
| Backend | Data Parallelism Approach/API |
| TensorFlow | tf.distribute |
| PyTorch | torch.distributed |
| JAX | Native sharding and pjit |
Keras 3 is not a mere upgrade from Keras 2 but it is altogether a reimagined, backend agnostic deep learning framework designed to meet the demands of modern Machine Learning workflows.
Follow the below mentioned steps to migrate from Keras 2 to Keras 3. For demonstration purposes, TensorFlow is used as the backend, since it was the default backend in Keras 2.
Note: As seen earlier, TensorFlow is compatible with Python versions v3.9 ~ v3.12. Hence, it is recommended to install the respective Python version so that TensorFlow can work without any issues!
Step 1 – Assess the codebase
Review the Keras 2 codebase and check for any incompatibilities in Keras 3. Specifically, take a closer look at custom layers, callbacks, third-party Keras plugins since they might need some fixes to work with Keras 3
Step 2 – Update the TensorFlow installation
TensorFlow is the default backend in Keras 2. However, TensorFlow is a standalone package in Keras 3.
- Keras 2: from tensorflow import keras
- Keras 3: import keras
Step 3 – Replace TensorFlow-specific imports
Refactor imports that directly reference tf.keras for backend compatibility:
- Replace tf.keras.layers, tf.keras.models, etc.
- Use keras.layers, keras.models, and keras.optimizers
Step 4 – Adjust for multi-backend support
Since Keras 3 supports multiple backends, you might need to update your code in case you have hard-coded the backend setting in Keras 2.
Since TensorFlow is the default backend in Keras 2, developers often:
- Imported TensorFlow ops directly inside model code
- Used tf.keras.backend functions
- Relied on TensorFlow-only utilities or callbacks
With Keras 3, the same model can run on TensorFlow, PyTorch, or JAX. Hence, the code that is hard-coded to TensorFlow APIs can break multi-backend compatibility.
Step 5 – Review the model creation implementation
Keras 3 is considered to have a more simplified model building capability due to ease with which you can seamlessly switch backends. Keras 3 uses a refined functional API, hence you might need to adjust the model code to accommodate the revised input layer structure.
If the Keras 2 code has reliance on TensorFlow specific callbacks, tf.function, etc., you might need to either retain the backend as TensorFlow or refactor to backend-agnostic Keras APIs where possible.
Step 6 – Verify layer names and attributes
Some layer attributes and functions in Keras 2 might have deprecated or undergone changes in Keras 3.
It is recommended to have a look at Keras 3 Layers documentation for updating any layers (or functions) that might have been removed (or renamed). In case the Keras 2 code has implementation for custom layers, ensure that your code conforms to updated standards for Keras 3. You can find more information about custom layers in Keras 3 in the Keras 3 custom layers support documentation.
Step 7 – Verify Data Pipelines
Keras 3 supports tf.data.Dataset, PyTorch DataLoader, NumPy arrays, and Pandas dataframes. Since Keras 3 can be integrated with multiple backends, it is possible that Keras 3 may handle pre-processing logic differently.
Test the pre-processing data pipelines to confirm if they work as expected and update as per the new API requirements.
Step 8 – Fix Typical Errors
Porting errors are common during the migration process. Make sure the shapes of each layer match what Keras 3 expects. Make sure the model code is properly configured in Keras 3 if it relies on a certain backend.
Step 9 – Validate Training and Performance
Multi-backend features are optimized in Keras 3, hence test the model to verify if the performance improves on different backends. Performance tests can provide detailed insights into the backend performs for the specific model and hardware.
Conduct end-to-end tests to verify that features like distributed training, callbacks, checkpoints, and logging operate as intended throughout the chosen backend and that model accuracy is constant.
Once the migration is complete, document the changes and maintain a changelog so that it is helpful for the other members of the team.
Let’s take an example of a Sequential Model (in Keras 2) that uses TensorFlow as the backend.
Keras 2 Sample – Sequential Model
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(16, activation="relu", input_shape=(10,)),
Dense(1)
])
model.compile(optimizer="adam", loss="mean_squared_error")
model.summary()
Keras 3 Port of Keras 2 Sequential Model
import keras
from keras.models import Sequential
from keras.layers import Dense, Input
model = Sequential([
Input(shape=(10,)),
Dense(16, activation="relu"),
Dense(1)
])
print("Keras backend:", keras.backend.backend())
model.compile(optimizer="adam", loss="mean_squared_error")
model.summary()
As seen above, there are minor changes in the model implementation. Let’s break-down the Keras 2 to Keras 3 porting changes:
1. Import Statements
Keras 2
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
Keras 3
import keras
from keras.models import Sequential
from keras.layers import Dense, Input
Difference
- Keras 2 – Keras is bundled with TensorFlow and accessed via tensorflow.keras
- Keras 3 – Keras is a standalone, backend-agnostic library
2. Input Definition
Keras 2
Dense(16, activation="relu", input_shape=(10,)),
Keras 3
Input(shape=(10,)),
Difference
- Keras 2 – Passes input_shape directly to the first layer
- Keras 3 – Recommends defining inputs explicitly using an Input layer
3. Model definition style
Keras 2
model = Sequential([
Dense(16, activation="relu", input_shape=(10,)),
Dense(1)
])
Keras 3
model = Sequential([
Input(shape=(10,)),
Dense(16, activation="relu"),
Dense(1)
])
- Though the overall structure remains the same, Keras 3 separates input specification from layer definitions for supporting multi-backend execution
4. Compilation Step
Keras 2 and Keras 3 (No change)
model.compile (optimizer="adam", loss="mean_squared_error")
model.summary()
- Model compilation works the same in both the Keras versions
In summary, Keras 3 has changes related to imports and input definition style to support multiple backends (apart from TensorFlow). However, the model architecture, training logic and other aspects related to the model and output remain the same as in Keras 2.
Switching Backends in Keras 3
Now that we have covered the porting aspects of the Keras framework, let’s look at how seamless it is to switch backends in the Keras 3 framework. Switching backends does not require any changes in the underlying model implementation.
Since Keras 3 supports three backends, you need to have them installed on the machine before using them in the model code. In case the respective backend is not installed, you can install the same by running the following commands on the terminal:
- TensorFlow – pip install tensorflow
- PyTorch – pip install torch
- JAX – pip install jax jaxlib
Once the required backend is installed, set the backend via the environment variables (recommended approach):
- TensorFlow – export KERAS_BACKEND=tensorflow
- PyTorch – export KERAS_BACKEND=torch
- JAX – export KERAS_BACKEND=jax
Alternatively, you can also set the backend inside the script but this is not a recommended approach.
import os
os.environ["KERAS_BACKEND"] = "torch"
import keras
Now that everything is set up, let’s run the Keras 3 Sequential model demonstrated earlier with the backend set to the PyTorch framework.
Post installing the Torch framework, we set the environment variable KERAS_BACKEND to torch.


Now that the backend is set, we run the model code by invoking the command python keras-sequential-model.py on the terminal.
FileName – keras-sequential-model.py

As seen from the execution snapshot, the model executed successfully with the backend set to the Torch framework.

As shown above, switching backends in Keras 3 requires no code changes, which is a major advantage for developers looking to leverage multiple backends effectively.
Keras Alternatives
Apart from Keras, PyTorch and TensorFlow are the other popular full-stack deep learning frameworks. At the time of writing this blog, the monthly downloads of the PyTorch (or Torch) package outnumbered that of the TensorFlow package.
- Monthly Downloads – PyTorch (or Torch) : 65,223,857
- Monthly Downloads – TensorFlow (or tensorflow) : 22,190,758
Setting the numbers aside, let’s take a closer look at TensorFlow and PyTorch, and see how they stack up against the Keras deep learning framework.
What is TensorFlow
TensorFlow is a widely used end-to-end (or full-stack) deep learning framework that was developed by the Google Bain team for internal usage. The source code of TensorFlow was open-sourced in 2015 under the Apache-2.0 license. The complete source code of the TensorFlow framework is available on GitHub.
TensorFlow has an extensive ecosystem of tools, libraries, and community resources that can be used by developers and AI researchers to build and deploy AI/ML applications.
Salient Features of TensorFlow
At the time of writing this blog, the latest version of TensorFlow is 2.20.0. Here are some of the major features of the TensorFlow deep learning framework:
1. High Performance and Scalability
Apart from training on GPUs and TPUs, TensorFlow is optimized to train and optimize large-scale AI workloads on CPUs.
2. Enhanced Toolong Ecosystem
AI developers can leverage tools in TensorFlow to accelerate TensorFlow workflows. One such tool is TensorBoard which is a suite of visualization tools that help in understanding, debugging, and optimizing AI workflows.
ML Perf is a standardized machine learning benchmarking suite that is used to measure the performance of ML frameworks and ML cloud platforms. TensorFlow Serving, TensorFlow Lite and TensorFlow.js are other useful tools of the TensorFlow ecosystem.
3. Core Libraries & APIs
A vast array of machine learning models and deployment scenarios are supported by TensorFlow’s extensive and comprehensive ecosystem of libraries, tools, and APIs. These libraries and extensions help in building advanced ML models in TensorFlow.
TensorFlow Hub is a repository of pre-trained models that can be fine-tuned and deployed anywhere. Trained models like BERT and Faster R-CNN can be reused with just a few lines of code changes.
TensorFlow Recommenders, TensorFlow Probability, TensorFlow Quantum, TensorFlow Decision Forests and TF-Agents are some of the specialized libraries that are an integral part of the TensorFlow ecosystem. These libraries can support advanced AI workflows and domain-specific ML requirements.
4. Extensive Language and Platform Support
Primary APIs of TensorFlow are available in popular programming languages (i.e., Python, JavaScript, C++ and Java). Over and above, third-party bindings are also available in other languages like R, Scala, C#, Julia and Rust.
Apart from support for macOS, Windows and Linux, TensorFlow is also compatible with popular mobile operating systems such as iOS and Android.
5. Support for Distributed Training
TensorFlow supports distributed training (or parallel computing) with the tf.distribute.Strategy API that distributes training across multiple GPUs, multiple machines, or TPUs.
Developers can use the API to distribute existing models and training code with minimal code changes. This significantly speeds up the model training time. You can find more information in the TensorFlow Distributed Training documentation.
Pros of TensorFlow
Apart from being a popular open-source deep learning framework, here are some of the major advantages of TensorFlow:
1. Keras Compatibility
As seen so far, TensorFlow is the default backend framework in Keras 2 as well as Keras 3. Though Keras 3 is a multi-backend framework, Keras 3 is fully compatible with TensorFlow. Hence, developers can leverage the benefits offered by both Keras and TensorFlow when developing AI/ML applications.
2. Scalability
TensorFlow is highly scalable, as it has the capability to run on a single device as well as on specialized hardware and distributed computing environments. TensorFlow is built to handle massive datasets and complex AI models.
TensorFlow model compression can also be used to make models smaller, faster, and resource efficient, without compromising on its accuracy and performance.
3. Compatibility
Primary TensorFlow APIs and third-party bindings (available in different programming languages) make it easy for developers to develop, debug and deploy ML solutions across diverse platforms and environments.
4. Debugging
TensorFlow also provides tools like TensorBoard that helps with model debugging and performance monitoring during training and evaluation. The visualization aspects of TensorBoard provide the much-needed help in accelerated experimentation with the model code.
Cons of TensorFlow
Here are some of the shortcomings of the TensorFlow framework:
1. Steep Learning Curve
Multiple abstraction levels in TensorFlow APIs can be a bit overwhelming, particularly for beginners and teams focussed on rapid experimentation.
2. More BoilerPlate
TensorFlow requires more setup and configuration, especially for custom training loops, distributed training and low-level control & optimization.
3. Debugging Distributed TensorFlow Workloads
Though TensorBoard is available in TensorFlow for model debugging, visualization, and performance monitoring; debugging complex graphs and distributed TensorFlow workloads can be a challenging task in TensorFlow.
What is PyTorch
PyTorch is an open-source deep learning framework that was developed by the AI research group of Meta (formerly Facebook). Now a part of the Linux foundation, PyTorch was open-sourced in 2016.
The core engine of PyTorch is implemented in C++ and the GPU acceleration is handled via CUDA. Python acts as the high-level language in PyTorch. The entire source code of the PyTorch model is open-sourced on GitHub. At the time of writing this blog, the latest stable version of PyTorch is v2.9.1
PyTorch lets developers and researchers build complex neural networks for applications such as computer vision, NLP, etc. due to its ease of use and flexibility.
Salient Features of PyTorch
Here are some of the major features of the PyTorch deep learning framework:
1. Dynamic Computation Graphs (DCGs)
Also known as define-by-run, DCGs in PyTorch provide a more user-friendly, flexible, and highly efficient approach to development of deep learning models. In PyTorch, a computational graph that represents a sequence of operations on the data, is built and modified as the operations are executed.
Hence, all of this is done in real-time rather than being defined before runtime. Dynamic computation makes it easy to modify the computational graph during runtime, a factor that is crucial for models with dynamic architectures. Since the graph is built dynamically, standard Python debugging tools can be used for debugging the model.
2. Rich Ecosystem of Libraries
PyTorch has a rich ecosystem of libraries in areas such as training, modeling and optimization. TorchVision, TorchText, TorchAudio, and TorchServe are some of the popular libraries of the PyTorch ecosystem.
3. Excellent for Research and Prototyping
PyTorch is one of the preferred frameworks for research and rapid prototyping. Flexibility, ease of use, scalability, and DCGs are some of the primary reasons for PyTorch’s growing adoption in the Academia and industry.
PyTorch hub for researchers provides a large number of pre-trained models that can be used for research exploration.
4. Support for Distributed Training
Like TensorFlow, the PyTorch framework also supports distributed training via pytorch.distributed, a native PyTorch submodule that provides Python APIs for scalable, distributed model training and performance optimization.
Pros of PyTorch
Apart from being a popular open-source deep learning framework, here are some of the major advantages of PyTorch:
1. Simpler to learn
Since the high-level API code of PyTorch is in Python, anyone well-versed with Python can pick up PyTorch real fast!
2. Easy Debugging
As mentioned earlier, the standard Python debugging tools (e.g., pdb, ipdb, etc.) can be used for debugging model code implemented with PyTorch.
3. Intuitive Model Development
As the computational graphs are built dynamically, it provides room for flexible and intuitive model development.
Python-first and highly readable API(s), strong and growing ecosystem of libraries, and strong GPU acceleration with CUDA support are some of the other benefits offered by the PyTorch framework.
Cons of PyTorch
Here are some of the cons of the PyTorch framework:
1. Optional dependency on external web frameworks
Even though PyTorch offers serving solutions like TorchServe, a lot of deployments still rely on third-party web frameworks, which might increase integration costs.
2. No Data Visualization
Unlike TensorFlow that had TensorBoard for visualization and performance monitoring, there is no such built-in (or third-party) tool for PyTorch.
Keras vs Tensorflow vs PyTorch
The choice of the deep learning framework entirely depends on the model requirements, hence there is no one-size-fits-all solution. The answer to “which framework is the best” is going to be subjective, as every framework has its own set of benefits and shortcomings.
Based on the framework walkthrough covered so far, here is a quick comparison of Keras vs TensorFlow and Keras vs PyTorch across the same evaluation criteria:
| Aspect | Keras | TensorFlow | PyTorch |
|---|---|---|---|
| Category | High-level deep learning API | Full deep learning framework | Full deep learning framework |
| API level | High-level only | High-level + low-level APIs | Low-level + high-level APIs |
| Written in | Python | C++, CUDA, Python | C++, CUDA, Python |
| Hardware support | CPU, GPU, TPU (via backend) | CPU, GPU, TPU | CPU, GPU |
| Ease of use | Very easy and beginner-friendly | Moderate | Easy and Pythonic |
| Pretrained models | Yes (via backends) | Yes (TensorFlow Hub, Keras apps) | Yes (TorchVision, TorchHub) |
| Architecture style | Declarative, abstraction-driven | Static graph + eager execution | Dynamic computation graph |
| Readability | Very high | Moderate | High |
| Boilerplate Code | Minimal due to the availability of concise APIs | Moderate to high, as it requires more configuration | Moderable but more readable due to its Pythonic implementation |
| Model Backend | TensorFlow, PyTorch, and JAX (Keras 3) | Native execution engine | Native execution engine |
| Model definition | Declarative (Sequential, Functional) | High-level Keras and low-level APIs | Pythonic |
| Debugging | Simple models, minimal debugging | Improved, but still complex in large graphs | Excellent and intuitive due to Dynamic Computation Graph (DCG) |
| Production readiness | Via backend (TensorFlow / Torch / JAX) | Very strong | Strong and improving |
| Performance | Comparable via backend | High, optimized for scale | High, research and production |
| Research usability | Primarily used for prototyping purposes | Good | Excellent |
| Typical user base | Beginners and hobbyists looking for rapid prototyping an AI model | Enterprises and product teams | AI/ML engineers and researchers |
In summary, the Keras framework keeps things simple and portable. On the other hand, the TensorFlow framework focuses on scalable production systems due to which it is a preferred framework by enterprises and product teams. Lastly, PyTorch owing to its Pythonic implementation is best-suited for prototyping and research.
Its A Wrap
In this blog, we looked at all the critical aspects of the Keras deep learning framework. Apart from Keras, AI/ML developers and researchers can also take a closer look at the TensorFlow and PyTorch frameworks.
Akin to programming languages and web frameworks, each deep learning framework offers unique capabilities that might be beneficial for model development. The choice purely depends on the use case, model requirements, and technical evaluation conducted by the team.
Teams can leverage the potential of cloud and Keras by opting for cloud GPUs for Keras, which further helps them accelerate training and deployment of deep learning workloads.