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 low-level operations such as tensor products, convolutions and so on itself. 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 three backend implementations available: the TensorFlow backend, the Theano backend, and the CNTK backend.

  • 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.

In the future, we are likely to add more backend options.

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 replace $HOME with %USERPROFILE%.

The default configuration file looks like this:

  1. {
  2. "image_data_format": "channels_last",
  3. "epsilon": 1e-07,
  4. "floatx": "float32",
  5. "backend": "tensorflow"
  6. }

Simply change the field backend to "theano", "tensorflow", or "cntk", 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 willoverride what is defined in your config file :

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

In Keras it is possible to load more backends than "tensorflow", "theano", and "cntk". Keras can use external backends as well, and this can be performed by changing the keras.json configuration file, and the "backend" setting. Suppose you have a Python module called my_module that you wanted to use as your external backend. The keras.json configuration file would be changed as follows:

  1. {
  2. "image_data_format": "channels_last",
  3. "epsilon": 1e-07,
  4. "floatx": "float32",
  5. "backend": "my_package.my_module"
  6. }

An external backend must be validated in order to be used, a valid backend must have the following functions: placeholder, variable and function.

If an external backend is not valid due to missing a required entry, an error will be logged notifying which entry/entries are missing.

keras.json details

The keras.json configuration file contains the following settings:

  1. {
  2. "image_data_format": "channels_last",
  3. "epsilon": 1e-07,
  4. "floatx": "float32",
  5. "backend": "tensorflow"
  6. }

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

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:

  1. 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.

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

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

  1. import numpy as np
  2. val = np.random.random((3, 4, 5))
  3. var = K.variable(value=val)
  4. # all-zeros variable:
  5. var = K.zeros(shape=(3, 4, 5))
  6. # all-ones:
  7. var = K.ones(shape=(3, 4, 5))

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

  1. # Initializing Tensors with Random Numbers
  2. b = K.random_uniform_variable(shape=(3, 4), low=0, high=1) # Uniform distribution
  3. c = K.random_normal_variable(shape=(3, 4), mean=0, scale=1) # Gaussian distribution
  4. d = K.random_normal_variable(shape=(3, 4), mean=0, scale=1)
  5. # Tensor Arithmetic
  6. a = b + c * K.abs(d)
  7. c = K.dot(a, K.transpose(b))
  8. a = K.sum(b, axis=1)
  9. a = K.softmax(b)
  10. a = K.concatenate([b, c], axis=-1)
  11. # etc...

Backend functions

backend

  1. keras.backend.backend()

Returns the name of the current backend (e.g. "tensorflow").

Returns

String, the name of the backend Keras is currently using.

Example

  1. >>> keras.backend.backend()
  2. 'tensorflow'

symbolic

  1. keras.backend.symbolic(func)

Decorator used in TensorFlow 2.0 to enter the Keras graph.

Arguments

  • func: Function to decorate.

Returns

Decorated function.

eager

  1. keras.backend.eager(func)

Decorator used in TensorFlow 2.0 to exit the Keras graph.

Arguments

  • func: Function to decorate.

Returns

Decorated function.

get_uid

  1. keras.backend.get_uid(prefix='')

Provides a unique UID given a string prefix.

Arguments

  • prefix: string.

Returns

An integer.

Example

  1. >>> keras.backend.get_uid('dense')
  2. 1
  3. >>> keras.backend.get_uid('dense')
  4. 2

manual_variable_initialization

  1. keras.backend.manual_variable_initialization(value)

Sets the manual variable initialization flag.

This boolean flag determines whethervariables should be initializedas they are instantiated (default), or ifthe user should handle the initialization.

Arguments

  • value: Python boolean.

epsilon

  1. keras.backend.epsilon()

Returns the value of the fuzz factor used in numeric expressions.

Returns

A float.

Example

  1. >>> keras.backend.epsilon()
  2. 1e-07

reset_uids

  1. keras.backend.reset_uids()

Resets graph identifiers.

set_epsilon

  1. keras.backend.set_epsilon(e)

Sets the value of the fuzz factor used in numeric expressions.

Arguments

  • e: float. New value of epsilon.

Example

  1. >>> from keras import backend as K
  2. >>> K.epsilon()
  3. 1e-07
  4. >>> K.set_epsilon(1e-05)
  5. >>> K.epsilon()
  6. 1e-05

floatx

  1. keras.backend.floatx()

Returns the default float type, as a string.(e.g. 'float16', 'float32', 'float64').

Returns

String, the current default float type.

Example

  1. >>> keras.backend.floatx()
  2. 'float32'

set_floatx

  1. keras.backend.set_floatx(floatx)

Sets the default float type.

Arguments

  • floatx: String, 'float16', 'float32', or 'float64'.

Example

  1. >>> from keras import backend as K
  2. >>> K.floatx()
  3. 'float32'
  4. >>> K.set_floatx('float16')
  5. >>> K.floatx()
  6. 'float16'

cast_to_floatx

  1. keras.backend.cast_to_floatx(x)

Cast a Numpy array to the default Keras float type.

Arguments

  • x: Numpy array.

Returns

The same Numpy array, cast to its new type.

Example

  1. >>> from keras import backend as K
  2. >>> K.floatx()
  3. 'float32'
  4. >>> arr = numpy.array([1.0, 2.0], dtype='float64')
  5. >>> arr.dtype
  6. dtype('float64')
  7. >>> new_arr = K.cast_to_floatx(arr)
  8. >>> new_arr
  9. array([ 1., 2.], dtype=float32)
  10. >>> new_arr.dtype
  11. dtype('float32')

image_data_format

  1. keras.backend.image_data_format()

Returns the default image data format convention.

Returns

A string, either 'channels_first' or 'channels_last'

Example

  1. >>> keras.backend.image_data_format()
  2. 'channels_first'

set_image_data_format

  1. keras.backend.set_image_data_format(data_format)

Sets the value of the data format convention.

Arguments

  • data_format: string. 'channels_first' or 'channels_last'.

Example

  1. >>> from keras import backend as K
  2. >>> K.image_data_format()
  3. 'channels_first'
  4. >>> K.set_image_data_format('channels_last')
  5. >>> K.image_data_format()
  6. 'channels_last'

learning_phase

  1. keras.backend.learning_phase()

Returns the learning phase flag.

The learning phase flag is a bool tensor (0 = test, 1 = train)to be passed as input to any Keras functionthat uses a different behavior at train time and test time.

Returns

Learning phase (scalar integer tensor or Python integer).

set_learning_phase

  1. keras.backend.set_learning_phase(value)

Sets the learning phase to a fixed value.

Arguments

  • value: Learning phase value, either 0 or 1 (integers).

Raises

  • ValueError: if value is neither 0 nor 1.

clear_session

  1. keras.backend.clear_session()

Destroys the current Keras graph and creates a new one.

Useful to avoid clutter from old models / layers.

is_sparse

  1. keras.backend.is_sparse(tensor)

Returns whether a tensor is a sparse tensor.

Arguments

  • tensor: A tensor instance.

Returns

A boolean.

Example

  1. >>> from keras import backend as K
  2. >>> a = K.placeholder((2, 2), sparse=False)
  3. >>> print(K.is_sparse(a))
  4. False
  5. >>> b = K.placeholder((2, 2), sparse=True)
  6. >>> print(K.is_sparse(b))
  7. True

to_dense

  1. keras.backend.to_dense(tensor)

Converts a sparse tensor into a dense tensor and returns it.

Arguments

  • tensor: A tensor instance (potentially sparse).

Returns

A dense tensor.

Examples

  1. >>> from keras import backend as K
  2. >>> b = K.placeholder((2, 2), sparse=True)
  3. >>> print(K.is_sparse(b))
  4. True
  5. >>> c = K.to_dense(b)
  6. >>> print(K.is_sparse(c))
  7. False

variable

  1. keras.backend.variable(value, dtype=None, name=None, constraint=None)

Instantiates a variable and returns it.

Arguments

  • value: Numpy array, initial value of the tensor.
  • dtype: Tensor type.
  • name: Optional name string for the tensor.
  • constraint: Optional projection function to be applied to the variable after an optimizer update.

Returns

