osl_dynamics.models.hmm_poi#

Hidden Markov Model (HMM) with a Possion observation model.

Classes#

Config

Settings for HMM-Poisson.

Model

HMM-Poisson class.

Module Contents#

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

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

Settings for HMM-Poisson.

Parameters:
  • model_name (str) – Model name.

  • n_states (int) – Number of states.

  • n_channels (int) – Number of channels.

  • sequence_length (int) – Length of sequence passed to the inference network and generative model.

  • learn_log_rates (bool) – Should we make log_rate for each state trainable?

  • initial_log_rates (np.ndarray) – Initialisation for state log_rates.

  • initial_trans_prob (np.ndarray) – Initialisation for the transition probability matrix.

  • learn_trans_prob (bool) – Should we make the transition probability matrix trainable?

  • trans_prob_prior (np.ndarray) – Dirichlet prior for the transition probability matrix. Each row is the alpha parameters of the Dirichlet distribution.

  • trans_prob_update_delay (float) – We update the transition probability matrix as trans_prob = (1-rho) * trans_prob + rho * trans_prob_update, where rho = (100 * epoch / n_epochs + 1 + trans_prob_update_delay) ** -trans_prob_update_forget. This is the delay parameter.

  • trans_prob_update_forget (float) – We update the transition probability matrix as trans_prob = (1-rho) * trans_prob + rho * trans_prob_update, where rho = (100 * epoch / n_epochs + 1 + trans_prob_update_delay) ** -trans_prob_update_forget. This is the forget parameter.

  • initial_state_probs (np.ndarray) – State probabilities at time=0.

  • learn_initial_state_probs (bool) – Should we make the initial state probabilities trainable?

  • baum_welch_implementation (str) – Which implementation of the Baum-Welch algorithm should we use? Either 'log' (default) or 'rescale'.

  • init_method (str) – Initialization method. Defaults to ‘random_state_time_course’.

  • n_init (int) – Number of initializations. Defaults to 3.

  • n_init_epochs (int) – Number of epochs for each initialization. Defaults to 1.

  • 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).

  • n_epochs (int) – Number of training epochs.

  • optimizer (str or tf.keras.optimizers.Optimizer) – Optimizer to use.

  • 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 = 'HMM-Poisson'[source]#
learn_log_rates: bool = None[source]#
initial_log_rates: numpy.ndarray = None[source]#
init_method: str = 'random_state_time_course'[source]#
n_init: int = 3[source]#
n_init_epochs: int = 1[source]#
init_take: float = 1.0[source]#
validate_observation_model_parameters()[source]#
Return type:

None

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

Bases: osl_dynamics.models.inf_mod_base.MarkovStateInferenceModelBase

HMM-Poisson class.

Parameters:

config (osl_dynamics.models.hmm_poi.Config)

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

Builds a keras model.

Return type:

None

get_log_rates()[source]#

Get the state log_rates.

Returns:

log_rates – State log_rates. Shape is (n_states, n_channels).

Return type:

np.ndarray

get_rates()[source]#

Get the state rates.

Returns:

rates – State rates. Shape is (n_states, n_channels).

Return type:

np.ndarray

get_observation_model_parameters()[source]#

Wrapper for get_log_rates.

Return type:

numpy.ndarray

get_log_likelihood(x)[source]#

Get log-likelihood.

Parameters:
  • data (np.ndarray) – Data to calculate log-likelihood for. Shape must be (batch_size, sequence_length, n_channels).

  • x (Union[numpy.ndarray, tensorflow.Tensor])

Returns:

log_likelihood – Log-likelihood. Shape is (batch_size,).

Return type:

np.ndarray

set_log_rates(log_rates, update_initializer=True)[source]#

Set the state log_rates.

Parameters:
  • log_rates (np.ndarray) – State log_rates. Shape is (n_states, n_channels).

  • update_initializer (bool, optional) – Do we want to use the passed log_rates when we re-initialize the model?

Return type:

None

set_rates(log_rates, epsilon=1e-06, update_initializer=True)[source]#

Set the state rates.

Parameters:
  • rates (np.ndarray) – State rates. Shape is (n_states, n_channels).

  • update_initializer (bool, optional) – Do we want to use the passed log_rates when we re-initialize the model?

  • log_rates (numpy.ndarray)

  • epsilon (float)

Return type:

None

set_observation_model_parameters(observation_model_parameters, update_initializer=True)[source]#

Wrapper for set_log_rates.

Parameters:
  • observation_model_parameters (numpy.ndarray)

  • update_initializer (bool)

Return type:

None

abstractmethod set_regularizers(training_dataset)[source]#

Set regularizers.

Parameters:

training_dataset (Union[tensorflow.data.Dataset, data.Data])

Return type:

None

set_random_state_time_course_initialization(training_dataset)[source]#

Sets the initial log_rates based on a random state time course.

Parameters:

training_dataset (tf.data.Dataset) – Training data.

Return type:

None