Config
类keras.utils.Config(**kwargs)
Config 是一个类似字典的容器,用于保存命名值。
它比普通字典提供了一些优势
示例
# 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
配置的行为类似于字典
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())}")
实际上,它可以转换为一个字典
config = Config("learning_rate"=0.1, "momentum"=0.9)
dict_config = config.as_dict()
您可以轻松地将配置序列化为 JSON
config = Config("learning_rate"=0.1, "momentum"=0.9)
json_str = config.to_json()
您还可以冻结配置以防止进一步更改
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.