A variable instance (with Keras metadata included).

Examples

  1. >>> from keras import backend as K
  2. >>> val = np.array([[1, 2], [3, 4]])
  3. >>> kvar = K.variable(value=val, dtype='float64', name='example_var')
  4. >>> K.dtype(kvar)
  5. 'float64'
  6. >>> print(kvar)
  7. example_var
  8. >>> K.eval(kvar)
  9. array([[ 1., 2.],
  10. [ 3., 4.]])

is_variable

  1. keras.backend.is_variable(x)

constant

  1. keras.backend.constant(value, dtype=None, shape=None, name=None)

Creates a constant tensor.

Arguments

  • value: A constant value (or list)
  • dtype: The type of the elements of the resulting tensor.
  • shape: Optional dimensions of resulting tensor.
  • name: Optional name for the tensor.

Returns

A Constant Tensor.

is_keras_tensor

  1. keras.backend.is_keras_tensor(x)

Returns whether x is a Keras tensor.

A "Keras tensor" is a tensor that was returned by a Keras layer,(Layer class) or by Input.

Arguments

  • x: A candidate tensor.

Returns

A boolean: Whether the argument is a Keras tensor.

Raises

  • ValueError: In case x is not a symbolic tensor.

Examples

  1. >>> from keras import backend as K
  2. >>> from keras.layers import Input, Dense
  3. >>> np_var = numpy.array([1, 2])
  4. >>> K.is_keras_tensor(np_var) # A numpy array is not a symbolic tensor.
  5. ValueError
  6. >>> k_var = tf.placeholder('float32', shape=(1,1))
  7. >>> # A variable indirectly created outside of keras is not a Keras tensor.
  8. >>> K.is_keras_tensor(k_var)
  9. False
  10. >>> keras_var = K.variable(np_var)
  11. >>> # A variable created with the keras backend is not a Keras tensor.
  12. >>> K.is_keras_tensor(keras_var)
  13. False
  14. >>> keras_placeholder = K.placeholder(shape=(2, 4, 5))
  15. >>> # A placeholder is not a Keras tensor.
  16. >>> K.is_keras_tensor(keras_placeholder)
  17. False
  18. >>> keras_input = Input([10])
  19. >>> K.is_keras_tensor(keras_input) # An Input is a Keras tensor.
  20. True
  21. >>> keras_layer_output = Dense(10)(keras_input)
  22. >>> # Any Keras layer output is a Keras tensor.
  23. >>> K.is_keras_tensor(keras_layer_output)
  24. True

is_tensor

  1. keras.backend.is_tensor(x)

placeholder

  1. keras.backend.placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None)

Instantiates a placeholder tensor and returns it.

Arguments

  • shape: Shape of the placeholder (integer tuple, may include None entries).
  • ndim: Number of axes of the tensor. At least one of {shape, ndim} must be specified. If both are specified, shape is used.
  • dtype: Placeholder type.
  • sparse: Boolean, whether the placeholder should have a sparse type.
  • name: Optional name string for the placeholder.

Returns

Tensor instance (with Keras metadata included).

Examples

  1. >>> from keras import backend as K
  2. >>> input_ph = K.placeholder(shape=(2, 4, 5))
  3. >>> input_ph._keras_shape
  4. (2, 4, 5)
  5. >>> input_ph
  6. <tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>

is_placeholder

  1. keras.backend.is_placeholder(x)

Returns whether x is a placeholder.

Arguments

  • x: A candidate placeholder.

Returns

Boolean.

shape

  1. keras.backend.shape(x)

Returns the symbolic shape of a tensor or variable.

Arguments

  • x: A tensor or variable.

Returns

A symbolic shape (which is itself a tensor).

Examples

  1. # TensorFlow example
  2. >>> from keras import backend as K
  3. >>> tf_session = K.get_session()
  4. >>> val = np.array([[1, 2], [3, 4]])
  5. >>> kvar = K.variable(value=val)
  6. >>> inputs = keras.backend.placeholder(shape=(2, 4, 5))
  7. >>> K.shape(kvar)
  8. <tf.Tensor 'Shape_8:0' shape=(2,) dtype=int32>
  9. >>> K.shape(inputs)
  10. <tf.Tensor 'Shape_9:0' shape=(3,) dtype=int32>
  11. # To get integer shape (Instead, you can use K.int_shape(x))
  12. >>> K.shape(kvar).eval(session=tf_session)
  13. array([2, 2], dtype=int32)
  14. >>> K.shape(inputs).eval(session=tf_session)
  15. array([2, 4, 5], dtype=int32)

int_shape

  1. keras.backend.int_shape(x)

Returns the shape of tensor or variable as a tuple of int or None entries.

Arguments

  • x: Tensor or variable.

Returns

A tuple of integers (or None entries).

Examples

  1. >>> from keras import backend as K
  2. >>> inputs = K.placeholder(shape=(2, 4, 5))
  3. >>> K.int_shape(inputs)
  4. (2, 4, 5)
  5. >>> val = np.array([[1, 2], [3, 4]])
  6. >>> kvar = K.variable(value=val)
  7. >>> K.int_shape(kvar)
  8. (2, 2)

Numpy implementation

  1. def int_shape(x):
  2. return x.shape

ndim

  1. keras.backend.ndim(x)

Returns the number of axes in a tensor, as an integer.

Arguments

  • x: Tensor or variable.

Returns

Integer (scalar), number of axes.

Examples

  1. >>> from keras import backend as K
  2. >>> inputs = K.placeholder(shape=(2, 4, 5))
  3. >>> val = np.array([[1, 2], [3, 4]])
  4. >>> kvar = K.variable(value=val)
  5. >>> K.ndim(inputs)
  6. 3
  7. >>> K.ndim(kvar)
  8. 2

Numpy implementation

  1. def ndim(x):
  2. return x.ndim

size

  1. keras.backend.size(x, name=None)

Returns the size of a tensor.

Arguments

  • x: Tensor or variable.
  • name: A name for the operation (optional).

Returns

Size of the tensor.

Examples

  1. >>> from keras import backend as K
  2. >>> val = np.array([[1, 2], [3, 4]])
  3. >>> kvar = K.variable(value=val)
  4. >>> K.size(inputs)
  5. <tf.Tensor: id=9, shape=(), dtype=int32, numpy=4>

dtype

  1. keras.backend.dtype(x)

Returns the dtype of a Keras tensor or variable, as a string.

Arguments

  • x: Tensor or variable.

Returns

String, dtype of x.

Examples

  1. >>> from keras import backend as K
  2. >>> K.dtype(K.placeholder(shape=(2,4,5)))
  3. 'float32'
  4. >>> K.dtype(K.placeholder(shape=(2,4,5), dtype='float32'))
  5. 'float32'
  6. >>> K.dtype(K.placeholder(shape=(2,4,5), dtype='float64'))
  7. 'float64'
  8. # Keras variable
  9. >>> kvar = K.variable(np.array([[1, 2], [3, 4]]))
  10. >>> K.dtype(kvar)
  11. 'float32_ref'
  12. >>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
  13. >>> K.dtype(kvar)
  14. 'float32_ref'

Numpy implementation

  1. def dtype(x):
  2. return x.dtype.name

eval

  1. keras.backend.eval(x)

Evaluates the value of a tensor.

Arguments

  • x: A tensor.

Returns

A Numpy array.

Examples

  1. >>> from keras import backend as K
  2. >>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
  3. >>> K.eval(kvar)
  4. array([[ 1., 2.],
  5. [ 3., 4.]], dtype=float32)

Numpy implementation

  1. def eval(x):
  2. return x

zeros

  1. keras.backend.zeros(shape, dtype=None, name=None)

Instantiates an all-zeros variable and returns it.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable
  • dtype: String, data type of returned Keras variable
  • name: String, name of returned Keras variable

Returns

A variable (including Keras metadata), filled with 0.0.Note that if shape was symbolic, we cannot return a variable,and will return a dynamically-shaped tensor instead.

Example

  1. >>> from keras import backend as K
  2. >>> kvar = K.zeros((3,4))
  3. >>> K.eval(kvar)
  4. array([[ 0., 0., 0., 0.],
  5. [ 0., 0., 0., 0.],
  6. [ 0., 0., 0., 0.]], dtype=float32)

