osl_dynamics.inference.layers#
Custom Tensorflow layers.
Attributes#
Classes#
Wrapper for tf.concat. |
|
Wrapper for tf.matmul. |
|
Wrapper for tf.range. |
|
Wrapper for tf.zeros. |
|
Wrapper for tf.broadcast_to. |
|
Wrapper for tf.gather. |
|
Wrapper for tf.add. |
|
Wrapper for tf.constant. |
|
Returns the inputs without modification. |
|
Layer for getting Cholesky vectors from positive definite symmetric matrices. |
|
Layer for getting the batch size. |
|
Layer for sampling from a Gamma distribution. |
|
Layer for sampling from a Normal distribution. |
|
Layer for sampling from a Gumbel-Softmax distribution. |
|
Layer for sampling from a Categorical distribution. |
|
Layer for applying a softmax activation function. |
|
Layer to learn a tensor. |
|
Layer to learn a set of vectors. |
|
Layer to learn a set of covariance matrices. |
|
Layer to learn a set of correlation matrices. |
|
Layer to learn a set of diagonal matrices. |
|
Layer to learn a set of oscillator covariances. |
|
Layer to learn a matrix. |
|
Mix a set of vectors. |
|
Layer to mix matrices. |
|
Layer to calculate the negative log-likelihood. |
|
Layer to calculate a KL divergence between two Normal distributions. |
|
Layer to calculate the KL loss. |
|
RNN inference network. |
|
RNN generative model. |
|
Layer to calculate a KL divergence between two categorical distributions. |
|
Layer to calculate the log-likelihood loss assuming a categorical model. |
|
Layer for getting the concatenated embeddings. |
|
Layer for getting the array specific parameters. |
|
Class for mixing means and covariances. |
|
Gamma exponential KL diverengnce layer. |
|
Multi-Layer Perceptron layer. |
|
Hidden Markov state inference layer. |
|
Layer to calculate the log-likelihood for different HMM states. |
|
Layer to calculate the log-likelihood for different HMM-Poisson states. |
|
Layer for summing log-likelihoods. |
|
Layer for embeddings. |
Functions#
|
Add epsilon. |
|
Returns a normalization layer. |
|
Returns a Recurrent Neural Network (RNN) layer. |
Module Contents#
- osl_dynamics.inference.layers.add_epsilon(A, epsilon, diag=False)[source]#
Add epsilon.
Adds epsilon the the diagonal of batches of square matrices or all elements of matrices.
- Parameters:
A (tf.Tensor) – Batches of square matrices or vectors. Shape is (…, N, N) or (…, N).
epsilon (float) – Small error added to the diagonal of the matrices or every element of the vectors.
diag (bool, optional) – Do we want to add epsilon to the diagonal only?
- Return type:
tensorflow.Tensor
- class osl_dynamics.inference.layers.TFConcatLayer(axis, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.concat.
- Parameters:
axis (int) – Axis to concatenate along.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.TFMatMulLayer[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.matmul.
- class osl_dynamics.inference.layers.TFRangeLayer(limit, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.range.
- Parameters:
limit (int) – Upper limit for range.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.TFZerosLayer(shape, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.zeros.
- Parameters:
shape (tuple) – Shape of the zeros tensor.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.TFBroadcastToLayer(n_modes, n_channels, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.broadcast_to.
- Parameters:
n_modes (int)
n_channels (int)
- class osl_dynamics.inference.layers.TFGatherLayer(axis, batch_dims=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.gather.
- Parameters:
axis (int)
batch_dims (int)
- class osl_dynamics.inference.layers.TFAddLayer[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.add.
- class osl_dynamics.inference.layers.TFConstantLayer(values, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerWrapper for tf.constant.
- Parameters:
values (numpy.ndarray)
- osl_dynamics.inference.layers.NormalizationLayer(norm_type, *args, **kwargs)[source]#
Returns a normalization layer.
- Parameters:
norm_type (str) – Type of normalization layer. Either
'layer','batch'orNone.args (arguments) – Arguments to pass to the normalization layer.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the normalization layer.
- Return type:
tensorflow.keras.layers.Layer
- osl_dynamics.inference.layers.RNNLayer(rnn_type, *args, **kwargs)[source]#
Returns a Recurrent Neural Network (RNN) layer.
- Parameters:
rnn_type (str) – Type of RNN. Either
'lstm'or'gru'.args (arguments) – Arguments to pass to the normalization layer.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the normalization layer.
- Return type:
tensorflow.keras.layers.Layer
- class osl_dynamics.inference.layers.DummyLayer[source]#
Bases:
tensorflow.keras.layers.LayerReturns the inputs without modification.
- class osl_dynamics.inference.layers.InverseCholeskyLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for getting Cholesky vectors from positive definite symmetric matrices.
- Parameters:
epsilon (float) – Small error added to the diagonal of the matrices.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.BatchSizeLayer[source]#
Bases:
tensorflow.keras.layers.LayerLayer for getting the batch size.
- Parameters:
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.SampleGammaDistributionLayer(epsilon, do_annealing, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for sampling from a Gamma distribution.
This layer is a wrapper for tfp.distributions.Gamma.
- Parameters:
epsilon (float) – Error to add to the shape and rate for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
do_annealing (bool)
- class osl_dynamics.inference.layers.SampleNormalDistributionLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for sampling from a Normal distribution.
This layer is a wrapper for tfp.distributions.Normal.
- Parameters:
epsilon (float) – Error to add to the standard deviations for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.SampleGumbelSoftmaxDistributionLayer(temperature, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for sampling from a Gumbel-Softmax distribution.
This layer is a wrapper for tfp.distributions.RelaxedOneHotCategorical.
- Parameters:
temperature (float) – Temperature for the Gumbel-Softmax distribution.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.SampleOneHotCategoricalDistributionLayer[source]#
Bases:
tensorflow.keras.layers.LayerLayer for sampling from a Categorical distribution.
This layer is a wrapper for tfp.distributions.OneHotCategorical.
- class osl_dynamics.inference.layers.SoftmaxLayer(initial_temperature, learn_temperature, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for applying a softmax activation function.
- Parameters:
initial_temperature (float) – Temperature parameter.
learn_temperature (bool) – Should we learn the alpha temperature?
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.LearnableTensorLayer(shape, learn=True, initializer=None, initial_value=None, regularizer=None, constraint=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a tensor.
- Parameters:
shape (tuple) – Shape of the tensor.
learn (bool, optional) – Should we learn the tensor?
initializer (tf.keras.initializers.Initializer, optional) – Initializer for the tensor.
initial_value (float, optional) – Initial value for the tensor if initializer is not passed.
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.constraint (tf.keras.constraints.Constraint, optional) – Constraint for the tensor. Limits the values the weights can take.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.VectorsLayer(n, m, learn, initial_value, regularizer=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a set of vectors.
- Parameters:
n (int) – Number of vectors.
m (int) – Number of elements.
learn (bool) – Should we learn the vectors?
initial_value (np.ndarray) – Initial value for the vectors. Shape must be (n, m).
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.CovarianceMatricesLayer(n, m, learn, initial_value, epsilon, regularizer=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a set of covariance matrices.
A cholesky factor is learnt and used to calculate a covariance matrix as \(C = LL^T\), where \(L\) is the cholesky factor. The cholesky factor is learnt as a vector of free parameters.
- Parameters:
n (int) – Number of matrices.
m (int) – Number of rows/columns.
learn (bool) – Should the matrices be learnable?
initial_value (np.ndarray) – Initial values for the matrices. Shape must be (n, m, m).
epsilon (float) – Error added to the diagonal of covariances matrices for numerical stability.
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.CorrelationMatricesLayer(n, m, learn, initial_value, epsilon, regularizer=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a set of correlation matrices.
A cholesky factor is learnt as a vector of free parameters and used to calculate a correlation matrix.
- Parameters:
n (int) – Number of matrices.
m (int) – Number of rows/columns.
learn (bool) – Should the matrices be learnable?
initial_value (np.ndarray) – Initial values for the matrices. Shape must be (n, m, m).
epsilon (float) – Error added to the diagonal of correlation matrices for numerical stability.
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.DiagonalMatricesLayer(n, m, learn, initial_value, epsilon, regularizer=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a set of diagonal matrices.
The diagonal is forced to be positive using a softplus transformation.
- Parameters:
n (int) – Number of matrices.
m (int) – Number of rows/columns.
learn (bool) – Should the matrices be learnable?
initial_value (np.ndarray) – Initial values for the matrices. Shape must be (n, m, m) or (n, m).
epsilon (float) – Error added to the diagonal matrices for numerical stability.
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.OscillatorCovarianceMatricesLayer(n, m, sampling_frequency, frequency_range, learn, epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a set of oscillator covariances.
- Parameters:
n (int) – Number of matrices.
m (int) – Number of rows/columns.
sampling_frequency (float) – Sampling frequency in Hz.
frequency_range (tuple[float, float]) – Limits for the frequency parameter. Upper limit should not be higher than the Nyquist frequency.
learn (bool) – Should we learn the covariances?
epsilon (float) – Error added to the diagonal matrices for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.MatrixLayer(m, constraint, learn, initial_value, epsilon, regularizer=None, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to learn a matrix.
- Parameters:
m (int) – Number of rows/columns.
constraint (str) – Either ‘covariance’ or ‘diagonal’.
learn (bool) – Should the matrix be trainable?
initial_value (np.ndarray) – Initial value for the matrix.
epsilon (float) – Error added to the matrices for numerical stability.
regularizer (osl-dynamics regularizer, optional) – Regularizer for the tensor. Must be from
osl_dynamics.inference.regularizers.kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.MixVectorsLayer[source]#
Bases:
tensorflow.keras.layers.LayerMix a set of vectors.
The mixture is calculated as \(m_t = \displaystyle\sum_j \alpha_{jt} \mu_j\), where \(\mu_j\) are the vectors and \(\alpha_{jt}\) are mixing coefficients.
- class osl_dynamics.inference.layers.MixMatricesLayer[source]#
Bases:
tensorflow.keras.layers.LayerLayer to mix matrices.
The mixture is calculated as \(C_t = \displaystyle\sum_j \alpha_{jt} D_j\), where \(D_j\) are the matrices and \(\alpha_{jt}\) are mixing coefficients.
- class osl_dynamics.inference.layers.LogLikelihoodLossLayer(calculation, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate the negative log-likelihood.
We assume a multivariate normal probability density. This layer will add the negative log-likelihood to the loss.
- Parameters:
calculation (str) – Operation for reducing the time dimension. Either ‘mean’ or ‘sum’.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.KLDivergenceLayer(epsilon, calculation, clip_start=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate a KL divergence between two Normal distributions.
- Parameters:
epsilon (float) – Error added to the standard deviations for numerical stability.
calculation (str) – Operation for reducing the time dimension. Either ‘mean’ or ‘sum’.
clip_start (int, optional) – Index to clip the sequences inputted to this layer. Default is no clipping.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.KLLossLayer(do_annealing, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate the KL loss.
This layer sums KL divergences if multiple values as passed, applies an annealing factor and adds the value to the loss function.
- Parameters:
do_annealing (bool) – Should we perform KL annealing?
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.InferenceRNNLayer(rnn_type, norm_type, act_type, n_layers, n_units, drop_rate, reg, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerRNN inference network.
- Parameters:
rnn_type (str) – Either
'lstm'or'gru'.norm_type (str) – Either
'layer','batch'orNone.act_type ('str') – Activation type, e.g.
'relu','elu', etc.n_layers (int) – Number of layers.
n_units (int) – Number of units/neurons per layer.
drop_rate (float) – Dropout rate for the output of each layer.
reg (str) – Regularization for each layer.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.ModelRNNLayer(rnn_type, norm_type, act_type, n_layers, n_units, drop_rate, reg, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerRNN generative model.
- Parameters:
rnn_type (str) – Either
'lstm'or'gru'.norm_type (str) – Either
'layer','batch'orNone.act_type ('str') – Activation type, e.g.
'relu','elu', etc.n_layers (int) – Number of layers.
n_units (int) – Number of units/neurons per layer.
drop_rate (float) – Dropout rate for the output of each layer.
reg (str) – Regularization for each layer.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.CategoricalKLDivergenceLayer(calculation, clip_start=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate a KL divergence between two categorical distributions.
- Parameters:
calculation (str) – Operation for reducing the time dimension. Either ‘mean’ or ‘sum’.
clip_start (int, optional) – Index to clip the sequences inputted to this layer. Default is no clipping.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.CategoricalLogLikelihoodLossLayer(n_states, calculation, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate the log-likelihood loss assuming a categorical model.
- Parameters:
n_states (int) – Number of states.
calculation (str) – Operation for reducing the time dimension. Either ‘mean’ or ‘sum’.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.ConcatEmbeddingsLayer[source]#
Bases:
tensorflow.keras.layers.LayerLayer for getting the concatenated embeddings.
The concatenated embeddings are obtained by concatenating embeddings and spatial embeddings.
- class osl_dynamics.inference.layers.SessionParamLayer(param, epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for getting the array specific parameters.
This layer adds deviations to the group spatial parameters.
- Parameters:
param (str) – Which parameter are we using? Must be
'means'or'covariances'.epsilon (float) – Error added to the diagonal of covariances for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.MixSessionSpecificParametersLayer[source]#
Bases:
tensorflow.keras.layers.LayerClass for mixing means and covariances.
The mixture is calculated as
\(m_t = \displaystyle\sum_j \alpha_{jt} \mu_j^{s_t}\)
\(C_t = \displaystyle\sum_j \alpha_{jt} D_j^{s_t}\)
where \(s_t\) is the array at time \(t\).
- class osl_dynamics.inference.layers.GammaExponentialKLDivergenceLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerGamma exponential KL diverengnce layer.
Layer to calculate KL divergence between Gamma posterior and exponential prior for deviation magnitude.
- Parameters:
epsilon (float) – Error added to the standard deviations for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.MultiLayerPerceptronLayer(n_layers, n_units, norm_type, act_type, drop_rate, regularizer=None, regularizer_strength=1, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerMulti-Layer Perceptron layer.
- Parameters:
n_layers (int) – Number of layers.
n_units (int) – Number of units/neurons.
norm_type (str) – Normalization layer type. Can be
'layer','batch'orNone.act_type (str) – Activation type.
drop_rate (float) – Dropout rate.
regularizer (str, optional) – Regularizer type.
regularizer_strength (float, optional) – Constant to multiply the regularization by.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.HiddenMarkovStateInferenceLayer(n_states, sequence_length, initial_trans_prob, trans_prob_prior, initial_state_probs, learn_trans_prob, learn_initial_state_probs, implementation='rescale', use_stationary_distribution=False, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerHidden Markov state inference layer.
This layer uses the Baum-Welch algorithm to calculate the posterior for the hidden state in a Hidden Markov Model (HMM).
- Parameters:
n_states (int) – Number of states.
sequence_length (int) – Length of the sequence.
initial_trans_prob (np.ndarray) – Initial transition probability matrix. Shape must be (n_states, n_states).
trans_prob_prior (np.ndarray) – Dirichlet prior for the transition probability matrix.
initial_state_probs (np.ndarray) – Initial transition probability matrix. Shape must be (n_states,)
learn_trans_prob (bool) – Should we learn the transition probability matrix?
learn_initial_state_probs (bool) – Should we learn the initial state probabilities?
implementation (str, optional) – ‘rescale’ or ‘log’ implementation of the Baum-Welch algorithm.
use_stationary_distribution (bool, optional) – Should we use the stationary distribution (estimated from the transition probability matrix) for the initial state probabilities?
kwargs (keyword arguments, optional) – Keyword arguments to pass to the normalization layer.
- class osl_dynamics.inference.layers.SeparateLogLikelihoodLayer(n_states, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate the log-likelihood for different HMM states.
- Parameters:
n_states (int) – Number of states.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the keras.layers.Layer.
- class osl_dynamics.inference.layers.SeparatePoissonLogLikelihoodLayer(n_states, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer to calculate the log-likelihood for different HMM-Poisson states.
- Parameters:
n_states (int) – Number of states.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the keras.layers.Layer.
- class osl_dynamics.inference.layers.SumLogLikelihoodLossLayer(calculation, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for summing log-likelihoods.
- Parameters:
calculation (str) – Operation for reducing the time dimension. Either ‘mean’ or ‘sum’.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the keras.layers.Layer.
- class osl_dynamics.inference.layers.EmbeddingLayer(input_dim, output_dim, unit_norm, **kwargs)[source]#
Bases:
tensorflow.keras.layers.LayerLayer for embeddings.
- Parameters:
input_dim (int) – Input dimension.
output_dim (int) – Output dimension.
unit_norm (bool, optional) – Should the embeddings be unit norm?