Tensorflow 2.0 Keras is training 4x slower than 2.0 Estimator
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
With the evolution of machine learning libraries, TensorFlow has been at the forefront, offering robust tools for building and training models. TensorFlow 2.0 brought significant changes with a stronger emphasis on Keras as its high-level API, making model building more intuitive. However, some users have reported that models built using TensorFlow 2.0's Keras API are training up to 4 times slower compared to the Estimator API. In this article, we'll dive into this discrepancy, understanding the underlying reasons and exploring solutions.
Keras vs. Estimator: Overview
Keras is a high-level API that simplifies the development process for deep learning models. It's recognized for its easy-to-use interface, flexibility, and ability to work seamlessly on both CPUs and GPUs. TensorFlow's Estimator API, on the other hand, was designed for large-scale, distributed training and offers robustness and scalability, making it a staple for production-ready systems.
Reasons for Slower Training Using Keras
- Graph Execution vs. Eager Execution:
- Keras in TensorFlow 2.0 runs in eager execution mode by default. This is great for debugging as operations are evaluated immediately, but it introduces overheads that can slow down computation.
- Estimators, however, leverage graph execution where the computation graph is compiled and optimized prior to execution, leading to faster training times as TensorFlow can optimize the computation paths better.
- Model Compilation Overheads:
- The Estimator API abstracts many of the configuration and compilation steps, optimizing them for faster execution.
- Conversely, Keras provides more granular control over training configurations, which can introduce compilation overheads that slow down the process.
- Distributed Training:
- Estimators were designed with distributed training in mind, resulting in them having highly optimized pipelines for data input and model training on distributed systems.
- Keras requires additional configurations for distributed training, potentially adding latency and data handling overheads.
- Thread Management:
- Estimators manage concurrency and threading internally, which aids in maximizing resource utilization.
- With Keras, thread management is less transparent, potentially leading to inefficient resource usage on complex models.
Examples of Keras vs. Estimator Performance
Let's go through a hypothetical example to illustrate these points. Consider training a simple convolutional neural network (CNN) on the CIFAR-10 dataset using both Keras and Estimator.
- Users can switch Keras models to function a call with `@tf.function` to force graph execution which can improve performance.
- Proper tuning of batch sizes, optimizers, and learning rates can mitigate some performance discrepancies.
- Ensure that training fully leverages available TPU/GPU resources.
- Use TensorFlow's `tf.data` API to build efficient input pipelines to reduce data loading times.
Related reading
- Tensorflow 2.0 Optimizer.minimize 'Adam' object has no attribute 'minimize
- TensorFlow 2.0 tf.random.set_seed not working since I am getting different results
- TensorFlow 2.1.0 _FallbackException This function does not handle the case of the path where all inputs are not already EagerTensors
- Tensorflow 2.1.0 Error, module ''tensorflow'' has no attribute ''GraphKeys''
- Tensorflow 2.2.0 error Predictions must be 0 Condition x y did not hold element-wise while using Bidirectional LSTM layer
- Tensorflow 2.4.1 - Couldn't invoke ptxas.exe
- Tensorflow Allocation Memory Allocation of 38535168 exceeds 10 of system memory
- Tensorflow AVX Support

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.