Numpy implementation

  1. def zeros(shape, dtype=floatx(), name=None):
  2. return np.zeros(shape, dtype=dtype)

ones

  1. keras.backend.ones(shape, dtype=None, name=None)

Instantiates an all-ones variable and returns it.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • dtype: String, data type of returned Keras variable.
  • name: String, name of returned Keras variable.

Returns

A Keras variable, filled with 1.0.Note that if shape was symbolic, we cannot return a variable,and will return a dynamically-shaped tensor instead.

Example

  1. >>> from keras import backend as K
  2. >>> kvar = K.ones((3,4))
  3. >>> K.eval(kvar)
  4. array([[ 1., 1., 1., 1.],
  5. [ 1., 1., 1., 1.],
  6. [ 1., 1., 1., 1.]], dtype=float32)

Numpy implementation

  1. def ones(shape, dtype=floatx(), name=None):
  2. return np.ones(shape, dtype=dtype)

eye

  1. keras.backend.eye(size, dtype=None, name=None)

Instantiate an identity matrix and returns it.

Arguments

  • size: Tuple, number of rows and columns. If Integer, number of rows.
  • dtype: String, data type of returned Keras variable.
  • name: String, name of returned Keras variable.

Returns

A Keras variable, an identity matrix.

Example

  1. >>> from keras import backend as K
  2. >>> K.eval(K.eye(3))
  3. array([[ 1., 0., 0.],
  4. [ 0., 1., 0.],
  5. [ 0., 0., 1.]], dtype=float32)
  6. >>> K.eval(K.eye((2, 3)))
  7. array([[1., 0., 0.],
  8. [0., 1., 0.]], dtype=float32)

Numpy implementation

  1. def eye(size, dtype=None, name=None):
  2. if isinstance(size, (list, tuple)):
  3. n, m = size
  4. else:
  5. n, m = size, size
  6. return np.eye(n, m, dtype=dtype)

zeros_like

  1. keras.backend.zeros_like(x, dtype=None, name=None)

Instantiates an all-zeros variable of the same shape as another tensor.

Arguments

  • x: Keras variable or Keras tensor.
  • dtype: String, dtype of returned Keras variable. None uses the dtype of x.
  • name: String, name for the variable to create.

Returns

A Keras variable with the shape of x filled with zeros.

Example

  1. >>> from keras import backend as K
  2. >>> kvar = K.variable(np.random.random((2,3)))
  3. >>> kvar_zeros = K.zeros_like(kvar)
  4. >>> K.eval(kvar_zeros)
  5. array([[ 0., 0., 0.],
  6. [ 0., 0., 0.]], dtype=float32)

Numpy implementation

  1. def zeros_like(x, dtype=floatx(), name=None):
  2. return np.zeros_like(x, dtype=dtype)

ones_like

  1. keras.backend.ones_like(x, dtype=None, name=None)

Instantiates an all-ones variable of the same shape as another tensor.

Arguments

  • x: Keras variable or tensor.
  • dtype: String, dtype of returned Keras variable. None uses the dtype of x.
  • name: String, name for the variable to create.

Returns

A Keras variable with the shape of x filled with ones.

Example

  1. >>> from keras import backend as K
  2. >>> kvar = K.variable(np.random.random((2,3)))
  3. >>> kvar_ones = K.ones_like(kvar)
  4. >>> K.eval(kvar_ones)
  5. array([[ 1., 1., 1.],
  6. [ 1., 1., 1.]], dtype=float32)

Numpy implementation

  1. def ones_like(x, dtype=floatx(), name=None):
  2. return np.ones_like(x, dtype=dtype)

identity

  1. keras.backend.identity(x, name=None)

Returns a tensor with the same content as the input tensor.

Arguments

  • x: The input tensor.
  • name: String, name for the variable to create.

Returns

A tensor of the same shape, type and content.

random_uniform_variable

  1. keras.backend.random_uniform_variable(shape, low, high, dtype=None, name=None, seed=None)

Instantiates a variable with values drawn from a uniform distribution.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • low: Float, lower boundary of the output interval.
  • high: Float, upper boundary of the output interval.
  • dtype: String, dtype of returned Keras variable.
  • name: String, name of returned Keras variable.
  • seed: Integer, random seed.

Returns

A Keras variable, filled with drawn samples.

Example

  1. # TensorFlow example
  2. >>> kvar = K.random_uniform_variable((2,3), 0, 1)
  3. >>> kvar
  4. <tensorflow.python.ops.variables.Variable object at 0x10ab40b10>
  5. >>> K.eval(kvar)
  6. array([[ 0.10940075, 0.10047495, 0.476143 ],
  7. [ 0.66137183, 0.00869417, 0.89220798]], dtype=float32)

Numpy implementation

  1. def random_uniform_variable(shape, low, high, dtype=None, name=None, seed=None):
  2. return (high - low) * np.random.random(shape).astype(dtype) + low

random_normal_variable

  1. keras.backend.random_normal_variable(shape, mean, scale, dtype=None, name=None, seed=None)

Instantiates a variable with values drawn from a normal distribution.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • mean: Float, mean of the normal distribution.
  • scale: Float, standard deviation of the normal distribution.
  • dtype: String, dtype of returned Keras variable.
  • name: String, name of returned Keras variable.
  • seed: Integer, random seed.

Returns

A Keras variable, filled with drawn samples.

Example

  1. # TensorFlow example
  2. >>> kvar = K.random_normal_variable((2,3), 0, 1)
  3. >>> kvar
  4. <tensorflow.python.ops.variables.Variable object at 0x10ab12dd0>
  5. >>> K.eval(kvar)
  6. array([[ 1.19591331, 0.68685907, -0.63814116],
  7. [ 0.92629528, 0.28055015, 1.70484698]], dtype=float32)

Numpy implementation

  1. def random_normal_variable(shape, mean, scale, dtype=None, name=None, seed=None):
  2. return scale * np.random.randn(*shape).astype(dtype) + mean

count_params

  1. keras.backend.count_params(x)

Returns the static number of elements in a Keras variable or tensor.

Arguments

  • x: Keras variable or tensor.

Returns

Integer, the number of elements in x, i.e., the product of thearray's static dimensions.

Example

  1. >>> kvar = K.zeros((2,3))
  2. >>> K.count_params(kvar)
  3. 6
  4. >>> K.eval(kvar)
  5. array([[ 0., 0., 0.],
  6. [ 0., 0., 0.]], dtype=float32)

Numpy implementation

  1. def count_params(x):
  2. return x.size

cast

  1. keras.backend.cast(x, dtype)

Casts a tensor to a different dtype and returns it.

You can cast a Keras variable but it still returns a Keras tensor.

Arguments

  • x: Keras tensor (or variable).
  • dtype: String, either ('float16', 'float32', or 'float64').

Returns

Keras tensor with dtype dtype.

Example

  1. >>> from keras import backend as K
  2. >>> input = K.placeholder((2, 3), dtype='float32')
  3. >>> input
  4. <tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
  5. # It doesn't work in-place as below.
  6. >>> K.cast(input, dtype='float16')
  7. <tf.Tensor 'Cast_1:0' shape=(2, 3) dtype=float16>
  8. >>> input
  9. <tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
  10. # you need to assign it.
  11. >>> input = K.cast(input, dtype='float16')
  12. >>> input
  13. <tf.Tensor 'Cast_2:0' shape=(2, 3) dtype=float16>

update

  1. keras.backend.update(x, new_x)

Update the value of x to new_x.

Arguments

  • x: A Variable.
  • new_x: A tensor of same shape as x.

Returns

The variable x updated.

update_add

  1. keras.backend.update_add(x, increment)

Update the value of x by adding increment.

Arguments

  • x: A Variable.
  • increment: A tensor of same shape as x.

Returns

The variable x updated.

update_sub

  1. keras.backend.update_sub(x, decrement)

Update the value of x by subtracting decrement.

Arguments

  • x: A Variable.
  • decrement: A tensor of same shape as x.

Returns

The variable x updated.

