vak.models.definition.validate#
- vak.models.definition.validate(definition: Type[ModelDefinition]) Type[ModelDefinition] [source]#
Validate a model definition.
- A model definition is a class that has the following class variables:
- network: torch.nn.Module or dict
Neural network. If a dict, should map string network names to torch.nn.Module classes.
- loss: torch.nn.Module, callable
Either a built-in loss module, or a callable function that computes loss.
- optimizer: torch.optim.Optimizer
Optimizer used to optimize neural network parameters during training.
- metrics: dict
Metrics used to evaluate network. Should map string names of metric to callable classes that compute metric.
- default_configdict
That specifies default keyword arguments to use when instantiating any classes in
network
,optimizer
,loss
, ormetrics
. Used byvak.models.base.Model
and its sub-classes that represent model families. E.g., those classes will do:network = self.definition.network(**self.definition.default_config['network'])
. If this class variable is not specified, it defaults to adict
with the required keys, that map to emptydicts
.
By providing this abstraction,
vak
commits in code to the idea that a neural network model consists of just the network function(s), the optimizer and the loss used to optimize the parameters of the network(s), as measured with the metrics.- Parameters:
definition (ModelDefinition) β A definition of a neural network model. A class having the class variables described above, with specific classes / callables / dicts assigned to those class variables. For an example, see
vak.models.tweetynet.TweetyNet
. Does not need to be a sub-class ofvak.models.definition.ModelDefinition
(that is used for type checking).- Returns:
definition β After validation, with
default_config
set to default if none was specified, as described above.- Return type:
Notes
This function is used by the decorator
vak.decorator.model
to validate a definition when converting it into a sub-class ofhttps://peps.python.org/pep-0416/vak.models.Model
.Itβs also used by
vak.models.Model
to validate a definition when initializing a new model instance from the definition.