我不介意它是 YAML、JSON、.ini 还是其他格式。
用户应该能够通过如下配置文件更改对 tf.keras.losses.CategoricalHinge 函数调用中的 reduction
值:
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
可以是 tf.keras.losses.Reduction.NONE
或 tf.keras.losses.Reduction.AUTO
。这些值是 tensorflow 对象。
我期望的 JSON 文件示例如下:
{
"reduction ": tf.keras.losses.Reduction.NONE
}
但显然,这是一个无效的 JSON 文件。
问题:
有没有办法让用户在配置文件中传递这种参数? 如果不是,那么在用户代码中不保留字符串到对象映射的情况下,还有哪些其他方法可以获取此类用户输入?
答案 0 :(得分:0)
我对 Python 不是很熟悉,但我假设您可以实现 read_the_config_file()
函数,不仅 (1) 将配置文件的内容读入字典,而且还用对象值替换一些字符串值字典中的特殊条目,例如您的示例中的 reduction
。至少这样,字符串到对象的映射代码被封装在 read_the_contents_file()
中,而不是散布在用户代码的其余部分中。