keras/docs/templates/backend.md
Valerio Maggio 0703d79606 Improvements in the Documentation of Backend (#5767)
* Fix and improvements to the `backend` documentation

Improved Preamble of the `backend.md` template:
- fixed a typo
- Added few notes that makes the documentation more self explanatory
- Made all code examples running by Copy&Paste

Aligned the format of the  `backend()` function

Fixed docstring of `set_image_dim_ordering()` function

* Fixed a Typo in %USERPROFILE% env name for Window Users

* Added `_variable` so not to get a different value every time
2017-03-15 12:40:42 -07:00

4.3 KiB

Keras backends

What is a "backend"?

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. Rather than picking one single tensor library and making the implementation of Keras tied to that library, Keras handles the problem in a modular way, and several different backend engines can be plugged seamlessly into Keras.

At this time, Keras has two backend implementations available: the TensorFlow backend and the Theano backend.

  • TensorFlow is an open-source symbolic tensor manipulation framework developed by Google, Inc.
  • Theano is an open-source symbolic tensor manipulation framework developed by LISA/MILA Lab at Université de Montréal.

In the future, we are likely to add more backend options. Go ask Microsoft about how their CNTK backend project is doing.


Switching from one backend to another

If you have run Keras at least once, you will find the Keras configuration file at:

$HOME/.keras/keras.json

If it isn't there, you can create it.

NOTE for Windows Users: Please change $HOME with %USERPROFILE%.

The default configuration file looks like this:

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

Simply change the field backend to either "theano" or "tensorflow", and Keras will use the new configuration next time you run any Keras code.

You can also define the environment variable KERAS_BACKEND and this will override what is defined in your config file :

KERAS_BACKEND=tensorflow python -c "from keras import backend"
Using TensorFlow backend.

keras.json details

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

You can change these settings by editing $HOME/.keras/keras.json.

  • image_data_format: string, either "channels_last" or "channels_first". It specifies which data format convention Keras will follow. (keras.backend.image_data_format() returns it.)
    • For 2D data (e.g. image), "channels_last" assumes (rows, cols, channels) while "channels_first" assumes (channels, rows, cols).
    • For 3D data, "channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels) while "channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).
  • epsilon: float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
  • floatx: string, "float16", "float32", or "float64". Default float precision.
  • backend: string, "tensorflow" or "theano".

Using the abstract Keras backend to write new code

If you want the Keras modules you write to be compatible with both Theano (th) and TensorFlow (tf), you have to write them via the abstract Keras backend API. Here's an intro.

You can import the backend module via:

*from keras import backend as K*

The code below instantiates an input placeholder. It's equivalent to tf.placeholder() or th.tensor.matrix(), th.tensor.tensor3(), etc.

input = K.placeholder(shape=(2, 4, 5))
# also works:
input = K.placeholder(shape=(None, 4, 5))
# also works:
input = K.placeholder(ndim=3)

The code below instantiates a shared variable. It's equivalent to tf.Variable() or th.shared().

import numpy as np
val = np.random.random((3, 4, 5))
var = K.variable(value=val)

# all-zeros variable:
var = K.zeros(shape=(3, 4, 5))
# all-ones:
var = K.ones(shape=(3, 4, 5))

Most tensor operations you will need can be done as you would in TensorFlow or Theano:

# Initializing Tensors with Random Numbers
b = K.random_uniform_variable(shape=(3, 4)). # Uniform distribution
c = K.random_normal_variable(shape=(3, 4)). # Gaussian distribution
d = K.random_normal_variable(shape=(3, 4)).
# Tensor Arithmetics
a = b + c * K.abs(d)
c = K.dot(a, K.transpose(b))
a = K.sum(b, axis=1)
a = K.softmax(b)
a = K.concatenate([b, c], axis=-1)
# etc...

Backend functions

{{autogenerated}}