google / ml_collections

ML Collections is a library of Python Collections designed for ML use cases.
https://ml-collections.readthedocs.io/
Apache License 2.0
898 stars 42 forks source link

Allow None values for float fields #33

Closed sedol1339 closed 2 months ago

sedol1339 commented 3 months ago

This is not a bug report, but is this possible to allow None values for float fields?

For example, I have kwargs field that stores kwarg-value pairs for Wav2vec2 constructor. Some kwarg, for example activation_dropout may be either float or None (use a value from loaded model checkpoint). None equivalent to the absence of this kwarg.

The config default value is None, and I want to override it with float in the command line:

--config.wav2vec2.kwargs.model_kwargs.activation_dropout=0

However, it says cannot override None type.

Actually, I can specify np.nan as default. But the Wav2vec2 does not accept np.nan, so I need to manyally convert np.nan to None before calling the constructor, which is awkward. Is there a solution?

GeorgOstrovski commented 2 months ago

This is indeed possible with a FieldReference.

model_kwargs = config_dict.ConfigDict()
model_kwargs.activation_dropout = config_dict.FieldReference(None, field_type=float)
...

This will now have default value None, but will allow overrides with floats (while still guarding against other types incompatible with float).