moving_average_update

  1. keras.backend.moving_average_update(x, value, momentum)

Compute the moving average of a variable.

Arguments

  • x: A Variable.
  • value: A tensor with the same shape as x.
  • momentum: The moving average momentum.

Returns

An operation to update the variable.

dot

  1. keras.backend.dot(x, y)

Multiplies 2 tensors (and/or variables) and returns a tensor.

When attempting to multiply a nD tensorwith a nD tensor, it reproduces the Theano behavior.(e.g. (2, 3) * (4, 3, 5) -> (2, 4, 5))

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor, dot product of x and y.

Examples

  1. # dot product between tensors
  2. >>> x = K.placeholder(shape=(2, 3))
  3. >>> y = K.placeholder(shape=(3, 4))
  4. >>> xy = K.dot(x, y)
  5. >>> xy
  6. <tf.Tensor 'MatMul_9:0' shape=(2, 4) dtype=float32>
  1. # dot product between tensors
  2. >>> x = K.placeholder(shape=(32, 28, 3))
  3. >>> y = K.placeholder(shape=(3, 4))
  4. >>> xy = K.dot(x, y)
  5. >>> xy
  6. <tf.Tensor 'MatMul_9:0' shape=(32, 28, 4) dtype=float32>
  1. # Theano-like behavior example
  2. >>> x = K.random_uniform_variable(shape=(2, 3), low=0, high=1)
  3. >>> y = K.ones((4, 3, 5))
  4. >>> xy = K.dot(x, y)
  5. >>> K.int_shape(xy)
  6. (2, 4, 5)

Numpy implementation

  1. def dot(x, y):
  2. return np.dot(x, y)

batch_dot

  1. keras.backend.batch_dot(x, y, axes=None)

Batchwise dot product.

batch_dot is used to compute dot product of x and y whenx and y are data in batches, i.e. in a shape of(batch_size, :).batch_dot results in a tensor or variable with less dimensionsthan the input. If the number of dimensions is reduced to 1,we use expand_dims to make sure that ndim is at least 2.

Arguments

  • x: Keras tensor or variable with ndim >= 2.
  • y: Keras tensor or variable with ndim >= 2.
  • axes: int or tuple(int, int). Target dimensions to be reduced.

Returns

A tensor with shape equal to the concatenation of x's shape(less the dimension that was summed over) and y's shape(less the batch dimension and the dimension that was summed over).If the final rank is 1, we reshape it to (batch_size, 1).

Examples

Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]]batch_dot(x, y, axes=1) = [[17], [53]] which is the main diagonalof x.dot(y.T), although we never have to calculate the off-diagonalelements.

Pseudocode:

  1. inner_products = []
  2. for xi, yi in zip(x, y):
  3. inner_products.append(xi.dot(yi))
  4. result = stack(inner_products)

Shape inference:Let x's shape be (100, 20) and y's shape be (100, 30, 20).If axes is (1, 2), to find the output shape of resultant tensor,loop through each dimension in x's shape and y's shape:

  • x.shape[0] : 100 : append to output shape
  • x.shape[1] : 20 : do not append to output shape,dimension 1 of x has been summed over. (dot_axes[0] = 1)
  • y.shape[0] : 100 : do not append to output shape,always ignore first dimension of y
  • y.shape[1] : 30 : append to output shape
  • y.shape[2] : 20 : do not append to output shape,dimension 2 of y has been summed over. (dot_axes[1] = 2)output_shape = (100, 30)
  1. >>> x_batch = K.ones(shape=(32, 20, 1))
  2. >>> y_batch = K.ones(shape=(32, 30, 20))
  3. >>> xy_batch_dot = K.batch_dot(x_batch, y_batch, axes=(1, 2))
  4. >>> K.int_shape(xy_batch_dot)
  5. (32, 1, 30)

Numpy implementation

Show the Numpy implementationwzxhzdk:102

transpose

  1. keras.backend.transpose(x)

Transposes a tensor and returns it.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

Examples

  1. >>> var = K.variable([[1, 2, 3], [4, 5, 6]])
  2. >>> K.eval(var)
  3. array([[ 1., 2., 3.],
  4. [ 4., 5., 6.]], dtype=float32)
  5. >>> var_transposed = K.transpose(var)
  6. >>> K.eval(var_transposed)
  7. array([[ 1., 4.],
  8. [ 2., 5.],
  9. [ 3., 6.]], dtype=float32)
  1. >>> inputs = K.placeholder((2, 3))
  2. >>> inputs
  3. <tf.Tensor 'Placeholder_11:0' shape=(2, 3) dtype=float32>
  4. >>> input_transposed = K.transpose(inputs)
  5. >>> input_transposed
  6. <tf.Tensor 'transpose_4:0' shape=(3, 2) dtype=float32>

Numpy implementation

  1. def transpose(x):
  2. return np.transpose(x)

gather

  1. keras.backend.gather(reference, indices)

Retrieves the elements of indices indices in the tensor reference.

Arguments

  • reference: A tensor.
  • indices: An integer tensor of indices.

Returns

A tensor of same type as reference.

Numpy implementation

  1. def gather(reference, indices):
  2. return reference[indices]

max

  1. keras.backend.max(x, axis=None, keepdims=False)

Maximum value in a tensor.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to find maximum values. If None (default), finds the maximum over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with maximum values of x.

