TensorFlow
Keras
Estimator
Machine Learning
Performance Optimization

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.

Practice ML system design

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

  1. 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.
  2. 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.
  3. 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.
  4. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.