## 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:

If you want to use the CNTK backend then you should follow the installation instructions for CNTK and then speicfy “cntk” in your call to use_backend():

## 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:

You would typically specify the “tensorflow” implementation when using Keras with the tfestimators package, as this implementation allows you to use Keras models seamlessly as TensorFlow Estimators.

### Keras Configuration File

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

~/.keras/keras.json

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

The default configuration file looks like this:

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. (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", "theano", or "cntk".

## 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(): The code below instantiates a variable. It’s equivalent to tf$Variable():

## Backend Functions

 Element-wise absolute value. Bitwise reduction (logical AND). Bitwise reduction (logical OR). Creates a 1D tensor containing a sequence of integers. Returns the index of the maximum value along an axis. Returns the index of the minimum value along an axis. Active Keras backend Batchwise dot product. Turn a nD tensor into a 2D tensor with same 1st dimension. Returns the value of more than one tensor variable. Applies batch normalization on x given mean, var, beta and gamma. Sets the values of many tensor variables at once. Adds a bias vector to a tensor. Binary crossentropy between an output tensor and a target tensor. Cast an array to the default Keras float type. Casts a tensor to a different dtype and returns it. Categorical crossentropy between an output tensor and a target tensor. Destroys the current TF graph and creates a new one. Element-wise value clipping. Concatenates a list of tensors alongside the specified axis. Creates a constant tensor. 1D convolution. 2D deconvolution (i.e. transposed convolution). 2D convolution. 3D deconvolution (i.e. transposed convolution). 3D convolution. Computes cos of x element-wise. Returns the static number of elements in a Keras variable or tensor. Runs CTC loss algorithm on each batch element. Decodes the output of a softmax. Converts CTC labels from dense to sparse. Cumulative product of the values in a tensor, alongside the specified axis. Cumulative sum of the values in a tensor, alongside the specified axis. 2D convolution with separable filters. Multiplies 2 tensors (and/or variables) and returns a tensor. Sets entries in x to zero at random, while scaling the entire tensor. Returns the dtype of a Keras tensor or variable, as a string. Exponential linear unit. Fuzz factor used in numeric expressions. Element-wise equality between two tensors. Evaluates the value of a variable. Element-wise exponential. Adds a 1-sized dimension at index “axis”. Instantiate an identity matrix and returns it. Flatten a tensor. Default float type Reduce elems using fn to combine them from left to right. Reduce elems using fn to combine them from right to left. Instantiates a Keras function Retrieves the elements of indices indices in the tensor reference. TF session to be used by the backend. Get the uid for the default graph. Returns the value of a variable. Returns the shape of a variable. Returns the gradients of variables w.r.t. loss. Element-wise truth value of (x >= y). Element-wise truth value of (x > y). Segment-wise linear approximation of sigmoid. Returns a tensor with the same content as the input tensor. Default image data format convention (‘channels_first’ or ‘channels_last’). Selects x in test phase, and alt otherwise. Returns whether the targets are in the top k predictions. Selects x in train phase, and alt otherwise. Returns the shape of tensor or variable as a list of int or NULL entries. Returns whether x is a symbolic tensor. Returns whether x is a Keras tensor. Returns whether x is a placeholder. Returns whether a tensor is a sparse tensor. Normalizes a tensor wrt the L2 norm alongside the specified axis. Returns the learning phase flag. Element-wise truth value of (x <= y). Element-wise truth value of (x < y). Apply 1D conv with un-shared weights. Apply 2D conv with un-shared weights. Element-wise log. Computes log(sum(exp(elements across dimensions of a tensor))). Sets the manual variable initialization flag. Map the function fn over the elements elems and return the outputs. Maximum value in a tensor. Element-wise maximum of two tensors. Mean of a tensor, alongside the specified axis. Minimum value in a tensor. Element-wise minimum of two tensors. Compute the moving average of a variable. Returns the number of axes in a tensor, as an integer. Computes mean and std for batch then apply batch_normalization on batch. Element-wise inequality between two tensors. Computes the one-hot representation of an integer tensor. Instantiates an all-ones variable of the same shape as another tensor. Instantiates an all-ones tensor variable and returns it. Permutes axes in a tensor. Instantiates a placeholder tensor and returns it. 2D Pooling. 3D Pooling. Element-wise exponentiation. Prints message and the tensor value when evaluated. Multiplies the values in a tensor, alongside the specified axis. Returns a tensor with random binomial distribution of values. Instantiates a variable with values drawn from a normal distribution. Returns a tensor with normal distribution of values. Instantiates a variable with values drawn from a uniform distribution. Returns a tensor with uniform distribution of values. Rectified linear unit. Repeats the elements of a tensor along an axis. Repeats a 2D tensor. Reset graph identifiers. Reshapes a tensor to the specified shape. Resizes the images contained in a 4D tensor. Resizes the volume contained in a 5D tensor. Reverse a tensor along the specified axes. Iterates over the time dimension of a tensor Element-wise rounding to the closest integer. 2D convolution with separable filters. Sets the learning phase to a fixed value. Sets the value of a variable, from an R array. Returns the symbolic shape of a tensor or variable. Element-wise sigmoid. Element-wise sign. Computes sin of x element-wise. Softmax of a tensor. Softplus of a tensor. Softsign of a tensor. Categorical crossentropy with integer targets. Pads the 2nd and 3rd dimensions of a 4D tensor. Pads 5D tensor with zeros along the depth, height, width dimensions. Element-wise square root. Element-wise square. Removes a 1-dimension from the tensor at index “axis”. Stacks a list of rank R tensors into a rank R+1 tensor. Standard deviation of a tensor, alongside the specified axis. Returns variables but with zero gradient w.r.t. every other variable. Sum of the values in a tensor, alongside the specified axis. Switches between two operations depending on a scalar value. Element-wise tanh. Pads the middle dimension of a 3D tensor. Creates a tensor by tiling x by n. Converts a sparse tensor into a dense tensor and returns it. Transposes a tensor and returns it. Returns a tensor with truncated random normal distribution of values. Update the value of x by adding increment. Update the value of x by subtracting decrement. Update the value of x to new_x. Variance of a tensor, alongside the specified axis. Instantiates a variable and returns it. Instantiates an all-zeros variable of the same shape as another tensor. Instantiates an all-zeros variable and returns it.