Skip to content

Keras - Backend Configuration

Understanding the relationship between Keras and its underlying computation engine is helpful, although direct backend configuration is less common now than in the past.

Originally, Keras was designed as a backend-agnostic API. This meant you could write Keras code and choose to run it on different deep learning frameworks like TensorFlow, Theano, or Microsoft Cognitive Toolkit (CNTK). Users could switch the backend via a configuration file (~/.keras/keras.json).

# Example of the OLD keras.json structure (for historical context ONLY)
# {
# "image_data_format": "channels_last",
# "epsilon": 1e-07,
# "floatx": "float32",
# "backend": "tensorflow" # Could be changed to "theano" or "cntk"
# }

This flexibility was a key advantage in the early days of deep learning when the framework landscape was rapidly evolving.

Current Standard: tf.keras and TensorFlow Integration

Section titled “Current Standard: tf.keras and TensorFlow Integration”

With the release of TensorFlow 2.x, Keras became the official high-level API for TensorFlow. The implementation used today is tf.keras, which is tightly integrated with TensorFlow and uses TensorFlow as its only backend.

Key Implications:

  • No Backend Switching: You no longer switch backends via keras.json. When you use tf.keras, you are inherently using the TensorFlow backend.
  • Seamless Ecosystem: tf.keras models integrate directly with the entire TensorFlow ecosystem, including tf.data for efficient input pipelines, TensorFlow Extended (TFX) for production ML pipelines, TensorFlow Lite for mobile/embedded deployment, and TensorFlow Serving for scalable model deployment.
  • Performance: Leveraging TensorFlow’s optimized operations, graph execution (with tf.function), and hardware acceleration (GPU/TPU) is straightforward.
  • Configuration: Settings like floating-point precision (floatx) or image data format (image_data_format) are typically handled by TensorFlow’s global settings or inferred, rather than a separate Keras config file. The default image_data_format is usually ‘channels_last’ ((batch, height, width, channels)), common for TensorFlow.

While you don’t configure the backend itself, you can check default settings managed by the Keras backend module (which interfaces with TensorFlow):

import tensorflow as tf
from tensorflow.keras import backend as K
# Check the default float type
print("Default float type:", K.floatx()) # Usually 'float32'
# Check the default image data format
print("Image data format:", K.image_data_format()) # Usually 'channels_last'
# Check the default epsilon value (for numerical stability)
print("Epsilon:", K.epsilon()) # Usually 1e-07

Changing these defaults globally (e.g., K.set_floatx('float64')) is possible but generally recommended only if you have a specific reason and understand the implications (e.g., performance, memory usage).

The tf.keras.backend module still exists and provides low-level tensor operations (like K.dot, K.sum, K.relu). These functions internally call the corresponding TensorFlow operations. While you can use them, it’s often more idiomatic in TensorFlow 2.x to use the direct TensorFlow API (tf.*) for custom computations within layers or models, especially when using model subclassing or custom layers.

In summary, the concept of backend configuration in Keras has evolved. With tf.keras, you are working within the TensorFlow ecosystem, eliminating the need to manage separate backend choices and providing deep integration with TensorFlow’s powerful features.