sacred
sacred copied to clipboard
Why config keys have to be string?
Hi, I encounter a problem when I pass a dictionary with keys that are not string to the parameter. eg. param = {'weight':{1:10, 2:20}}. I get the error: KeyError: u'Invalid key "2". Config-keys have to be strings, but was <type 'int'>'. Is there any reason for that? And if there is any work around? edit: realized that the config-keys should be json-serializable and that's the result why it should be string.
Hi, yes JSON is using only strings as keys. Using JSON is a requirement to store the configuration in the MongoDB, and also for some config-file formats. It also helps in setting things from the commandline. If for example you just convert the integers to strings in your case you could do:
$> ./my_experiment.py with param.weight.2 = 23
For this particular usecase it would be a possibility to make sacred automatically convert int
keys in dictionaries to str
when storing them. And then at runtime have a custom dict
subclass that tries to parses all keys as integers too and provides fallback access to the integers too.
It seems like a lot of effort for this special case though. How important is this to you? Do you have a concrete usecase where this would be required (like a 3rd party library that requires this format), or do you just prefer int
keys over str
keys?
Hi, Thanks for suggesting the way. Yes it is a requirement for third party library. If you want to fit unbalanced data in sklearn classifiers you have to give class weights in such format, where integer keys are the classes and the values are the weight.
Keeping this issue open as a feature request for non-string config keys.