osl_dynamics.inference.layers
#
Custom Tensorflow layers.
Module Contents#
Classes#
Returns the inputs without modification. |
|
Adds a regularization loss. |
|
Wrapper for tf.concat. |
|
Wrapper for tf.matmul. |
|
Wrapper for tf.range. |
|
Wrapper for tf.zeros. |
|
Layer for getting Cholesky vectors from postive 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 matrix. |
|
Mix a set of vectors. |
|
Layer to mix matrices. |
|
Layer to concatenate vectors and matrices. |
|
Layer to calculate the negative log-likelihood. |
|
Layer to calculate the negative log-likelihood for SAGE/MAGE. |
|
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 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. |
|
Layer to calculate KL divergence between Gamma posterior and exponential |
|
Multi-Layer Perceptron layer. |
|
Layer for calculating the scaling factor for static losses. |
|
Hidden Markov state inference layer. |
|
Layer to calculate the log-likelihood for different HMM states. |
|
Layer for summing log-likelihoods. |
|
Wrapper for tf.gather. |
|
Wrapper for tf.add. |
|
Layer for embeddings. |
|
Clip two tensors to ensure they align for causal forecasting. |
|
Wrapper for tf.constant. |
Functions#
|
Adds epsilon the the diagonal of batches of square matrices |
|
Returns a normalization layer. |
|
Returns a Recurrent Neural Network (RNN) layer. |
Attributes#
- osl_dynamics.inference.layers.add_epsilon(A, epsilon, diag=False)[source]#
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?
- 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.
- 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.
- class osl_dynamics.inference.layers.DummyLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Returns the inputs without modification.
- class osl_dynamics.inference.layers.AddRegularizationLossLayer(reg, strength, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Adds a regularization loss.
Can be used as a wrapper for a keras regularizer. Inputs are used to calculate the regularization loss and returned without modification.
- Parameters:
reg (str) – Type of regularization. Passed to tf.keras.regularizers.get.
strength (float) – Strength of regularization. The regularization is multiplied by the strength before being added to the loss.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.ConcatenateLayer(axis, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper 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.MatMulLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper for tf.matmul.
- class osl_dynamics.inference.layers.TFRangeLayer(limit, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper 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.ZeroLayer(shape, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper 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.InverseCholeskyLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer for getting Cholesky vectors from postive 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.Layer
Layer 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.Layer
Layer 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.
- class osl_dynamics.inference.layers.SampleNormalDistributionLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer 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.Layer
Layer 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.Layer
Layer 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.Layer
Layer 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, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer 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 inference.regularizers.
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.Layer
Layer 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 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.Layer
Layer 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 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.Layer
Layer 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 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.Layer
Layer 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 inference.regularizers.
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.Layer
Layer 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 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.Layer
Mix 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.Layer
Layer 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.ConcatVectorsMatricesLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to concatenate vectors and matrices.
- class osl_dynamics.inference.layers.LogLikelihoodLossLayer(epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer 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:
epsilon (float) – Error added to the covariance matrices for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.AdversarialLogLikelihoodLossLayer(n_channels, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate the negative log-likelihood for SAGE/MAGE.
This layer will add the negative log-likelihood to the loss.
- Parameters:
n_channels (int) – Number of channels.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.KLDivergenceLayer(epsilon, clip_start=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate a KL divergence between two Normal distributions.
- Parameters:
epsilon (float) – Error added to the standard deviations for numerical stability.
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.Layer
Layer 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.Layer
RNN 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.Layer
RNN 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(clip_start=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate a KL divergence between two categorical distributions.
- Parameters:
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, epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate the log-likelihood loss assuming a categorical model.
- Parameters:
n_states (int) – Number of states.
epsilon (float) – Error added to the covariances for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.CategoricalPoissonLogLikelihoodLossLayer(n_states, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate the log-likelihood loss assuming a categorical model with Poisson observation model.
- Parameters:
n_states (int) – Number of states.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.ConcatEmbeddingsLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Layer 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.Layer
Layer 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.Layer
Class 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.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_factor=0.0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Multi-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_factor (float, optional) – Regularizer factor.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the base class.
- class osl_dynamics.inference.layers.StaticLossScalingFactorLayer(**kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer for calculating the scaling factor for static losses.
When calculating loss, we sum over the sequence length (time dimension) and average over the sequences. If we add a static quantity to each time point we need to rescale it to account for the summation over time. The scaling factor is given by
\[\text{static_loss_scaling_factor} = \frac{1}{\text{batch_size} \times \text{n_batches}}\]
- class osl_dynamics.inference.layers.HiddenMarkovStateInferenceLayer(n_states, initial_trans_prob, learn, use_stationary_distribution=False, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Hidden 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.
initial_trans_prob (np.ndarray) – Initial transition probability matrix. Shape must be (n_states, n_states.)
learn (bool) – Should we learn the transition probability matrix?
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, epsilon, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer to calculate the log-likelihood for different HMM states.
- Parameters:
n_states (int) – Number of states.
epsilon (float) – Error added to the covariance matrices for numerical stability.
kwargs (keyword arguments, optional) – Keyword arguments to pass to the normalization layer.
- class osl_dynamics.inference.layers.SumLogLikelihoodLossLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Layer for summing log-likelihoods.
- class osl_dynamics.inference.layers.TFGatherLayer(axis, batch_dims=0, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper for tf.gather.
- class osl_dynamics.inference.layers.AddLayer[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper for tf.add.
- class osl_dynamics.inference.layers.EmbeddingLayer(input_dim, output_dim, unit_norm, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Layer for embeddings.
- Parameters:
input_dim (int) – Input dimension.
output_dim (int) – Output dimension.
unit_norm (bool, optional) – Should the embeddings be unit norm?
- class osl_dynamics.inference.layers.ShiftForForecastingLayer(clip, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Clip two tensors to ensure they align for causal forecasting.
- Parameters:
clip (int) – Number of elements to clip.
- class osl_dynamics.inference.layers.TFConstantLayer(values, **kwargs)[source]#
Bases:
tensorflow.keras.layers.Layer
Wrapper for tf.constant.