Is it possible to pass a python object from a config file?

Viewed 137

I don't mind if it's YAML, JSON, .ini, or whatever.

The user should be able to change the reduction value in the call to the tf.keras.losses.CategoricalHinge function via a configuration file like below:

import tensorflow as tf

# config file reading happens here (expected scenario):
f = read_the_config_file()
reduction = f["reduction"]  # retrieved the reduction value

# pass `reduction` to the loss function:
loss = tf.keras.losses(reduction)

reduction could either be tf.keras.losses.Reduction.NONE or tf.keras.losses.Reduction.AUTO. These values are tensorflow objects.

An example JSON file I'd expect will be like:

{
"reduction ": tf.keras.losses.Reduction.NONE
}

But obviously, this is an invalid JSON file.

Question:

Are there ways to let user pass this kind of a parameter in a config file? If not, what are the other ways of getting a such user input without keeping a string to object mapping in the user code?

2 Answers

I am not very familiar with Python, but I assume you can implement the read_the_config_file() function not only (1) reads the configuration file's contents into a dictionary, but also replaces some string values with object values for a few special entries in the dictionary, such as reduction in your example. At least that way, the string-to-object mapping code is encapsulated within read_the_contents_file() rather than being spread out in the rest of user code.

Look into using gin-config, a lightweight config framework that allows you to work with objects in the config file.

Related