clear_session
functiontf_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
floatx
functiontf_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'
set_floatx
functiontf_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
'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
image_data_format
functiontf_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'
set_image_data_format
functiontf_keras.backend.set_image_data_format(data_format)
Sets the value of the image data format convention.
Arguments
'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
data_format
value.epsilon
functiontf_keras.backend.epsilon()
Returns the value of the fuzz factor used in numeric expressions.
Returns
A float.
Example
>>> tf.keras.backend.epsilon()
1e-07
set_epsilon
functiontf_keras.backend.set_epsilon(value)
Sets the value of the fuzz factor used in numeric expressions.
Arguments
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)
is_keras_tensor
functiontf_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
Returns
Raises
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
get_uid
functiontf_keras.backend.get_uid(prefix="")
Associates a string prefix with an integer counter in a TensorFlow graph.
Arguments
Returns
Unique integer ID.
Example
>>> get_uid('dense')
1
>>> get_uid('dense')
2
rnn
functiontf_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
(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.(samples, time, ...)
(at least 3D), or nested tensors, and each of which has shape
(samples, time, ...)
.(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.(samples, time, 1)
,
with a zero for every element that is masked.while_loop
.(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.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
unroll
is True
but input timestep is not a fixed
number.mask
is provided (not None
) but states is not
provided (len(states)
== 0).