gin-config icon indicating copy to clipboard operation
gin-config copied to clipboard

Option to print gin dictionary (for, e.g., logging)

Open phrasenmaeher opened this issue 2 years ago • 1 comments

We can use the gin.operative_config_str(), which prints a useful overview. However, for logging the saved parameters, some people (me included) like to query the internal dictionary. We can access it with gin.config._OPERATIVE_CONFIG (recommended) or gin.config._CONFIG (not recommended, also includes unused parameters). However, this requires additional parsing.

For this, I came up with the following code:

def gin_config_to_readable_dictionary(gin_config: dict):
    """
    Parses the gin configuration to a dictionary. Useful for logging to e.g. W&B
    :param gin_config: the gin's config dictionary. Can be obtained by gin.config._OPERATIVE_CONFIG
    :return: the parsed (mainly: cleaned) dictionary
    """
    data = {}
    for key in gin_config.keys():
        name = key[1].split(".")[1]
        values = gin_config[key]
        for k, v in values.items():
            data[".".join([name, k])] = v

    return data

An example output is given below:

{'create_argument_parser.epochs': 10,
 'create_argument_parser.batch_size': 128,
 'create_argument_parser.model_number': -1}

The naming follows the way parameter values are stored in a config.gin file: the decorated class/function name, followed by the parameter's name.

This is not a critical thing, but it might be useful as an enhancement.

phrasenmaeher avatar Oct 13 '21 06:10 phrasenmaeher

Thanks, I was looking for exactly this, and I believe that this should be part of gin.utils or something. Especially for keeping track of different experiment runs, it's much more useful to have a dict as opposed to a pre-generated string with newlines.

autolyticus avatar Sep 09 '22 15:09 autolyticus