the type of float to use throughout a session epsilon fuzz factor used throughout the codebase default image data format one of channelslast channelsfirst default backend tensorflow return the default float type as a string e g float16 float32 float64 returns string the current default float type example keras config floatx float32 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 mixedprecision setdtypepolicy mixedfloat16 args value string float16 float32 or float64 examples keras config floatx float32 keras config setfloatx float64 keras config floatx float64 set it back to float32 keras config setfloatx float32 raises valueerror in case of invalid value return the value of the fuzz factor used in numeric expressions returns a float example keras config epsilon 1e07 set the value of the fuzz factor used in numeric expressions args value float new value of epsilon examples keras config epsilon 1e07 keras config setepsilon1e5 keras config epsilon 1e05 set it back to the default value keras config setepsilon1e7 return the default image data format convention returns a string either channelsfirst or channelslast example keras config imagedataformat channelslast set the value of the image data format convention args dataformat string channelsfirst or channelslast examples keras config imagedataformat channelslast keras config setimagedataformat channelsfirst keras config imagedataformat channelsfirst set it back to channelslast keras config setimagedataformat channelslast set keras base dir path given kerashome env variable if applicable otherwise either keras or tmp private accessor for the keras home location attempt to read keras config file save config file if possible except permission denied and potential race conditions in multithreaded environments except permission denied set backend based on kerasbackend flag if applicable if we are not running on the tensorflow backend we should stop tensorflow from using all available gpu memory see https www tensorflow orgguidegpulimitinggpumemorygrowth 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 the type of float to use throughout a session epsilon fuzz factor used throughout the codebase default image data format one of channels_last channels_first default backend tensorflow return the default float type as a string e g float16 float32 float64 returns string the current default float type example keras config floatx float32 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 args value string 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 return the value of the fuzz factor used in numeric expressions returns a float example keras config epsilon 1e 07 set the value of the fuzz factor used in numeric expressions args 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 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 the value of the image data format convention args 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 set keras base dir path given keras_home env variable if applicable otherwise either keras or tmp private accessor for the keras home location attempt to read keras config file save config file if possible except permission denied and potential race conditions in multi threaded environments except permission denied set backend based on keras_backend flag if applicable if we are not running on the tensorflow backend we should stop tensorflow from using all available gpu memory see https www tensorflow org guide gpu limiting_gpu_memory_growth 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
import json
import os

from keras.api_export import keras_export


_FLOATX = "float32"


_EPSILON = 1e-7


_IMAGE_DATA_FORMAT = "channels_last"


_BACKEND = "tensorflow"


@keras_export(["keras.config.floatx", "keras.backend.floatx"])
def floatx():
    
    return _FLOATX


@keras_export(["keras.config.set_floatx", "keras.backend.set_floatx"])
def set_floatx(value):
    
    global _FLOATX
    accepted_dtypes = {"float16", "float32", "float64"}
    if value not in accepted_dtypes:
        raise ValueError(
            f"Unknown `floatx` value: {value}. "
            f"Expected one of {accepted_dtypes}"
        )
    _FLOATX = str(value)


@keras_export(["keras.config.epsilon", "keras.backend.epsilon"])
def epsilon():
    
    return _EPSILON


@keras_export(["keras.config.set_epsilon", "keras.backend.set_epsilon"])
def set_epsilon(value):
    
    global _EPSILON
    _EPSILON = value


@keras_export(
    [
        "keras.config.image_data_format",
        "keras.backend.image_data_format",
    ]
)
def image_data_format():
    
    return _IMAGE_DATA_FORMAT


@keras_export(
    [
        "keras.config.set_image_data_format",
        "keras.backend.set_image_data_format",
    ]
)
def set_image_data_format(data_format):
    
    global _IMAGE_DATA_FORMAT
    data_format = str(data_format).lower()
    if data_format not in {"channels_first", "channels_last"}:
        raise ValueError(
            "The `data_format` argument must be one of "
            "{'channels_first', 'channels_last'}. "
            f"Received: data_format={data_format}"
        )
    _IMAGE_DATA_FORMAT = data_format


def standardize_data_format(data_format):
    if data_format is None:
        return image_data_format()
    data_format = str(data_format).lower()
    if data_format not in {"channels_first", "channels_last"}:
        raise ValueError(
            "The `data_format` argument must be one of "
            "{'channels_first', 'channels_last'}. "
            f"Received: data_format={data_format}"
        )
    return data_format




if "KERAS_HOME" in os.environ:
    _KERAS_DIR = os.environ.get("KERAS_HOME")
else:
    _keras_base_dir = os.path.expanduser("~")
    if not os.access(_keras_base_dir, os.W_OK):
        _keras_base_dir = "/tmp"
    _KERAS_DIR = os.path.join(_keras_base_dir, ".keras")


def keras_home():
    
    return _KERAS_DIR



_config_path = os.path.expanduser(os.path.join(_KERAS_DIR, "keras.json"))
if os.path.exists(_config_path):
    try:
        with open(_config_path) as f:
            _config = json.load(f)
    except ValueError:
        _config = {}
    _floatx = _config.get("floatx", floatx())
    assert _floatx in {"float16", "float32", "float64"}
    _epsilon = _config.get("epsilon", epsilon())
    assert isinstance(_epsilon, float)
    _backend = _config.get("backend", _BACKEND)
    _image_data_format = _config.get("image_data_format", image_data_format())
    assert _image_data_format in {"channels_last", "channels_first"}

    set_floatx(_floatx)
    set_epsilon(_epsilon)
    set_image_data_format(_image_data_format)
    _BACKEND = _backend


if not os.path.exists(_KERAS_DIR):
    try:
        os.makedirs(_KERAS_DIR)
    except OSError:
        
        
        pass

if not os.path.exists(_config_path):
    _config = {
        "floatx": floatx(),
        "epsilon": epsilon(),
        "backend": _BACKEND,
        "image_data_format": image_data_format(),
    }
    try:
        with open(_config_path, "w") as f:
            f.write(json.dumps(_config, indent=4))
    except IOError:
        
        pass


if "KERAS_BACKEND" in os.environ:
    _backend = os.environ["KERAS_BACKEND"]
    if _backend:
        _BACKEND = _backend


if _BACKEND != "tensorflow":
    
    
    
    os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"


@keras_export(
    [
        "keras.config.backend",
        "keras.backend.backend",
    ]
)
def backend():
    
    return _BACKEND