Keras 2 API documentation / Utilities / Backend utilities

Backend utilities

[source]

clear_session function

tf_keras.backend.clear_session()

Resets all state generated by TF-Keras.

TF-Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names.

If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.

Example 1: calling clear_session() when creating models in a loop

for _ in range(100):
  # Without `clear_session()`, each iteration of this loop will
  # slightly increase the size of the global state managed by Keras
  model = tf.keras.Sequential([
      tf.keras.layers.Dense(10) for _ in range(10)])

for _ in range(100):
  # With `clear_session()` called at the beginning,
  # TF-Keras starts with a blank state at each iteration
  # and memory consumption is constant over time.
  tf.keras.backend.clear_session()
  model = tf.keras.Sequential([
      tf.keras.layers.Dense(10) for _ in range(10)])

Example 2: resetting the layer name generation counter

>>> import tensorflow as tf
>>> layers = [tf.keras.layers.Dense(10) for _ in range(10)]
>>> new_layer = tf.keras.layers.Dense(10)
>>> print(new_layer.name)
dense_10
>>> tf.keras.backend.set_learning_phase(1)
>>> print(tf.keras.backend.learning_phase())
1
>>> tf.keras.backend.clear_session()
>>> new_layer = tf.keras.layers.Dense(10)
>>> print(new_layer.name)
dense

[source]

floatx function

tf_keras.backend.floatx()

Returns the default float type, as a string.

E.g. 'float16', 'float32', 'float64'.

Returns

String, the current default float type.

Example

>>> tf.keras.backend.floatx()
'float32'

[source]

set_floatx function

tf_keras.backend.set_floatx(value)

Sets the default float type.

Note: It is not recommended to set this to float16 for training, as this will likely cause numeric stability issues. Instead, mixed precision, which is using a mix of float16 and float32, can be used by calling tf.keras.mixed_precision.set_global_policy('mixed_float16'). See the mixed precision guide for details.

Arguments

  • value: String; 'float16', 'float32', or 'float64'.

Example

>>> tf.keras.backend.floatx()
'float32'
>>> tf.keras.backend.set_floatx('float64')
>>> tf.keras.backend.floatx()
'float64'
>>> tf.keras.backend.set_floatx('float32')

Raises

  • ValueError: In case of invalid value.

[source]

image_data_format function

tf_keras.backend.image_data_format()

Returns the default image data format convention.

Returns

A string, either 'channels_first' or 'channels_last'

Example

>>> tf.keras.backend.image_data_format()
'channels_last'

[source]

set_image_data_format function

tf_keras.backend.set_image_data_format(data_format)

Sets the value of the image data format convention.

Arguments

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

Example

>>> tf.keras.backend.image_data_format()
'channels_last'
>>> tf.keras.backend.set_image_data_format('channels_first')
>>> tf.keras.backend.image_data_format()
'channels_first'
>>> tf.keras.backend.set_image_data_format('channels_last')

Raises

  • ValueError: In case of invalid data_format value.

[source]

epsilon function

tf_keras.backend.epsilon()

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

Returns

A float.

Example

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

[source]

set_epsilon function

tf_keras.backend.set_epsilon(value)

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

Arguments

  • value: float. New value of epsilon.

Example

>>> tf.keras.backend.epsilon()
1e-07
>>> tf.keras.backend.set_epsilon(1e-5)
>>> tf.keras.backend.epsilon()
1e-05
>>> tf.keras.backend.set_epsilon(1e-7)

[source]

is_keras_tensor function

tf_keras.backend.is_keras_tensor(x)

Returns whether x is a TF-Keras tensor.

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

Arguments

  • x: A candidate tensor.

Returns

  • A boolean: Whether the argument is a TF-Keras tensor.

Raises

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

Examples

>>> np_var = np.array([1, 2])
>>> # A numpy array is not a symbolic tensor.
>>> tf.keras.backend.is_keras_tensor(np_var)
Traceback (most recent call last):
...
ValueError: Unexpectedly found an instance of type
`<class 'numpy.ndarray'>`.
Expected a symbolic tensor instance.
>>> keras_var = tf.keras.backend.variable(np_var)
>>> # A variable created with the keras backend is not a TF-Keras tensor.
>>> tf.keras.backend.is_keras_tensor(keras_var)
False
>>> keras_placeholder = tf.keras.backend.placeholder(shape=(2, 4, 5))
>>> # A placeholder is a TF-Keras tensor.
>>> tf.keras.backend.is_keras_tensor(keras_placeholder)
True
>>> keras_input = tf.keras.layers.Input([10])
>>> # An Input is a TF-Keras tensor.
>>> tf.keras.backend.is_keras_tensor(keras_input)
True
>>> keras_layer_output = tf.keras.layers.Dense(10)(keras_input)
>>> # Any TF-Keras layer output is a TF-Keras tensor.
>>> tf.keras.backend.is_keras_tensor(keras_layer_output)
True

[source]

get_uid function

tf_keras.backend.get_uid(prefix="")

Associates a string prefix with an integer counter in a TensorFlow graph.

Arguments

  • prefix: String prefix to index.

Returns

Unique integer ID.

Example

>>> get_uid('dense')
1
>>> get_uid('dense')
2

[source]

rnn function

tf_keras.backend.rnn(
    step_function,
    inputs,
    initial_states,
    go_backwards=False,
    mask=None,
    constants=None,
    unroll=False,
    input_length=None,
    time_major=False,
    zero_output_for_mask=False,
    return_all_outputs=True,
)

Iterates over the time dimension of a tensor.

Arguments

  • step_function: RNN step function. Args; input; Tensor with shape (samples, ...) (no time dimension), representing input for the batch of samples at a certain time step. states; List of tensors. Returns; output; Tensor with shape (samples, output_dim) (no time dimension). new_states; List of tensors, same length and shapes as 'states'. The first state in the list must be the output tensor at the previous timestep.
  • inputs: Tensor of temporal data of shape (samples, time, ...) (at least 3D), or nested tensors, and each of which has shape (samples, time, ...).
  • initial_states: Tensor with shape (samples, state_size) (no time dimension), containing the initial values for the states used in the step function. In the case that state_size is in a nested shape, the shape of initial_states will also follow the nested structure.
  • 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, 1), with a zero for every element that is masked.
  • constants: List of constant values passed at each step.
  • unroll: Whether to unroll the RNN or to use a symbolic while_loop.
  • input_length: An integer or a 1-D Tensor, depending on whether the time dimension is fixed-length or not. In case of variable length input, it is used for masking in case there's no mask specified.
  • time_major: Boolean. If true, the inputs and outputs will be in shape (timesteps, batch, ...), whereas in the False case, it will be (batch, timesteps, ...). Using time_major = True is a bit more efficient because it avoids transposes at the beginning and end of the RNN calculation. However, most TensorFlow data is batch-major, so by default this function accepts input and emits output in batch-major form.
  • zero_output_for_mask: Boolean. If True, the output for masked timestep will be zeros, whereas in the False case, output from previous timestep is returned.
  • return_all_outputs: Boolean. If True, return the recurrent outputs for all timesteps in the sequence. If False, only return the output for the last timestep (which consumes less memory).

Returns

A tuple, (last_output, outputs, new_states). last_output: the latest output of the rnn, of shape (samples, ...) outputs: - If return_all_outputs=True: a tensor with shape (samples, time, ...) where each entry outputs[s, t] is the output of the step function at time t for sample s - Else, a tensor equal to last_output with shape (samples, 1, ...) new_states: list of tensors, latest states returned by the 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).