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, or metrics. Used by vak.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 a dict with the required keys, that map to empty dicts.

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 of vak.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:

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.