Params class represents a dictionary of
parameters (e.g. for configuring a model), with added functionality around
logging and validation.
Params(params: typing.Dict[str, typing.Any], history: str = '', loading_from_archive: bool = False, files_to_archive: typing.Dict[str, str] = None) → None¶
Represents a parameter dictionary with a history, and contains other functionality around parameter passing and validation for AllenNLP.
There are currently two benefits of a
Paramsobject over a plain dictionary for parameter passing:
- We handle a few kinds of parameter validation, including making sure that parameters representing discrete choices actually have acceptable values, and making sure no extra parameters are passed.
- We log all parameter reads, including default values. This gives a more complete specification of the actual parameters used than is given in a JSON / HOCON file, because those may not specify what default values were used, whereas this will log them.
The convention for using a
Paramsobject in AllenNLP is that you will consume the parameters as you read them, so that there are none left when you’ve read everything you expect. This lets us easily validate that you didn’t pass in any extra parameters, just by making sure that the parameter dictionary is empty. You should do this when you’re done handling parameters, by calling
DEFAULT= <object object>¶
add_file_to_archive(name: str) → None¶
Any class in its
from_paramsmethod can request that some of its input files be added to the archive by calling this method.
For example, if some class
input_fileparameter, it could call
` params.add_file_to_archive("input_file") `
which would store the supplied value for
input_fileat the key
files_to_archivedict is shared with child instances via the
_check_is_dictmethod, so that the final mapping can be retrieved from the top-level
NOTE: You must call
pop()the parameter, because the
Paramsinstance looks up the value of the filename inside itself.
loading_from_archiveflag is True, this will be a no-op.
Sometimes we need to just represent the parameters as a dict, for instance when we pass them to a Keras layer(so that they can be serialised).
- quiet: bool, optional (default = False)
Whether to log the parameters before returning them as a dict.
Returns the parameters of a flat dictionary from keys to values. Nested structure is collapsed with periods.
self.paramsis not empty. We take
class_nameas an argument so that the error message gives some idea of where an error happened, if there was one.
class_nameshould be the name of the calling class, the one that got extra parameters (if there are any).
duplicate() → allennlp.common.params.Params¶
copy.deepcopy()to create a duplicate (but fully distinct) copy of these Params.
from_file(params_file: str, params_overrides: str = '') → allennlp.common.params.Params¶
Load a Params object from a configuration file.
get(key: str, default: typing.Any = <object object>)¶
Performs the functionality associated with dict.get(key) but also checks for returned dicts and returns a Params object in their place with an updated history.
pop(key: str, default: typing.Any = <object object>) → typing.Any¶
Performs the functionality associated with dict.pop(key), along with checking for returned dictionaries, replacing them with Param objects with an updated history.
keyis not present in the dictionary, and no default was specified, we raise a
ConfigurationError, instead of the typical
pop_bool(key: str, default: typing.Any = <object object>) → bool¶
Performs a pop and coerces to a bool.
pop_choice(key: str, choices: typing.List[typing.Any], default_to_first_choice: bool = False) → typing.Any¶
Gets the value of
paramsdictionary, ensuring that the value is one of the given choices. Note that this pops the key from params, modifying the dictionary, consistent with how parameters are processed in this codebase.
- key: str
Key to get the value from in the param dictionary
- choices: List[Any]
A list of valid options for values corresponding to
key. For example, if you’re specifying the type of encoder to use for some part of your model, the choices might be the list of encoder classes we know about and can instantiate. If the value we find in the param dictionary is not in
choices, we raise a
ConfigurationError, because the user specified an invalid value in their parameter file.
- default_to_first_choice: bool, optional (default=False)
If this is
True, we allow the
keyto not be present in the parameter dictionary. If the key is not present, we will use the return as the value the first choice in the
choiceslist. If this is
False, we raise a
ConfigurationError, because specifying the
keyis required (e.g., you have to specify your model class when running an experiment, but you can feel free to use default settings for encoders if you want).
pop_float(key: str, default: typing.Any = <object object>) → float¶
Performs a pop and coerces to a float.
pop_int(key: str, default: typing.Any = <object object>) → int¶
Performs a pop and coerces to an int.
pop_choice(params: typing.Dict[str, typing.Any], key: str, choices: typing.List[typing.Any], default_to_first_choice: bool = False, history: str = '?.') → typing.Any¶
Performs the same function as
Params.pop_choice(), but is required in order to deal with places that the Params object is not welcome, such as inside Keras layers. See the docstring of that method for more detail on how this function works.
This method adds a
historyparameter, in the off-chance that you know it, so that we can reproduce
Params.pop_choice()exactly. We default to using ”?.” if you don’t know the history, so you’ll have to fix that in the log if you want to actually recover the logged parameters.