osl_dynamics.models.mdynemo#
Multi-Dynamic Network Modes (M-DyNeMo).
See the model description for more details.
See also
Example script for training M-DyNeMo on simulated data (with multiple dynamics).
Classes#
Module Contents#
- class osl_dynamics.models.mdynemo.Config[source]#
Bases:
osl_dynamics.models.mod_base.BaseModelConfig,osl_dynamics.models.inf_mod_base.VariationalInferenceModelConfigSettings for M-DyNeMo.
- Parameters:
model_name (str) – Model name.
n_modes (int) – Number of modes.
n_corr_modes (int) – Number of modes for correlation. If
None, then set ton_modes.n_channels (int) – Number of channels.
sequence_length (int) – Length of sequence passed to the inference network and generative model.
inference_rnn (str) – RNN to use, either
'gru'or'lstm'.inference_n_layers (int) – Number of layers.
inference_n_units (int) – Number of units.
inference_normalization (str) – Type of normalization to use. Either
None,'batch'or'layer'.inference_activation (str) – Type of activation to use after normalization and before dropout. E.g.
'relu','elu', etc.inference_dropout (float) – Dropout rate.
inference_regularizer (str) – Regularizer.
model_rnn (str) – RNN to use, either
'gru'or'lstm'.model_n_layers (int) – Number of layers.
model_n_units (int) – Number of units.
model_normalization (str) – Type of normalization to use. Either
None,'batch'or'layer'.model_activation (str) – Type of activation to use after normalization and before dropout. E.g.
'relu','elu', etc.model_dropout (float) – Dropout rate.
model_regularizer (str) – Regularizer.
learn_means (bool) – Should we make the mean for each mode trainable?
learn_stds (bool) – Should we make the standard deviation for each mode trainable?
learn_corrs (bool) – Should we make the correlation for each mode trainable?
initial_means (np.ndarray) – Initialisation for the mode means.
initial_stds (np.ndarray) – Initialisation for mode standard deviations.
initial_corrs (np.ndarray) – Initialisation for mode correlation matrices.
stds_epsilon (float) – Error added to mode stds for numerical stability.
corrs_epsilon (float) – Error added to mode corrs for numerical stability.
means_regularizer (tf.keras.regularizers.Regularizer) – Regularizer for the mean vectors.
stds_regularizer (tf.keras.regularizers.Regularizer) – Regularizer for the standard deviation vectors.
corrs_regularizer (tf.keras.regularizers.Regularizer) – Regularizer for the correlation matrices.
do_kl_annealing (bool) – Should we use KL annealing during training?
kl_annealing_curve (str) – Type of KL annealing curve. Either
'linear'or'tanh'.kl_annealing_sharpness (float) – Parameter to control the shape of the annealing curve if
kl_annealing_curve='tanh'.n_kl_annealing_epochs (int) – Number of epochs to perform KL annealing.
init_method (str) – Initialization method to use. Defaults to ‘random_subset’.
n_init (int) – Number of initializations. Defaults to 5.
n_init_epochs (int) – Number of epochs for each initialization. Defaults to 2.
init_take (float) – Fraction of dataset to use in the initialization. Defaults to 1.0.
batch_size (int) – Mini-batch size.
learning_rate (float) – Learning rate.
lr_decay (float) – Decay for learning rate. Default is 0.1. We use
lr = learning_rate * exp(-lr_decay * epoch).gradient_clip (float) – Value to clip gradients by. This is the
clipnormargument passed to the Keras optimizer. Cannot be used ifmulti_gpu=True.n_epochs (int) – Number of training epochs.
optimizer (str or tf.keras.optimizers.Optimizer) – Optimizer to use.
'adam'is recommended.loss_calc (str) – How should we collapse the time dimension in the loss? Either
'mean'or'sum'.multi_gpu (bool) – Should be use multiple GPUs for training?
strategy (str) – Strategy for distributed learning.
best_of (int) – Number of full training runs to perform. A single run includes its own initialization and fitting from scratch.
- class osl_dynamics.models.mdynemo.Model(config)[source]#
Bases:
osl_dynamics.models.inf_mod_base.VariationalInferenceModelBaseM-DyNeMo model class.
- Parameters:
config (osl_dynamics.models.mdynemo.Config)
- get_means()[source]#
Get the mode means.
- Returns:
means – Mode means. Shape (n_modes, n_channels).
- Return type:
np.ndarray
- get_stds()[source]#
Get the mode standard deviations.
- Returns:
stds – Mode standard deviations. Shape (n_modes, n_channels, n_channels).
- Return type:
np.ndarray
- get_corrs()[source]#
Get the mode correlations.
- Returns:
corrs – Mode correlations. Shape (n_modes, n_channels, n_channels).
- Return type:
np.ndarray
- get_means_stds_corrs()[source]#
Get the mode means, standard deviations, correlations.
This is a wrapper for
get_means,get_stds,get_corrs.- Returns:
means (np.ndarray) – Mode means. Shape is (n_modes, n_channels).
stds (np.ndarray) – Mode standard deviations. Shape is (n_modes, n_channels, n_channels).
corrs (np.ndarray) – Mode correlations. Shape is (n_modes, n_channels, n_channels).
- Return type:
Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]
- get_observation_model_parameters()[source]#
Wrapper for
get_means_stds_corrs.- Return type:
Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]
- set_means(means, update_initializer=True)[source]#
Set the mode means.
- Parameters:
means (np.ndarray) – Mode means. Shape is (n_modes, n_channels).
update_initializer (bool) – Do we want to use the passed parameters when we re-initialize the model?
- Return type:
None
- set_stds(stds, update_initializer=True)[source]#
Set the mode standard deviations.
- Parameters:
stds (np.ndarray) – Mode standard deviations. Shape is (n_modes, n_channels, n_channels) or (n_modes, n_channels).
update_initializer (bool) – Do we want to use the passed parameters when we re-initialize the model?
- Return type:
None
- set_corrs(corrs, update_initializer=True)[source]#
Set the mode correlations.
- Parameters:
corrs (np.ndarray) – Mode correlations. Shape is (n_modes, n_channels, n_channels).
update_initializer (bool) – Do we want to use the passed parameters when we re-initialize the model?
- Return type:
None
- set_means_stds_corrs(means, stds, corrs, update_initializer=True)[source]#
This is a wrapper for set_means, set_stds, set_corrs.
- Parameters:
means (numpy.ndarray)
stds (numpy.ndarray)
corrs (numpy.ndarray)
update_initializer (bool)
- Return type:
None
- set_observation_model_parameters(observation_model_parameters, update_initializer=True)[source]#
Wrapper for set_means_stds_corrs.
- Parameters:
observation_model_parameters (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray])
update_initializer (bool)
- Return type:
None
- set_regularizers(training_dataset)[source]#
Set the regularizers of means, stds and corrs based on the training data.
A multivariate normal prior is applied to the mean vectors with
mu=0,sigma=diag((range/2)**2), a log normal prior is applied to the standard deviations withmu=0,sigma=sqrt(log(2*range))and a marginal inverse Wishart prior is applied to the functional connectivity matrices withnu=n_channels-1+0.1.- Parameters:
training_dataset (tf.data.Dataset or osl_dynamics.data.Data) – Training dataset.
- Return type:
None