Numpy implementation

  1. def max(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.max(x, axis=axis, keepdims=keepdims)

min

  1. keras.backend.min(x, axis=None, keepdims=False)

Minimum value in a tensor.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to find minimum values. If None (default), finds the minimum over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with miminum values of x.

Numpy implementation

  1. def min(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.min(x, axis=axis, keepdims=keepdims)

sum

  1. keras.backend.sum(x, axis=None, keepdims=False)

Sum of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to sum over. If None (default), sums over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with sum of x.

Numpy implementation

  1. def sum(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.sum(x, axis=axis, keepdims=keepdims)

prod

  1. keras.backend.prod(x, axis=None, keepdims=False)

Multiplies the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the product. If None (default), computes the product over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the product of elements of x.

Numpy implementation

  1. def prod(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.prod(x, axis=axis, keepdims=keepdims)

cumsum

  1. keras.backend.cumsum(x, axis=0)

Cumulative sum of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer, the axis to compute the sum.

Returns

A tensor of the cumulative sum of values of x along axis.Numpy implementation

  1. def cumsum(x, axis=0):
  2. return np.cumsum(x, axis=axis)

cumprod

  1. keras.backend.cumprod(x, axis=0)

Cumulative product of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer, the axis to compute the product.

Returns

A tensor of the cumulative product of values of x along axis.Numpy implementation

  1. def cumprod(x, axis=0):
  2. return np.cumprod(x, axis=axis)

var

  1. keras.backend.var(x, axis=None, keepdims=False)

Variance of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the variance. If None (default), computes the variance over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the variance of elements of x.Numpy implementation

  1. def var(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.var(x, axis=axis, keepdims=keepdims)

std

  1. keras.backend.std(x, axis=None, keepdims=False)

Standard deviation of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the standard deviation. If None (default), computes the standard deviation over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the standard deviation of elements of x.Numpy implementation

  1. def std(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.std(x, axis=axis, keepdims=keepdims)

mean

  1. keras.backend.mean(x, axis=None, keepdims=False)

Mean of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the mean. If None (default), computes the mean over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1 for each entry in axis. If keepdims is True, the reduced dimensions are retained with length 1.

Returns

A tensor with the mean of elements of x.Numpy implementation

  1. def mean(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.mean(x, axis=axis, keepdims=keepdims)

any

  1. keras.backend.any(x, axis=None, keepdims=False)

Bitwise reduction (logical OR).

Arguments

  • x: Tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logical or. If None (default), computes the logical or over all dimensions.
  • keepdims: whether the drop or broadcast the reduction axes.

Returns

A uint8 tensor (0s and 1s).Numpy implementation

  1. def any(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.any(x, axis=axis, keepdims=keepdims)

all

  1. keras.backend.all(x, axis=None, keepdims=False)

Bitwise reduction (logical AND).

Arguments

  • x: Tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logical and. If None (default), computes the logical and over all dimensions.
  • keepdims: whether the drop or broadcast the reduction axes.

Returns

A uint8 tensor (0s and 1s).Numpy implementation

  1. def all(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return np.all(x, axis=axis, keepdims=keepdims)

argmax

  1. keras.backend.argmax(x, axis=-1)

Returns the index of the maximum value along an axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform the reduction.

Returns

A tensor.Numpy implementation

  1. def argmax(x, axis=-1):
  2. return np.argmax(x, axis=axis)

argmin

  1. keras.backend.argmin(x, axis=-1)

Returns the index of the minimum value along an axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform the reduction.

Returns

A tensor.Numpy implementation

  1. def argmin(x, axis=-1):
  2. return np.argmin(x, axis=axis)

square

  1. keras.backend.square(x)

Element-wise square.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

abs

  1. keras.backend.abs(x)

Element-wise absolute value.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

sqrt

  1. keras.backend.sqrt(x)

Element-wise square root.

Arguments

  • x: Tensor or variable.

Returns

A tensor.Numpy implementation

  1. def sqrt(x):
  2. y = np.sqrt(x)
  3. y[np.isnan(y)] = 0.
  4. return y

exp

  1. keras.backend.exp(x)

Element-wise exponential.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

log

  1. keras.backend.log(x)

Element-wise log.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

logsumexp

  1. keras.backend.logsumexp(x, axis=None, keepdims=False)

Computes log(sum(exp(elements across dimensions of a tensor))).

This function is more numerically stable than log(sum(exp(x))).It avoids overflows caused by taking the exp of large inputs andunderflows caused by taking the log of small inputs.

Arguments

  • x: A tensor or variable.
  • axis: axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logsumexp. If None (default), computes the logsumexp over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

The reduced tensor.Numpy implementation

  1. def logsumexp(x, axis=None, keepdims=False):
  2. if isinstance(axis, list):
  3. axis = tuple(axis)
  4. return sp.special.logsumexp(x, axis=axis, keepdims=keepdims)

round

  1. keras.backend.round(x)

Element-wise rounding to the closest integer.

In case of tie, the rounding mode used is "half to even".

Arguments

  • x: Tensor or variable.

Returns

A tensor.

sign

  1. keras.backend.sign(x)

Element-wise sign.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

pow

  1. keras.backend.pow(x, a)

Element-wise exponentiation.

Arguments

  • x: Tensor or variable.
  • a: Python integer.

Returns

A tensor.Numpy implementation

  1. def pow(x, a=1.):
  2. return np.power(x, a)

clip

  1. keras.backend.clip(x, min_value, max_value)

Element-wise value clipping.

Arguments

  • x: Tensor or variable.
  • min_value: Python float, integer or tensor.
  • max_value: Python float, integer or tensor.

Returns

A tensor.Numpy implementation

  1. def clip(x, min_value, max_value):
  2. return np.clip(x, min_value, max_value)

equal

  1. keras.backend.equal(x, y)

Element-wise equality between two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def equal(x, y):
  2. return x == y

not_equal

  1. keras.backend.not_equal(x, y)

Element-wise inequality between two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def not_equal(x, y):
  2. return x != y

greater

  1. keras.backend.greater(x, y)

Element-wise truth value of (x > y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def greater(x, y):
  2. return x > y

greater_equal

  1. keras.backend.greater_equal(x, y)

Element-wise truth value of (x >= y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def greater_equal(x, y):
  2. return x >= y

less

  1. keras.backend.less(x, y)

Element-wise truth value of (x < y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def less(x, y):
  2. return x < y

less_equal

  1. keras.backend.less_equal(x, y)

Element-wise truth value of (x <= y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

  1. def less_equal(x, y):
  2. return x <= y

maximum

  1. keras.backend.maximum(x, y)

Element-wise maximum of two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def maximum(x, y):
  2. return np.maximum(x, y)

minimum

  1. keras.backend.minimum(x, y)

Element-wise minimum of two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def minimum(x, y):
  2. return np.minimum(x, y)

sin

  1. keras.backend.sin(x)

Computes sin of x element-wise.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

cos

  1. keras.backend.cos(x)

Computes cos of x element-wise.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

normalize_batch_in_training

  1. keras.backend.normalize_batch_in_training(x, gamma, beta, reduction_axes, epsilon=0.001)

Computes mean and std for batch then apply batch_normalization on batch.

Arguments

  • x: Input tensor or variable.
  • gamma: Tensor by which to scale the input.
  • beta: Tensor with which to center the input.
  • reduction_axes: iterable of integers, axes over which to normalize.
  • epsilon: Fuzz factor.

Returns

A tuple length of 3, (normalized_tensor, mean, variance).

batch_normalization

  1. keras.backend.batch_normalization(x, mean, var, beta, gamma, axis=-1, epsilon=0.001)

Applies batch normalization on x given mean, var, beta and gamma.

I.e. returns:output = (x - mean) / sqrt(var + epsilon) * gamma + beta

Arguments

  • x: Input tensor or variable.
  • mean: Mean of batch.
  • var: Variance of batch.
  • beta: Tensor with which to center the input.
  • gamma: Tensor by which to scale the input.
  • axis: Integer, the axis that should be normalized. (typically the features axis).
  • epsilon: Fuzz factor.

Returns

A tensor.

Numpy implementation

  1. def batch_normalization(x, mean, var, beta, gamma, axis=-1, epsilon=0.001):
  2. return ((x - mean) / sqrt(var + epsilon)) * gamma + beta

concatenate

  1. keras.backend.concatenate(tensors, axis=-1)

Concatenates a list of tensors alongside the specified axis.

Arguments

  • tensors: list of tensors to concatenate.
  • axis: concatenation axis.

Returns

A tensor.

reshape

  1. keras.backend.reshape(x, shape)

Reshapes a tensor to the specified shape.

Arguments

  • x: Tensor or variable.
  • shape: Target shape tuple.

Returns

A tensor.

permute_dimensions

  1. keras.backend.permute_dimensions(x, pattern)

Permutes axes in a tensor.

Arguments

  • x: Tensor or variable.
  • pattern: A tuple of dimension indices, e.g. (0, 2, 1).

Returns

A tensor.

resize_images

  1. keras.backend.resize_images(x, height_factor, width_factor, data_format, interpolation='nearest')

Resizes the images contained in a 4D tensor.

Arguments

  • x: Tensor or variable to resize.
  • height_factor: Positive integer.
  • width_factor: Positive integer.
  • data_format: string, "channels_last" or "channels_first".
  • interpolation: A string, one of nearest or bilinear.

Returns

A tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

resize_volumes

  1. keras.backend.resize_volumes(x, depth_factor, height_factor, width_factor, data_format)

Resizes the volume contained in a 5D tensor.

Arguments

  • x: Tensor or variable to resize.
  • depth_factor: Positive integer.
  • height_factor: Positive integer.
  • width_factor: Positive integer.
  • data_format: string, "channels_last" or "channels_first".

Returns

A tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

repeat_elements

  1. keras.backend.repeat_elements(x, rep, axis)

Repeats the elements of a tensor along an axis, like np.repeat.

If x has shape (s1, s2, s3) and axis is 1, the outputwill have shape (s1, s2 * rep, s3).

Arguments

  • x: Tensor or variable.
  • rep: Python integer, number of times to repeat.
  • axis: Axis along which to repeat.

Returns

A tensor.

repeat

  1. keras.backend.repeat(x, n)

Repeats a 2D tensor.

if x has shape (samples, dim) and n is 2,the output will have shape (samples, 2, dim).

Arguments

  • x: Tensor or variable.
  • n: Python integer, number of times to repeat.

Returns

A tensor.

arange

  1. keras.backend.arange(start, stop=None, step=1, dtype='int32')

Creates a 1D tensor containing a sequence of integers.

The function arguments use the same convention asTheano's arange: if only one argument is provided,it is in fact the "stop" argument and "start" is 0.

The default type of the returned tensor is 'int32' tomatch TensorFlow's default.

Arguments

  • start: Start value.
  • stop: Stop value.
  • step: Difference between two successive values.
  • dtype: Integer dtype to use.

Returns

An integer tensor.

tile

  1. keras.backend.tile(x, n)

Creates a tensor by tiling x by n.

Arguments

  • x: A tensor or variable
  • n: A list of integer. The length must be the same as the number of dimensions in x.

Returns

A tiled tensor.

Example

  1. >>> from keras import backend as K
  2. >>> kvar = K.variable(np.random.random((2, 3)))
  3. >>> kvar_tile = K.tile(K.eye(2), (2, 3))
  4. >>> K.eval(kvar_tile)
  5. array([[1., 0., 1., 0., 1., 0.],
  6. [0., 1., 0., 1., 0., 1.],
  7. [1., 0., 1., 0., 1., 0.],
  8. [0., 1., 0., 1., 0., 1.]], dtype=float32)

Numpy implementation

  1. def tile(x, n):
  2. return np.tile(x, n)

flatten

  1. keras.backend.flatten(x)

Flatten a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor, reshaped into 1-D

batch_flatten

  1. keras.backend.batch_flatten(x)

Turn a nD tensor into a 2D tensor with same 0th dimension.

In other words, it flattens each data samples of a batch.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

expand_dims

  1. keras.backend.expand_dims(x, axis=-1)

Adds a 1-sized dimension at index "axis".

Arguments

  • x: A tensor or variable.
  • axis: Position where to add a new axis.

Returns

A tensor with expanded dimensions.

squeeze

  1. keras.backend.squeeze(x, axis)

Removes a 1-dimension from the tensor at index "axis".

Arguments

  • x: A tensor or variable.
  • axis: Axis to drop.

Returns

A tensor with the same data as x but reduced dimensions.

temporal_padding

  1. keras.backend.temporal_padding(x, padding=(1, 1))

Pads the middle dimension of a 3D tensor.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 2 integers, how many zeros to add at the start and end of dim 1.

Returns

A padded 3D tensor.

spatial_2d_padding

  1. keras.backend.spatial_2d_padding(x, padding=((1, 1), (1, 1)), data_format=None)

Pads the 2nd and 3rd dimensions of a 4D tensor.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 2 tuples, padding pattern.
  • data_format: string, "channels_last" or "channels_first".

Returns

A padded 4D tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

spatial_3d_padding

  1. keras.backend.spatial_3d_padding(x, padding=((1, 1), (1, 1), (1, 1)), data_format=None)

Pads 5D tensor with zeros along the depth, height, width dimensions.

Pads these dimensions with respectively"padding[0]", "padding[1]" and "padding[2]" zeros left and right.

For 'channels_last' data_format,the 2nd, 3rd and 4th dimension will be padded.For 'channels_first' data_format,the 3rd, 4th and 5th dimension will be padded.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 3 tuples, padding pattern.
  • data_format: string, "channels_last" or "channels_first".

Returns

A padded 5D tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

stack

  1. keras.backend.stack(x, axis=0)

Stacks a list of rank R tensors into a rank R+1 tensor.

Arguments

  • x: List of tensors.
  • axis: Axis along which to perform stacking.

Returns

A tensor.

Numpy implementation

  1. def stack(x, axis=0):
  2. return np.stack(x, axis=axis)

one_hot

  1. keras.backend.one_hot(indices, num_classes)

Computes the one-hot representation of an integer tensor.

Arguments

  • indices: nD integer tensor of shape (batch_size, dim1, dim2, … dim(n-1))
  • num_classes: Integer, number of classes to consider.

Returns

(n + 1)D one hot representation of the inputwith shape (batch_size, dim1, dim2, … dim(n-1), num_classes)

reverse

  1. keras.backend.reverse(x, axes)

Reverses a tensor along the specified axes.

Arguments

  • x: Tensor to reverse.
  • axes: Integer or iterable of integers. Axes to reverse.

Returns

A tensor.

Numpy implementation

  1. def reverse(x, axes):
  2. if isinstance(axes, list):
  3. axes = tuple(axes)
  4. return np.flip(x, axes)

slice

  1. keras.backend.slice(x, start, size)

Extracts a slice from a tensor.

Arguments

  • x: Input tensor.
  • start: Integer list/tuple or tensor indicating the start indices of the slice along each axis.
  • size: Integer list/tuple or tensor indicating how many dimensions to slice along each axis.

Returns

A sliced tensor:

  1. new_x = x[start[0]: start[0] + size[0], ..., start[-1]: start[-1] + size[-1]]

Raises

  • ValueError: if the dimension and the size of indices mismatches.

Numpy implementation

  1. def slice(x, start, size):
  2. slices = [py_slice(i, i + j) for i, j in zip(start, size)]
  3. return x[tuple(slices)]

get_value

  1. keras.backend.get_value(x)

Returns the value of a variable.

Arguments

  • x: input variable.

Returns

A Numpy array.

batch_get_value

  1. keras.backend.batch_get_value(ops)

Returns the value of more than one tensor variable.

Arguments

  • ops: list of ops to run.

Returns

A list of Numpy arrays.

set_value

  1. keras.backend.set_value(x, value)

Sets the value of a variable, from a Numpy array.

Arguments

  • x: Variable to set to a new value.
  • value: Value to set the tensor to, as a Numpy array (of the same shape).

batch_set_value

  1. keras.backend.batch_set_value(tuples)

Sets the values of many tensor variables at once.

Arguments

  • tuples: a list of tuples (tensor, value). value should be a Numpy array.

print_tensor

  1. keras.backend.print_tensor(x, message='')

Prints message and the tensor value when evaluated.

Note that print_tensor returns a new tensor identical to xwhich should be used in the following code. Otherwise theprint operation is not taken into account during evaluation.

Example

  1. >>> x = K.print_tensor(x, message="x is: ")

Arguments

  • x: Tensor to print.
  • message: Message to print jointly with the tensor.

Returns

The same tensor x, unchanged.

function

  1. keras.backend.function(inputs, outputs, updates=None)

gradients

  1. keras.backend.gradients(loss, variables)

Returns the gradients of loss w.r.t. variables.

Arguments

  • loss: Scalar tensor to minimize.
  • variables: List of variables.

Returns

A gradients tensor.

stop_gradient

  1. keras.backend.stop_gradient(variables)

Returns variables but with zero gradient w.r.t. every other variable.

Arguments

  • variables: tensor or list of tensors to consider constant with respect to any other variable.

Returns

A single tensor or a list of tensors (depending on the passed argument) that has constant gradient with respect to any other variable.

rnn

  1. keras.backend.rnn(step_function, inputs, initial_states, go_backwards=False, mask=None, constants=None, unroll=False, input_length=None)

Iterates over the time dimension of a tensor.

Arguments

  • step_function: Parameters: inputs: Tensor with shape (samples, …) (no time dimension), representing input for the batch of samples at a certain time step. states: List of tensors. Returns: outputs: Tensor with shape (samples, …) (no time dimension), new_states: List of tensors, same length and shapes as 'states'.
  • inputs: Tensor of temporal data of shape (samples, time, …) (at least 3D).
  • initial_states: Tensor with shape (samples, …) (no time dimension), containing the initial values for the states used in the step function.
  • go_backwards: Boolean. If True, do the iteration over the time dimension in reverse order and return the reversed sequence.
  • mask: Binary tensor with shape (samples, time), with a zero for every element that is masked.
  • constants: A list of constant values passed at each step.
  • unroll: Whether to unroll the RNN or to use a symbolic loop (while_loop or scan depending on backend).
  • input_length: Static number of timesteps in the input.

Returns

A tuple, (last_output, outputs, new_states).

last_output: The latest output of the rnn, of shape (samples, …)outputs: Tensor with shape (samples, time, …) where eachentry outputs[s, t] is the output of the step functionat time t for sample s.new_states: List of tensors, latest states returned bythe step function, of shape (samples, …).

Raises

  • ValueError: If input dimension is less than 3.
  • ValueError: If unroll is True but input timestep is not a fixed number.
  • ValueError: If mask is provided (not None) but states is not provided (len(states) == 0).

Numpy implementation

Show the Numpy implementationwzxhzdk:206

switch

  1. keras.backend.switch(condition, then_expression, else_expression)

Switches between two operations depending on a scalar value.

Note that both thenexpression and else_expressionshould be symbolic tensors of the _same shape.

Arguments

  • condition: tensor (int or bool).
  • then_expression: either a tensor, or a callable that returns a tensor.
  • else_expression: either a tensor, or a callable that returns a tensor.

Returns

The selected tensor.

Raises

  • ValueError: If rank of condition is greater than rank of expressions.

Numpy implementation

  1. def switch(condition, then_expression, else_expression):
  2. cond_float = condition.astype(floatx())
  3. while cond_float.ndim < then_expression.ndim:
  4. cond_float = cond_float[..., np.newaxis]
  5. return cond_float * then_expression + (1 - cond_float) * else_expression

in_train_phase

  1. keras.backend.in_train_phase(x, alt, training=None)

Selects x in train phase, and alt otherwise.

Note that alt should have the same shape as x.

Arguments

  • x: What to return in train phase (tensor or callable that returns a tensor).
  • alt: What to return otherwise (tensor or callable that returns a tensor).
  • training: Optional scalar tensor (or Python boolean, or Python integer) specifying the learning phase.

Returns

Either x or alt based on the training flag.the training flag defaults to K.learning_phase().

in_test_phase

  1. keras.backend.in_test_phase(x, alt, training=None)

Selects x in test phase, and alt otherwise.

Note that alt should have the same shape as x.

Arguments

  • x: What to return in test phase (tensor or callable that returns a tensor).
  • alt: What to return otherwise (tensor or callable that returns a tensor).
  • training: Optional scalar tensor (or Python boolean, or Python integer) specifying the learning phase.

Returns

Either x or alt based on K.learning_phase.

relu

  1. keras.backend.relu(x, alpha=0.0, max_value=None, threshold=0.0)

Rectified linear unit.

With default values, it returns element-wise max(x, 0).

Otherwise, it follows:f(x) = max_value for x >= max_value,f(x) = x for threshold <= x < max_value,f(x) = alpha * (x - threshold) otherwise.

Arguments

  • x: A tensor or variable.
  • alpha: A scalar, slope of negative section (default=0.).
  • max_value: float. Saturation threshold.
  • threshold: float. Threshold value for thresholded activation.

Returns

A tensor.

Numpy implementation

  1. def relu(x, alpha=0., max_value=None, threshold=0.):
  2. if max_value is None:
  3. max_value = np.inf
  4. above_threshold = x * (x >= threshold)
  5. above_threshold = np.clip(above_threshold, 0.0, max_value)
  6. below_threshold = alpha * (x - threshold) * (x < threshold)
  7. return below_threshold + above_threshold

elu

  1. keras.backend.elu(x, alpha=1.0)

Exponential linear unit.

Arguments

  • x: A tensor or variable to compute the activation function for.
  • alpha: A scalar, slope of negative section.

Returns

A tensor.

Numpy implementation

  1. def elu(x, alpha=1.):
  2. return x * (x > 0) + alpha * (np.exp(x) - 1.) * (x < 0)

softmax

  1. keras.backend.softmax(x, axis=-1)

Softmax of a tensor.

Arguments

  • x: A tensor or variable.
  • axis: The dimension softmax would be performed on. The default is -1 which indicates the last dimension.

Returns

A tensor.

Numpy implementation

  1. def softmax(x, axis=-1):
  2. y = np.exp(x - np.max(x, axis, keepdims=True))
  3. return y / np.sum(y, axis, keepdims=True)

softplus

  1. keras.backend.softplus(x)

Softplus of a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def softplus(x):
  2. return np.log(1. + np.exp(x))

softsign

  1. keras.backend.softsign(x)

Softsign of a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def softsign(x):
  2. return x / (1 + np.abs(x))

categorical_crossentropy

  1. keras.backend.categorical_crossentropy(target, output, from_logits=False, axis=-1)

Categorical crossentropy between an output tensor and a target tensor.

Arguments

  • target: A tensor of the same shape as output.
  • output: A tensor resulting from a softmax (unless from_logits is True, in which case output is expected to be the logits).
  • from_logits: Boolean, whether output is the result of a softmax, or is a tensor of logits.
  • axis: Int specifying the channels axis. axis=-1 corresponds to data format channels_last, and axis=1 corresponds to data format channels_first.

Returns

Output tensor.

Raises

  • ValueError: if axis is neither -1 nor one of the axes of output.

sparse_categorical_crossentropy

  1. keras.backend.sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1)

Categorical crossentropy with integer targets.

Arguments

  • target: An integer tensor.
  • output: A tensor resulting from a softmax (unless from_logits is True, in which case output is expected to be the logits).
  • from_logits: Boolean, whether output is the result of a softmax, or is a tensor of logits.
  • axis: Int specifying the channels axis. axis=-1 corresponds to data format channels_last, and axis=1 corresponds to data format channels_first.

Returns

Output tensor.

Raises

  • ValueError: if axis is neither -1 nor one of the axes of output.

binary_crossentropy

  1. keras.backend.binary_crossentropy(target, output, from_logits=False)

Binary crossentropy between an output tensor and a target tensor.

Arguments

  • target: A tensor with the same shape as output.
  • output: A tensor.
  • from_logits: Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution.

Returns

A tensor.

sigmoid

  1. keras.backend.sigmoid(x)

Element-wise sigmoid.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def sigmoid(x):
  2. return 1. / (1. + np.exp(-x))

hard_sigmoid

  1. keras.backend.hard_sigmoid(x)

Segment-wise linear approximation of sigmoid.

Faster than sigmoid.Returns 0. if x < -2.5, 1. if x > 2.5.In -2.5 <= x <= 2.5, returns 0.2 * x + 0.5.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def hard_sigmoid(x):
  2. y = 0.2 * x + 0.5
  3. return np.clip(y, 0, 1)

tanh

  1. keras.backend.tanh(x)

Element-wise tanh.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

  1. def tanh(x):
  2. return np.tanh(x)

dropout

  1. keras.backend.dropout(x, level, noise_shape=None, seed=None)

Sets entries in x to zero at random, while scaling the entire tensor.

Arguments

  • x: tensor
  • level: fraction of the entries in the tensor that will be set to 0.
  • noise_shape: shape for randomly generated keep/drop flags, must be broadcastable to the shape of x
  • seed: random seed to ensure determinism.

Returns

A tensor.Numpy implementation

Show the Numpy implementationwzxhzdk:231

l2_normalize

  1. keras.backend.l2_normalize(x, axis=None)

Normalizes a tensor wrt the L2 norm alongside the specified axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform normalization.

Returns

A tensor.

Numpy implementation

  1. def l2_normalize(x, axis=-1):
  2. y = np.max(np.sum(x ** 2, axis, keepdims=True), axis, keepdims=True)
  3. return x / np.sqrt(y)

in_top_k

  1. keras.backend.in_top_k(predictions, targets, k)

Returns whether the targets are in the top k predictions.

Arguments

  • predictions: A tensor of shape (batch_size, classes) and type float32.
  • targets: A 1D tensor of length batch_size and type int32 or int64.
  • k: An int, number of top elements to consider.

Returns

A 1D tensor of length batch_size and type bool.output[i] is True if predictions[i, targets[i]] is within top-kvalues of predictions[i].

conv1d

  1. keras.backend.conv1d(x, kernel, strides=1, padding='valid', data_format=None, dilation_rate=1)

1D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: stride integer.
  • padding: string, "same", "causal" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: integer dilate rate.

Returns

A tensor, result of 1D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv2d

  1. keras.backend.conv2d(x, kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 2 integers.

Returns

A tensor, result of 2D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv2d_transpose

  1. keras.backend.conv2d_transpose(x, kernel, output_shape, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D deconvolution (i.e. transposed convolution).

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • output_shape: 1D int tensor for the output shape.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 2 integers.

Returns

A tensor, result of transposed 2D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

separable_conv1d

  1. keras.backend.separable_conv1d(x, depthwise_kernel, pointwise_kernel, strides=1, padding='valid', data_format=None, dilation_rate=1)

1D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • pointwise_kernel: kernel for the 1x1 convolution.
  • strides: stride integer.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: integer dilation rate.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

separable_conv2d

  1. keras.backend.separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • pointwise_kernel: kernel for the 1x1 convolution.
  • strides: strides tuple (length 2).
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: tuple of integers, dilation rates for the separable convolution.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

depthwise_conv2d

  1. keras.backend.depthwise_conv2d(x, depthwise_kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • strides: strides tuple (length 2).
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: tuple of integers, dilation rates for the separable convolution.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv3d

  1. keras.backend.conv3d(x, kernel, strides=(1, 1, 1), padding='valid', data_format=None, dilation_rate=(1, 1, 1))

3D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 3 integers.

Returns

A tensor, result of 3D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv3d_transpose

  1. keras.backend.conv3d_transpose(x, kernel, output_shape, strides=(1, 1, 1), padding='valid', data_format=None)

3D deconvolution (i.e. transposed convolution).

Arguments

  • x: input tensor.
  • kernel: kernel tensor.
  • output_shape: 1D int tensor for the output shape.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.

Returns

A tensor, result of transposed 3D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

pool2d

  1. keras.backend.pool2d(x, pool_size, strides=(1, 1), padding='valid', data_format=None, pool_mode='max')

2D Pooling.

Arguments

  • x: Tensor or variable.
  • pool_size: tuple of 2 integers.
  • strides: tuple of 2 integers.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • pool_mode: string, "max" or "avg".

Returns

A tensor, result of 2D pooling.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

  • ValueError: if pool_mode is neither "max" or "avg".

pool3d

  1. keras.backend.pool3d(x, pool_size, strides=(1, 1, 1), padding='valid', data_format=None, pool_mode='max')

3D Pooling.

Arguments

  • x: Tensor or variable.
  • pool_size: tuple of 3 integers.
  • strides: tuple of 3 integers.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • pool_mode: string, "max" or "avg".

Returns

A tensor, result of 3D pooling.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

  • ValueError: if pool_mode is neither "max" or "avg".

local_conv1d

  1. keras.backend.local_conv1d(inputs, kernel, kernel_size, strides, data_format=None)

Apply 1D conv with un-shared weights.

Arguments

  • inputs: 3D tensor with shape: (batch_size, steps, input_dim)
  • kernel: the unshared weight for convolution, with shape (output_length, feature_dim, filters)
  • kernel_size: a tuple of a single integer, specifying the length of the 1D convolution window
  • strides: a tuple of a single integer, specifying the stride length of the convolution
  • data_format: the data format, channels_first or channels_last

Returns

the tensor after 1d conv with un-shared weights,with shape (batch_size, output_length, filters)

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

local_conv2d

  1. keras.backend.local_conv2d(inputs, kernel, kernel_size, strides, output_shape, data_format=None)

Apply 2D conv with un-shared weights.

Arguments

  • inputs: 4D tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'.
  • kernel: the unshared weight for convolution, with shape (output_items, feature_dim, filters)
  • kernel_size: a tuple of 2 integers, specifying the width and height of the 2D convolution window.
  • strides: a tuple of 2 integers, specifying the strides of the convolution along the width and height.
  • output_shape: a tuple with (output_row, output_col)
  • data_format: the data format, channels_first or channels_last

Returns

A 4d tensor with shape:(batch_size, filters, new_rows, new_cols)if data_format='channels_first'or 4D tensor with shape:(batch_size, new_rows, new_cols, filters)if data_format='channels_last'.

Raises

  • ValueError: if data_format is neither channels_last or channels_first.

bias_add

  1. keras.backend.bias_add(x, bias, data_format=None)

Adds a bias vector to a tensor.

Arguments

  • x: Tensor or variable.
  • bias: Bias tensor to add.
  • data_format: string, "channels_last" or "channels_first".

Returns

Output tensor.

Raises

ValueError: In one of the two cases below:1. invalid data_format argument.2. invalid bias shape.the bias should be either a vector ora tensor with ndim(x) - 1 dimensionNumpy implementation

Show the Numpy implementationwzxhzdk:248

random_normal

  1. keras.backend.random_normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None)

Returns a tensor with normal distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • mean: A float, mean of the normal distribution to draw samples.
  • stddev: A float, standard deviation of the normal distribution to draw samples.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.

random_uniform

  1. keras.backend.random_uniform(shape, minval=0.0, maxval=1.0, dtype=None, seed=None)

Returns a tensor with uniform distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • minval: A float, lower boundary of the uniform distribution to draw samples.
  • maxval: A float, upper boundary of the uniform distribution to draw samples.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.

random_binomial

  1. keras.backend.random_binomial(shape, p=0.0, dtype=None, seed=None)

Returns a tensor with random binomial distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • p: A float, 0. <= p <= 1, probability of binomial distribution.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.

truncated_normal

  1. keras.backend.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None)

Returns a tensor with truncated random normal distribution of values.

The generated values follow a normal distributionwith specified mean and standard deviation,except that values whose magnitude is more thantwo standard deviations from the mean are dropped and re-picked.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • mean: Mean of the values.
  • stddev: Standard deviation of the values.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.

ctc_label_dense_to_sparse

  1. keras.backend.ctc_label_dense_to_sparse(labels, label_lengths)

Converts CTC labels from dense to sparse.

Arguments

  • labels: dense CTC labels.
  • label_lengths: length of the labels.

Returns

A sparse tensor representation of the labels.

ctc_batch_cost

  1. keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)

Runs CTC loss algorithm on each batch element.

Arguments

  • y_true: tensor (samples, max_string_length) containing the truth labels.
  • y_pred: tensor (samples, time_steps, num_categories) containing the prediction, or output of the softmax.
  • input_length: tensor (samples, 1) containing the sequence length for each batch item in y_pred.
  • label_length: tensor (samples, 1) containing the sequence length for each batch item in y_true.

Returns

Tensor with shape (samples,1) containing the CTC loss of each element.

ctc_decode

  1. keras.backend.ctc_decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1, merge_repeated=False)

Decodes the output of a softmax.

Can use either greedy search (also known as best path)or a constrained dictionary search.

Arguments

  • y_pred: tensor (samples, time_steps, num_categories) containing the prediction, or output of the softmax.
  • input_length: tensor (samples, ) containing the sequence length for each batch item in y_pred.
  • greedy: perform much faster best-path search if True. This does not use a dictionary.
  • beam_width: if greedy is False: a beam search decoder will be used with a beam of this width.
  • top_paths: if greedy is False, how many of the most probable paths will be returned.
  • merge_repeated: if greedy is False, merge repeated classes in the output beams.

Returns

  • Tuple: List: if greedy is True, returns a list of one element that contains the decoded sequence. If False, returns the top_paths most probable decoded sequences. Important: blank labels are returned as -1. Tensor (top_paths, ) that contains the log probability of each decoded sequence.

control_dependencies

  1. keras.backend.control_dependencies(control_inputs)

A context manager that specifies control dependencies.

Arguments

  • control_inputs: A list of Operation or Tensor objects which must be executed or computed before running the operations defined in the context. Can also be None to clear the control dependencies.

Returns

A context manager.

map_fn

  1. keras.backend.map_fn(fn, elems, name=None, dtype=None)

Map the function fn over the elements elems and return the outputs.

Arguments

  • fn: Callable that will be called upon each element in elems
  • elems: tensor
  • name: A string name for the map node in the graph
  • dtype: Output data type.

Returns

Tensor with dtype dtype.

foldl

  1. keras.backend.foldl(fn, elems, initializer=None, name=None)

Reduce elems using fn to combine them from left to right.

Arguments

  • fn: Callable that will be called upon each element in elems and an accumulator, for instance lambda acc, x: acc + x
  • elems: tensor
  • initializer: The first value used (elems[0] in case of None)
  • name: A string name for the foldl node in the graph

Returns

Tensor with same type and shape as initializer.

foldr

  1. keras.backend.foldr(fn, elems, initializer=None, name=None)

Reduce elems using fn to combine them from right to left.

Arguments

  • fn: Callable that will be called upon each element in elems and an accumulator, for instance lambda acc, x: acc + x
  • elems: tensor
  • initializer: The first value used (elems[-1] in case of None)
  • name: A string name for the foldr node in the graph

Returns

Tensor with same type and shape as initializer.