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#

Config

Settings for M-DyNeMo.

Model

M-DyNeMo model class.

Module Contents#

class osl_dynamics.models.mdynemo.Config[source]#

Bases: osl_dynamics.models.mod_base.BaseModelConfig, osl_dynamics.models.inf_mod_base.VariationalInferenceModelConfig

Settings 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 to n_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 clipnorm argument passed to the Keras optimizer. Cannot be used if multi_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.

model_name: str = 'M-DyNeMo'[source]#
inference_rnn: str = 'lstm'[source]#
inference_n_layers: int = 1[source]#
inference_n_units: int = None[source]#
inference_normalization: str = None[source]#
inference_activation: str = None[source]#
inference_dropout: float = 0.0[source]#
inference_regularizer: str = None[source]#
model_rnn: str = 'lstm'[source]#
model_n_layers: int = 1[source]#
model_n_units: int = None[source]#
model_normalization: str = None[source]#
model_activation: str = None[source]#
model_dropout: float = 0.0[source]#
model_regularizer: str = None[source]#
n_corr_modes: int = None[source]#
learn_means: bool = None[source]#
learn_stds: bool = None[source]#
learn_corrs: bool = None[source]#
initial_means: numpy.ndarray = None[source]#
initial_stds: numpy.ndarray = None[source]#
initial_corrs: numpy.ndarray = None[source]#
stds_epsilon: float = None[source]#
corrs_epsilon: float = None[source]#
means_regularizer: tensorflow.keras.regularizers.Regularizer = None[source]#
stds_regularizer: tensorflow.keras.regularizers.Regularizer = None[source]#
corrs_regularizer: tensorflow.keras.regularizers.Regularizer = None[source]#
multiple_dynamics: bool = True[source]#
pca_components: numpy.ndarray = None[source]#
init_method: str = 'random_subset'[source]#
n_init: int = 5[source]#
n_init_epochs: int = 2[source]#
init_take: float = 1.0[source]#
validate_rnn_parameters()[source]#
Return type:

None

validate_observation_model_parameters()[source]#
Return type:

None

validate_dimension_parameters()[source]#
Return type:

None

class osl_dynamics.models.mdynemo.Model(config)[source]#

Bases: osl_dynamics.models.inf_mod_base.VariationalInferenceModelBase

M-DyNeMo model class.

Parameters:

config (osl_dynamics.models.mdynemo.Config)

config_type[source]#
build_model()[source]#

Builds a keras model.

Return type:

None

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 with mu=0, sigma=sqrt(log(2*range)) and a marginal inverse Wishart prior is applied to the functional connectivity matrices with nu=n_channels-1+0.1.

Parameters:

training_dataset (tf.data.Dataset or osl_dynamics.data.Data) – Training dataset.

Return type:

None

sample_time_courses(n_samples)[source]#

Uses the model RNN to sample mode mixing factors, alpha and beta.

Parameters:

n_samples (int) – Number of samples to take.

Returns:

  • alpha (np.ndarray) – Sampled alpha.

  • beta (np.ndarray) – Sampled beta.

Return type:

Tuple[numpy.ndarray, numpy.ndarray]