Keras configuration utilities
- 원본 링크 : https://keras.io/api/utils/config_utils/
- 최종 확인 : 2024-11-25
version
function
keras.version()
clear_session
function
keras.utils.clear_session(free_memory=True)
Resets all state generated by Keras.
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.
Arguments
- free_memory: Whether to call Python garbage collection.
It’s usually a good practice to call it to make sure
memory used by deleted objects is immediately freed.
However, it may take a few seconds to execute, so
when using
clear_session()
in a short loop, you may want to skip it.
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 = keras.Sequential([
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.
keras.backend.clear_session()
model = keras.Sequential([
keras.layers.Dense(10) for _ in range(10)])
Example 2: resetting the layer name generation counter
>>> layers = [keras.layers.Dense(10) for _ in range(10)]
>>> new_layer = keras.layers.Dense(10)
>>> print(new_layer.name)
dense_10
>>> keras.backend.clear_session()
>>> new_layer = keras.layers.Dense(10)
>>> print(new_layer.name)
dense
enable_traceback_filtering
function
keras.config.enable_traceback_filtering()
Turn on traceback filtering.
Raw Keras tracebacks (also known as stack traces) involve many internal frames, which can be challenging to read through, while not being actionable for end users. By default, Keras filters internal frames in most exceptions that it raises, to keep traceback short, readable, and focused on what’s actionable for you (your own code).
See also keras.config.disable_traceback_filtering()
and
keras.config.is_traceback_filtering_enabled()
.
If you have previously disabled traceback filtering via
keras.config.disable_traceback_filtering()
, you can re-enable it via
keras.config.enable_traceback_filtering()
.
disable_traceback_filtering
function
keras.config.disable_traceback_filtering()
Turn off traceback filtering.
Raw Keras tracebacks (also known as stack traces) involve many internal frames, which can be challenging to read through, while not being actionable for end users. By default, Keras filters internal frames in most exceptions that it raises, to keep traceback short, readable, and focused on what’s actionable for you (your own code).
See also keras.config.enable_traceback_filtering()
and
keras.config.is_traceback_filtering_enabled()
.
If you have previously disabled traceback filtering via
keras.config.disable_traceback_filtering()
, you can re-enable it via
keras.config.enable_traceback_filtering()
.
is_traceback_filtering_enabled
function
keras.config.is_traceback_filtering_enabled()
Check if traceback filtering is enabled.
Raw Keras tracebacks (also known as stack traces) involve many internal frames, which can be challenging to read through, while not being actionable for end users. By default, Keras filters internal frames in most exceptions that it raises, to keep traceback short, readable, and focused on what’s actionable for you (your own code).
See also keras.config.enable_traceback_filtering()
and
keras.config.disable_traceback_filtering()
.
If you have previously disabled traceback filtering via
keras.config.disable_traceback_filtering()
, you can re-enable it via
keras.config.enable_traceback_filtering()
.
Returns
Boolean, True
if traceback filtering is enabled,
and False
otherwise.
enable_interactive_logging
function
keras.config.enable_interactive_logging()
Turn on interactive logging.
When interactive logging is enabled, Keras displays logs via stdout. This provides the best experience when using Keras in an interactive environment such as a shell or a notebook.
disable_interactive_logging
function
keras.config.disable_interactive_logging()
Turn off interactive logging.
When interactive logging is disabled, Keras sends logs to absl.logging
.
This is the best option when using Keras in a non-interactive
way, such as running a training or inference job on a server.
is_interactive_logging_enabled
function
keras.config.is_interactive_logging_enabled()
Check if interactive logging is enabled.
To switch between writing logs to stdout and absl.logging
, you may use
keras.config.enable_interactive_logging()
and
keras.config.disable_interactive_logging()
.
Returns
Boolean, True
if interactive logging is enabled,
and False
otherwise.
enable_unsafe_deserialization
function
keras.config.enable_unsafe_deserialization()
Disables safe mode globally, allowing deserialization of lambdas.
floatx
function
keras.config.floatx()
Return the default float type, as a string.
E.g. 'bfloat16'
, 'float16'
, 'float32'
, 'float64'
.
Returns
String, the current default float type.
Example
>>> keras.config.floatx()
'float32'
set_floatx
function
keras.config.set_floatx(value)
Set the default float dtype.
Note: It is not recommended to set this to "float16"
for training,
as this will likely cause numeric stability issues.
Instead, mixed precision, which leverages
a mix of float16
and float32
. It can be configured by calling
keras.mixed_precision.set_dtype_policy('mixed_float16')
.
Arguments
- value: String;
'bfloat16'
,'float16'
,'float32'
, or'float64'
.
Examples
>>> keras.config.floatx()
'float32'
>>> keras.config.set_floatx('float64')
>>> keras.config.floatx()
'float64'
>>> # Set it back to float32
>>> keras.config.set_floatx('float32')
Raises
- ValueError: In case of invalid value.
image_data_format
function
keras.config.image_data_format()
Return the default image data format convention.
Returns
A string, either 'channels_first'
or 'channels_last'
.
Example
>>> keras.config.image_data_format()
'channels_last'
set_image_data_format
function
keras.config.set_image_data_format(data_format)
Set the value of the image data format convention.
Arguments
- data_format: string.
'channels_first'
or'channels_last'
.
Examples
>>> keras.config.image_data_format()
'channels_last'
>>> keras.config.set_image_data_format('channels_first')
>>> keras.config.image_data_format()
'channels_first'
>>> # Set it back to `'channels_last'`
>>> keras.config.set_image_data_format('channels_last')
epsilon
function
keras.config.epsilon()
Return the value of the fuzz factor used in numeric expressions.
Returns
A float.
Example
>>> keras.config.epsilon()
1e-07
set_epsilon
function
keras.config.set_epsilon(value)
Set the value of the fuzz factor used in numeric expressions.
Arguments
- value: float. New value of epsilon.
Examples
>>> keras.config.epsilon()
1e-07
>>> keras.config.set_epsilon(1e-5)
>>> keras.config.epsilon()
1e-05
>>> # Set it back to the default value.
>>> keras.config.set_epsilon(1e-7)
backend
function
keras.config.backend()
Publicly accessible method for determining the current backend.
Returns
String, the name of the backend Keras is currently using. One of
"tensorflow"
, "torch"
, or "jax"
.
Example
>>> keras.config.backend()
'tensorflow'