text
stringlengths
0
1.46k
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,
# 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 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'
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.experimental.set_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.
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'
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.
epsilon function