Config¶
-
class
tfsnippet.utils.
Config
¶ Bases:
object
Base class for defining config values.
Derive sub-classes of
Config
, and define config values as public class attributes. For example:class YourConfig(Config): max_epoch = 100 learning_rate = 0.01 activation = ConfigField(str, default='leaky_relu', choices=['sigmoid', 'relu', 'leaky_relu']) l2_regularization = ConfigField(float, default=None) config = YourConfig()
When an attribute is assigned, it will be validated by:
- If the attribute is defined as a
ConfigField
, then itsvalidate()
will be used to validate the value. - If the attribute is not
None
, and a validator is registered for type(value), then an instance of this type of validator will be used to validate the value. - Otherwise if the attribute is not defined, or is
None
, then no validation will be performed.
For example:
config.l2_regularization = 5e-4 # okay config.l2_regularization = 'xxx' # raise an error config.activation = 'sigmoid' # okay config.activation = 'tanh' # raise an error config.new_attribute = 'yyy' # okay
The config object also implements dict-like interface:
# to test whether a key exists print(key in config) # to iterate through all config values for key in config: print(key, config[key]) # to set a config value config[key] = value
You may get all the config values of a config object as dict:
print(config_to_dict(config))
Or you may get the default values of the config class as dict:
print(config_defaults(YourConfig)) print(config_defaults(config)) # same as the above line
Methods Summary
to_dict
()Get the config values as a dict. update
(key_values)Update the config values from key_values. Methods Documentation
-
update
(key_values)¶ Update the config values from key_values.
Parameters: key_values – A dict, or a sequence of (key, value) pairs.
- If the attribute is defined as a