osl_dynamics.models.base.MarkovKerasModelBase#
- class osl_dynamics.models.base.MarkovKerasModelBase(config)#
Bases:
osl_dynamics.models.base.model.KerasModelBaseKeras model with a hidden Markov chain.
The posterior of the states (
gammaandxi) is calculated from the log-likelihood of each state with the Baum-Welch algorithm. The loss is the negative expected log-likelihood, whose gradient updates the observation model. The transition probabilities are updated with an exponential moving average after each training step.Subclasses create an observation model,
obs_model, with alog_likelihood(x)method.- Parameters:
- configMarkovConfig
Settings for the model.
- reset_weights(keep=None)#
Re-initialize the weights of the layers.
- Parameters:
- keeplist of str, optional
Names of the layers (attributes of the Keras model) to not re-initialize.
- log_likelihood(inputs)#
Log-likelihood of each state.
- Parameters:
- inputsdict
Batch.
- Returns:
- log_likelihoodtf.Tensor
Shape is (batch_size, sequence_length, n_states).
- markov_outputs(ll)#
Posterior of the states and the negative expected log-likelihood.
- Parameters:
- lltf.Tensor
Log-likelihood of each state. Shape is (batch_size, sequence_length, n_states).
- Returns:
- outputsdict
nll_loss,gammaandxi.
- update_non_trainable_weights(outputs, finite)#
Update weights that are not trained with gradients.
Called after the gradient update in each training step.
- Parameters:
- outputsdict
Outputs of
call.- finitetf.Tensor
Boolean. Were the loss and gradients finite?
- property input_sequence_length: int#
Number of time points in each sequence passed to the Keras model.
- example_inputs()#
Inputs for a batch with one sequence of zeros.
- Returns:
- inputsdict
Batch.
- create_weights()#
Create all weights by calling the Keras model on example inputs.
- regularization()#
Regularization added to the loss during training.
- Returns:
- regularizationtf.Tensor
Scalar.
- anneal(kl_loss, training)#
KL loss multiplied by the KL annealing factor during training.
- Parameters:
- kl_losstf.Tensor
KL loss.
- trainingbool
Is the model training? The KL loss is returned unchanged if not.
- Returns:
- kl_losstf.Tensor
KL loss used in the loss.
- train_step(data)#
Training step.
A step whose loss or gradients contain a NaN or inf is skipped: the weights and optimizer are left as they were and the batch is left out of the losses reported for the epoch.
- Parameters:
- datadict
Batch.
- Returns:
- lossesdict
Average of each loss over the steps of the epoch so far.
- infer(inputs, keys)#
Outputs of
call(not training) for a batch.- Parameters:
- inputsdict
Batch.
- keystuple of str
Outputs to return.
- Returns:
- outputsdict
Requested outputs as numpy arrays.
- named_layers()#
Layers that are attributes of the Keras model.
- Returns:
- layersdict
Layers, keyed by attribute name.