View source on GitHub
|
A Config is a dict-like container for named values.
tf.keras.utils.Config(
**kwargs
)
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.
Attributes | |
|---|---|
frozen
|
Returns True if the config is frozen. |
Methods
as_dict
as_dict()
freeze
freeze()
Marks the config as frozen, preventing any ulterior modification.
get
get(
keyname, value=None
)
items
items()
keys
keys()
pop
pop(
*args
)
to_json
to_json()
unfreeze
unfreeze()
update
update(
*args, **kwargs
)
values
values()
__contains__
__contains__(
item
)
__getitem__
__getitem__(
key
)
__iter__
__iter__()
__len__
__len__()
View source on GitHub