dnadna.params

Management of parameters to be learned in a training run.

Classes

LearnedParams(config, scenario_params[, …])

Class for managing the parameters on which a model is trained.

MCELoss()

Mean Circular Error

ParamSet([config, validate])

Class for managing a set of model parameters as defined by the param set schema.

class dnadna.params.LearnedParams(config, scenario_params, validate=True)[source]

Bases: dnadna.params.ParamSet

Class for managing the parameters on which a model is trained.

config

The learned parameters configuration, confirming to the learned parameters schema.

Type

Config or dict

property loss_funcs[source]

Maps parameter names to their loss functions.

property loss_weights[source]

Maps parameter names to their loss weights (if any).

property scenario_params

The scenario parameters table, as a pandas.DataFrame giving the known values of parameters the dataset is being trained on for all scenarios the dataset is being trained on.

to(device=None)[source]

Similarly to torch.nn.Module.to with device=device, moves all loss_weights and the parameters of all loss_funcs to the specified device.

class dnadna.params.MCELoss[source]

Bases: torch.nn.modules.module.Module

Mean Circular Error

forward(x, y)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class dnadna.params.ParamSet(config={}, validate=True)[source]

Bases: dnadna.utils.config.ConfigMixIn

Class for managing a set of model parameters as defined by the param set schema.

In most cases this is not used directly; instead the LearnedParams subclass of this is used to manage a parameter set used for model training. This base class implements more basic functionality that does not depend on a scenario_params table and does not support training-specific functionality like LearnedParams.

property n_outputs[source]

Expected number of outputs for a network that is trained for this set of parameters.

This should be one output per regression parameter, and one output per class of each classification parameter, giving the likelihood of an input falling into that class.

property param_names[source]

Return list of names of all learned parameters.

Parameters are given in the order they were specified in the config. If an unordered mapping was used, the order will still be preserved since Python 3.6+ preserves dictionary insertion order and this holds when loading mappings from YAML and JSON.

It is also possible to instantiate the ParamSet with a list of mappings, each containing one element. This is how omaps (ordered mappings) are encoded in YAML, and the same structure can be used in JSON. In this case the order of the elements in the list are preserved.

Examples

>>> from dnadna.params import ParamSet
>>> config = {
...     'b': {'type': 'regression', 'loss_func': 'MSE'},
...     # 'x' gets the default loss func, cross entropy
...     'x': {'type': 'classification', 'classes': 3},
...     # 'a' gets the default loss func, MSE
...     'a': {'type': 'regression'},
...     'z': {'type': 'classification', 'classes': 3,
...           'loss_func': 'Cross Entropy'},
...     'y': {'type': 'regression', 'loss_func': 'MCE'}
... }
>>> learned_params = ParamSet(config)
>>> learned_params.param_names
['b', 'x', 'a', 'z', 'y']

Similar (shorter) example, but from a list:

>>> config = [
...     {'b': {'type': 'regression', 'loss_func': 'MSE'}},
...     {'x': {'type': 'classification', 'classes': 3}}
... ]
...
>>> learned_params = ParamSet(config)
>>> learned_params.param_names
['b', 'x']
property param_slices[source]

Returns a dict mapping parameter names to tuples of slices.

The slice to take of the targets tensor for values of that parameter, and the slice to take of the outputs tensor for that parameter.

This is used during training: The targets tensor contains (for a batch of one or more scenarios) the known target values for each parameter being learned, and the outputs tensor contains the predicted values for each parameter returned from the model being trained (where in outputs there is a predicted likelihood for each category in a classification parameter).

Examples

>>> from dnadna.params import ParamSet
>>> import pandas as pd
>>> config = {
...     'b': {'type': 'regression', 'loss_func': 'MSE'},
...     # 'x' gets the default loss func, cross entropy
...     'x': {'type': 'classification', 'classes': 3},
...     # 'a' gets the default loss func, MSE
...     'a': {'type': 'regression'},
...     'z': {'type': 'classification', 'classes': 3,
...           'loss_func': 'Cross Entropy'},
...     'y': {'type': 'regression', 'loss_func': 'MCE'}
... }
>>> learned_params = ParamSet(config)
>>> learned_params.param_slices
{'b': (slice(0, 1, None), slice(0, 1, None)),
 'x': (slice(1, 2, None), slice(1, 4, None)),
 'a': (slice(2, 3, None), slice(4, 5, None)),
 'z': (slice(3, 4, None), slice(5, 8, None)),
 'y': (slice(4, 5, None), slice(8, 9, None))}
property params[source]

Returns a dict of params.

Examples

This example also demonstrates that the param details are also filled out with defaults not included in the original config (loss_weight, n_classes, etc.):

>>> from dnadna.params import ParamSet
>>> config = {
...     'b': {'type': 'regression', 'loss_func': 'MSE'},
...     # 'x' gets the default loss func, cross entropy
...     'x': {'type': 'classification', 'classes': 3},
...     # 'a' gets the default loss func, MSE
...     'a': {'type': 'regression'},
...     'z': {'type': 'classification', 'classes': ['a', 'b'],
...           'loss_func': 'Cross Entropy'},
...     'y': {'type': 'regression', 'loss_func': 'MCE'}
... }
>>> learned_params = ParamSet(config)
>>> learned_params.params
{'b': {'type': 'regression', 'loss_func': 'MSE', 'loss_weight': 1,
       'log_transform': False, 'tied_to_position': False},
 'x': {'type': 'classification', 'classes': ['0', '1', '2'],
       'loss_func': 'Cross Entropy', 'loss_weight': 1, 'n_classes': 3},
 'a': {'type': 'regression', 'loss_func': 'MSE', 'loss_weight': 1,
       'log_transform': False, 'tied_to_position': False},
 'z': {'type': 'classification', 'classes': ['a', 'b'],
       'loss_func': 'Cross Entropy', 'loss_weight': 1, 'n_classes': 2},
 'y': {'type': 'regression', 'loss_func': 'MCE', 'loss_weight': 1,
       'log_transform': False, 'tied_to_position': False}}