osl_dynamics.models.base.InitializationMixin#

class osl_dynamics.models.base.InitializationMixin#

Initialization methods of ModelBase.

Expects the class to provide config, keras_model, fit, get_weights, set_weights, reset, reset_optimizer and make_dataset.

initialization(*args, method=None, **kwargs)#

Run an initialization method.

Parameters:
*argsarguments

Arguments for the initialization method.

methodstr, optional

Initialization method name. Defaults to config.init_method.

**kwargskeyword arguments

Keyword arguments for the initialization method.

Returns:
historydict

Training history of the initialization.

random_subset_initialization(training_data, n_epochs=None, n_init=None, take=None, **kwargs)#

Random subset initialization.

The model is trained for a few epochs with different random subsets of the training dataset, starting from the parameters given by reset(). The best model is kept.

Parameters:
training_datatf.data.Dataset or osl_dynamics.data.Data

Dataset to use for training.

n_epochsint, optional

Number of epochs to train the model. By default we use the value passed in the config.

n_initint, optional

Number of initializations. By default we use the value passed in the config.

takefloat, optional

Fraction of total batches to take. By default we use the value passed in the config.

kwargskeyword arguments, optional

Keyword arguments for fit, e.g. n_kl_annealing_epochs for models with KL annealing.

Returns:
historydict

The training history of the best initialization.

random_state_time_course_initialization(training_data, n_epochs=None, n_init=None, take=None, stay_prob=0.9, n_batches=N_ESTIMATION_BATCHES, **kwargs)#

Random state time course initialization.

A state time course is sampled and each state’s parameters are set from the data assigned to it in n_batches batches. The model is then trained for a few epochs and the best candidate is kept.

Parameters:
training_datatf.data.Dataset or osl_dynamics.data.Data

Dataset to use for training.

n_epochsint, optional

Number of epochs to train the model. By default we use the value passed in the config.

n_initint, optional

Number of initializations. By default we use the value passed in the config.

takefloat, optional

Fraction of total batches to take. By default we use the value passed in the config.

stay_probfloat, optional

Stay probability (diagonal for the transition probability matrix) of the sampled state time course. Other states have uniform probability.

n_batchesint, optional

Number of batches used to set each candidate’s initial parameters, None for the whole dataset (which is held in memory). With more batches the candidates differ less.

kwargskeyword arguments, optional

Keyword arguments for fit, e.g. n_kl_annealing_epochs for models with KL annealing.

Returns:
historydict

The training history of the best initialization.

nfindr_initialization(training_data, n_epochs=None, n_init=None, take=None, window_length=20, n_restarts=10, n_batches=N_ESTIMATION_BATCHES, **kwargs)#

N-FINDR initialization.

For models whose covariance at each time point is a sum-to-one mixture of the mode covariances (e.g. DyNeMo and DIVE’s group covariances), the second moment of a short window of data is also a mixture, so the windows lie in a simplex whose vertices are the mode covariances. The mode covariances are set to the vertices found with N-FINDR (nfindr_covariances()) and the model is then trained for a few epochs with different random subsets of the training dataset, each starting from these covariances. The best model is kept and a later reset() returns to the N-FINDR covariances.

Assumes zero-mean data (the means are not initialized). If the covariances are not learned or initial_covariances were passed, the covariances are left as they are and this is the same as random_subset_initialization.

Parameters:
training_datatf.data.Dataset or osl_dynamics.data.Data

Dataset to use for training. The windows are cut from the sequences the model is trained on, so for a Data object only the sessions in training_data.keep are used.

n_epochsint, optional

Number of epochs to train the model. By default we use the value passed in the config.

n_initint, optional

Number of initializations. By default we use the value passed in the config.

takefloat, optional

Fraction of total batches to take. By default we use the value passed in the config.

window_lengthint, optional

Number of samples in each window. Shorter windows are closer to a single mode but noisier. Should be at most the sequence length.

n_restartsint, optional

Number of random starts for N-FINDR.

n_batchesint, optional

Number of batches the windows are cut from, None for the whole dataset (after take). The upper triangle of the covariance of each window is held in memory.

kwargskeyword arguments, optional

Keyword arguments for fit, e.g. n_kl_annealing_epochs for models with KL annealing.

Returns:
historydict

The training history of the best initialization.

single_subject_initialization(training_data, n_epochs=None, n_init=None, **kwargs)#

Single subject initialization.

Pick a subject at random, train a model, repeat a few times. Use the weights from the best model.

Parameters:
training_datalist of tf.data.Dataset or osl_dynamics.data.Data

Datasets for each subject.

n_epochsint, optional

Number of epochs to train. By default we use the value passed in the config.

n_initint, optional

How many subjects should we train on? By default we use the value passed in the config.

kwargskeyword arguments, optional

Keyword arguments for fit, e.g. n_kl_annealing_epochs for models with KL annealing.

Returns:
historydict

The training history of the best initialization.