Keras Backend

Overview

Keras is a model-level library, providing high-level building blocks for developing deep learning models. It does not handle itself low-level operations such as tensor products, convolutions and so on. Instead, it relies on a specialized, well-optimized tensor manipulation library to do so, serving as the “backend engine” of Keras.

The R interface to Keras uses TensorFlow™ as it’s default tensor backend engine, however it’s possible to use other backends if desired. At this time, Keras has three backend implementations available:

TensorFlow is an open-source symbolic tensor manipulation framework developed by Google.

Theano is an open-source symbolic tensor manipulation framework developed by LISA Lab at Université de Montréal.

CNTK is an open-source toolkit for deep learning developed by Microsoft.

Selecting a Backend

Keras uses the TensorFlow backend by default. If you want to switch to Theano or CNTK call the use_backend() function just after your call to library(keras). For example:

Selecting an Implementation

Keras specifies an API that can be implemented by multiple providers. By default, the Keras R package uses the implementation provided by the Keras Python package (“keras”). TensorFlow also provides an integrated implementation of Keras which you can use by specifying “tensorflow” in a call to the use_implementation() function. For example:

Using the Backend

If you want the Keras modules you write to be compatible with all available backends, you have to write them via the abstract Keras backend API. Backend API functions have a k_ prefix (e.g. k_placeholder, k_constant, k_dot, etc.).

For example, the code below instantiates an input placeholder. It’s equivalent to tf$placeholder():