Keras 3 API documentation / Utilities / Experiment management utilities

Experiment management utilities

[source]

Config class

keras.utils.Config(**kwargs)

A Config is a dict-like container for named values.

It offers a few advantages over a plain dict:

  • Setting and retrieving values via attribute setting / getting.
  • Ability to freeze the config to ensure no accidental config modifications occur past a certain point in your program.
  • Easy serialization of the whole config as JSON.

Examples

# Create a config via constructor arguments
config = Config("learning_rate"=0.1, "momentum"=0.9)

# Then keep adding to it via attribute-style setting
config.use_ema = True
config.ema_overwrite_frequency = 100

# You can also add attributes via dict-like access
config["seed"] = 123

# You can retrieve entries both via attribute-style
# access and dict-style access
assert config.seed == 100
assert config["learning_rate"] == 0.1

A config behaves like a dict:

config = Config("learning_rate"=0.1, "momentum"=0.9)
for k, v in config.items():
    print(f"{k}={v}")

print(f"keys: {list(config.keys())}")
print(f"values: {list(config.values())}")

In fact, it can be turned into one:

config = Config("learning_rate"=0.1, "momentum"=0.9)
dict_config = config.as_dict()

You can easily serialize a config to JSON:

config = Config("learning_rate"=0.1, "momentum"=0.9)

json_str = config.to_json()

You can also freeze a config to prevent further changes:

config = Config()
config.optimizer = "adam"
config.seed = 123

# Freeze the config to prevent changes.
config.freeze()
assert config.frozen

config.foo = "bar"  # This will raise an error.