Base classes#
Inherit from these classes to add a new model, objective function or fitting paradigm that works with the rest of WhoBPyT. See Code architecture for an overview.
- class whobpyt.datatypes.AbstractParams(**kwargs)#
Base class for a model’s parameter container.
Subclasses set each model parameter as an attribute holding a
Parameter, typically with defaults that keyword arguments can override (seeJansenRitParams). The parameters may belong to the neural mass model and/or the measurement model.Methods
Return the names of the learnable parameters.
to(device)Move all parameters to
device(atorch.device).- getFittedNames()#
Return the names of the learnable parameters.
- to(device)#
Move all parameters to
device(atorch.device).
- class whobpyt.datatypes.AbstractNeuralModel(params)#
Base class for models (typically neural mass models) that can be fitted.
A model is a
torch.nn.Modulethat simulates one window of activity per call toforward(). The measurement model (for example a lead field for EEG) may be part of the model.Subclasses should set
state_namesandoutput_names, callsetModelParameters()in their__init__, and implementcreateIC(),createDelayIC()andforward().- Parameters:
- params
AbstractParams The model’s parameters.
- params
- Attributes:
- state_names
listofstr Names of the model’s state variables.
- output_names
listofstr Names of the model’s outputs. The first is used by the objective functions by default.
- track_params
listofstr Names of the learnable parameters, recorded during training.
- params_fitted
dict 'modelparameter'and'hyperparameter'lists of tensors that the optimizers update. Set bysetModelParameters().- commit_hash
strorNone WhoBPyT’s git commit, if running from a git checkout.
- state_names
Methods
createDelayIC(ver)Return the initial history of state variables needed for delays.
createIC(ver)Return initial conditions for the model's state variables.
forward(external, hx, hE)Simulate one window.
info()Return the model's state names, output names, tracked parameters and commit.
Register the learnable parameters with PyTorch.
- createDelayIC(ver)#
Return the initial history of state variables needed for delays.
The default returns a dummy tensor, for models without delays.
- createIC(ver)#
Return initial conditions for the model’s state variables.
- forward(external, hx, hE)#
Simulate one window.
- Parameters:
- external
torch.Tensor External input for the window.
- hx
torch.Tensor Current state.
- hE
torch.Tensor History used for delayed inputs.
- external
- Returns:
- next_state
dict Simulated time series of each state variable and output, plus
'current_state', the state at the end of the window.- hE
torch.Tensor Updated history.
- next_state
- info()#
Return the model’s state names, output names, tracked parameters and commit.
- setModelParameters()#
Register the learnable parameters with PyTorch.
Every
Parameterinparamswithfit_par=Truebecomes atorch.nn.Parameterand is added toparams_fitted['modelparameter']; learnable priors (fit_hyper=True) are added toparams_fitted['hyperparameter']. Call this in the subclass’s__init__.
- class whobpyt.datatypes.AbstractLoss(simKey=None, model=None, device=torch.device('cpu'))#
Base class for objective functions.
Subclasses implement
main_loss(), which compares simulated and empirical data. A full objective can combine it withprior_loss(), asCostsJRdoes.- Parameters:
- simKey
str Which of the model’s outputs or state variables to compare with the data, for example
'eeg'.- model
AbstractNeuralModel The model being fitted. Needed by
prior_loss().- device
torch.device Device to compute on.
- simKey
Methods
main_loss(simData, empData)Return the loss comparing simulated and empirical data.
Return the penalties keeping learnable parameters near their priors.
- main_loss(simData, empData)#
Return the loss comparing simulated and empirical data.
Any other information the loss needs should be given when the object is created, so the fitting classes can call every loss the same way.
- Parameters:
- simData
dictoftorch.Tensor Simulated time series, keyed by variable name. Usually accessed as
simData[self.simKey].- empData
torch.Tensor The target: an empirical time series or a summary measure computed from one.
- simData
- Returns:
torch.TensorA scalar loss to back-propagate.
- prior_loss()#
Return the penalties keeping learnable parameters near their priors.
For every parameter whose prior is learned (
fit_hyper=True), the penalty is the precision-weighted squared distance between the value and the prior mean, minus the log precision.- Returns:
listoftorch.TensorOne penalty per parameter; sum them to add to the loss.
- class whobpyt.datatypes.AbstractFitting(model: AbstractNeuralModel, cost: AbstractLoss, device=torch.device('cpu'))#
Base class for fitting paradigms.
A fitting paradigm runs a model, compares its output with data using an objective function, and updates the model’s learnable parameters. Subclasses implement
train(),evaluate()andsimulate().- Parameters:
- model
AbstractNeuralModel The model to fit.
- cost
AbstractLoss The objective function.
- device
torch.device Device to compute on.
- model
- Attributes:
- trainingStats
TrainingStats Loss and parameter values recorded during training.
- lastRec
dictofTimeseriesorNone The simulated time series from the most recent run.
- trainingStats
Methods
evaluate(*args, **kwargs)Run the model and compare it with data, without updating parameters.
save(filename)Save this object, including the model and training history, with pickle.
simulate(*args, **kwargs)Run the model for a fixed duration.
train(*args, **kwargs)Fit the model's learnable parameters to data.
- evaluate(*args, **kwargs)#
Run the model and compare it with data, without updating parameters.
- save(filename)#
Save this object, including the model and training history, with pickle.
- simulate(*args, **kwargs)#
Run the model for a fixed duration.
- train(*args, **kwargs)#
Fit the model’s learnable parameters to data.